V哥官网:http://www.vgxit.com
本博客对应视频教程:http://www.vgxit.com/course/23
加载属性配置文件
1,概述
在实际的开发中往往有很多配置是写在properties类型的配置文件中的。我们使用Spring的时候,也需要能够加载并读取到配置文件。解决的思路是,spring应用在启动的时候,找到对应的配置文件,然后读取到内存中,要使用的时候,我们调用Spring为我们提供的Api进行读取。
2,使用直接加载的方式加载读取properties文件:
首先,我们在resources下面创建一个properties配置文件。这里我们就用之前用过的druid.properties文件就好了:
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis.ktdm?serverTimezone=Asia/Shanghai
username=root
password=Abc@123456
# 初始化连接数量
initialSize=5
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000
我们定义一个Spring框架在家配置的类:
package com.vgxit.learn.spring.ktdm.properties.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@ComponentScan(basePackages = "com.vgxit.learn.spring.ktdm.properties")
@Configuration
@PropertySource(value = "classpath:druid.properties", ignoreResourceNotFound = true)//spring启动的时候,会自动的读取druid.properties文件的配置放在内存中
public class PropConfig {
}
这里,我们使用了一个注解@PropertySource来定义我们的properties配置文件的路劲。这个注解有几个配置项,我们这里说一下:
- name:字符串,表示这次属性配置的名称,一般情况下,我们不用填写
- value:对应的配置文件的数组
- ignoreResourceNotFound:如果找不到配置文件,是否忽略。默认情况是下false,如果是true表示如果找不到对应的配置文件直接忽略。如果是false的话,找不到对应的配置文件会直接抛出异常
对应的测试代码如下:
package com.vgxit.learn.spring.ktdm.properties.test;
import com.vgxit.learn.spring.ktdm.properties.config.PropConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class PropertyTest {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(PropConfig.class);
String url = ctx.getEnvironment().getProperty("url");
System.out.println(url);
}
}
3,通过属性占位符加载:
通过属性占位符的方式加载配置文件,意思就是我们把对应的配置文件加载进来之后,赋值给一个我们已经定义好的对象的各个属性。
1,首先我们定义好一个项目配置类,配置类里面我们注入一个可以实现属性加载的配置工具
package com.vgxit.learn.spring.ktdm.properties.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@ComponentScan("com.vgxit.learn.spring.ktdm.properties")
@Configuration
@PropertySource("classpath:druid.properties")
public class PropSeatConfig {
/**
* 注入舒心占位工具类
* @return
*/
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
这个地方我们注入了一个Bean,名字叫做PropertySourcesPlaceholderConfigurer。它的作用就是让Spring能够解析属性占位符。这里只要创建了这个Bean,Spring会自动的给我们属性占位符赋值。
2,定义配置文件:
datasource.driverClassName=com.mysql.cj.jdbc.Driver
datasource.url=jdbc:mysql://localhost:3306/mybatis.ktdm?serverTimezone=Asia/Shanghai
datasource.username=root
datasource.password=Abc@123456
# 初始化连接数量
datasource.initialSize=5
# 最大连接数
datasource.maxActive=10
# 最大等待时间
datasource.maxWait=3000
3,编写一个JavaBean来接收这些配置:
package com.vgxit.learn.spring.ktdm.properties.propconfig;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@Data
public class DataSourceConfig {
@Value("${driverClassName}")
private String driverClassName;
@Value("${url}")
private String url;
@Value("${username}")
private String username;
@Value("${password}")
private String password;
@Value("${initialSize}")
private Integer initialSize;
@Value("${maxActive}")
private Integer maxActive;
@Value("${maxWait}")
private Integer maxWait;
}
4,测试代码:
package com.vgxit.learn.spring.ktdm.properties.test;
import com.vgxit.learn.spring.ktdm.properties.config.PropSeatConfig;
import com.vgxit.learn.spring.ktdm.properties.propconfig.DataSourceConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class PropSetTest {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(PropSeatConfig.class);
DataSourceConfig dataSourceConfig = ctx.getBean(DataSourceConfig.class);
System.out.println(dataSourceConfig);
}
}
这个时候我们发现除了username之外的其他属性都是OK的。username属性编程Administrator。这个是因为我们使用windows操作系统,当前登陆的用户就是Administrator,而spring启动的时候,读取配置会优先读取系统配置。所以我们为了解决这个问题,可以再配置上加上前缀:
datasource.driverClassName=com.mysql.cj.jdbc.Driver
datasource.url=jdbc:mysql://localhost:3306/mybatis.ktdm?serverTimezone=Asia/Shanghai
datasource.username=root
datasource.password=Abc@123456
# 初始化连接数量
datasource.initialSize=5
# 最大连接数
datasource.maxActive=10
# 最大等待时间
datasource.maxWait=3000
package com.vgxit.learn.spring.ktdm.properties.propconfig;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@Data
public class DataSourceConfig {
@Value("${datasource.driverClassName}")
private String driverClassName;
@Value("${datasource.url}")
private String url;
@Value("${datasource.username}")
private String username;
@Value("${datasource.password}")
private String password;
@Value("${datasource.initialSize}")
private Integer initialSize;
@Value("${datasource.maxActive}")
private Integer maxActive;
@Value("${datasource.maxWait}")
private Integer maxWait;
}
现在再来查看打印配置类,一切正常。
4,通过XML的方式加载配置文件
1,首先我们定义好接收properties配置文件的类。
package com.vgxit.learn.spring.ktdm.properties.propconfig;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@Data
public class DataSourceConfig {
@Value("${datasource.driverClassName}")
private String driverClassName;
@Value("${datasource.url}")
private String url;
@Value("${datasource.username}")
private String username;
@Value("${datasource.password}")
private String password;
@Value("${datasource.initialSize}")
private Integer initialSize;
@Value("${datasource.maxActive}")
private Integer maxActive;
@Value("${datasource.maxWait}")
private Integer maxWait;
}
2,创建对应的xml,加上对应xsd和对应的配置:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--定义对应的扫描包-->
<context:component-scan base-package="com.vgxit.learn.spring.ktdm.properties"/>
<!--引入一个配置文件,如果有多个配置文件,我们用逗号隔开-->
<context:property-placeholder ignore-resource-not-found="false" location="druid.properties"/>
</beans>