读取配置文件内容
1、springboot读取配置文件:
a、读取默认的application.yml
在属性通过注解注入
@Value("${demo.name}")
public String name;
@Value("${demo.age}")
public String age;
b、通过配置类注入bean
@Component
public class ConfigBeanValue {
@Value("${demo.name}")
public String name;
@Value("${demo.age}")
public String age;
}
然后
@Autowired
ConfigBeanValue configBeanValue ;
以及另获取其他配置文件内容:
@Component
@ConfigurationProperties(prefix = "demo")
@PropertySource(value = "config.properties")
c、环境变量获取
Environment.getProperty("demo.age")
springboot引入其他配置文件:
application.yml:
spring:
profiles:
active: dev
多环境使用application-dev.yml
spring:
profiles:
include: db
引入application-db.yml
2、java使用Properties获取配置文件内容
static {
properties = new Properties();
try {
properties.load(getProperties.class.getClassLoader().getResourceAsStream("./myConfig.properties"));
}catch(IOException e) {
throw new RuntimeException(e.getMessage(),e);
}
}
properties.getProperty(key);
3、spring获取配置文件内容
<!--
用途1:Spring的xml配置文件中,可以通过${属性名}使用properties文件配置的值
用途2:可以使用@Value("${属性名}")注解读取properties文件配置的值,再给字段赋值
方法1:注解在字段上,给字段赋值
方法2:注解在字段的setter方法中赋值
-->
a、
<context:property-placeholder location="classpath:jdbc.properties"/>
b、
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/configs/jdbc.properties</value>
</property>
</bean>
c、
使用org.springframework.core.io.support包下的PropertiesLoaderUtils读取配置文件从而获得对应的配置信息
PropertiesLoaderUtils.loadProperties(new EncodedResource(new ClassPathResource(location), "UTF-8"));
d、@Value注解
2、读取数据库配置文件,配置数据源
@Configuration
@PropertySource(value={"classpath:jdbc.properties"})
@ConfigurationProperties(prefix="database",ignoreUnknownFields = false)
@Component
//@ConfigurationProperties(prefix="database",locations="classpath:jdbc.properties")高版本中@ConfigurationProperties去除了locations的属性
public class PropertyConfig {
private String driverName=null;
private String url=null;
private String username=null;
private String password=null;
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Bean
public DataSource druidDataSource() {
DataSource datasource = new DataSource();
datasource.setUrl(url);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverName);
return datasource;
}
/**另一种方式连接数据库
*
@Bean
public DataSource dataSource() {
DataSource datasource = null;
Properties pros=new Properties();
pros.setProporties("url",url);
...
try{
datasource=BasicDataSourceFactory.createDataSource(pros);
}catch(){}
return datasource;
}
* */
}
3、entity持久化
@Entity
@Table(name = "table_user")
public class User implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String sex;
private int age;
private String phone;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
@Repository
public interface UserRepository extends JpaRepository<User,Integer>{
}
@Repository
public class UserDao {
@Autowired
private PropertyConfig propertyConfig;
@Autowired
private UserRepository userRepository;
@Transactional
public void saveUser(User user){
userRepository.saveAndFlush(user);
}
@Transactional
public User queryById(int id){
User user=(User) userRepository.findOne(id);
return user;
}
@Transactional
public List queryAll(){
List userList=userRepository.findAll();
return userList;
}
}
maven配置:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--spring操作数据库jpa 用于将数据存入数据库的类和方法的集-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--数据库相关-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 引用properties配置文件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<build>
<finalName>springbootStudy</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.6.RELEASE</version>
</plugin>
</plugins>
</build>