<?xml version="1.0" encoding="UTF-8"?>
4.0.0com.spring.propertysourcespring-propertysource0.0.1-SNAPSHOTspring-propertysourceDemo project for Spring Boot1.84.3.13.RELEASEorg.springframeworkspring-core${spring.version}org.springframeworkspring-context${spring.version}org.apache.maven.pluginsmaven-compiler-plugin3.21.61.6
一般把版本名称统一定义在 标签中,便于统一管理,如上可以通过${…} 来获取指定版本。
定义一个application.properties 来写入如下配置
com.spring.name=liuXuan
com.spring.age=18
新建一个TestBean,定义几个属性
public class TestBean {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "TestBean{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
新建一个main class ,用来演示@PropertySource 的使用
@Configuration
@PropertySource(value = "classpath:application.properties",ignoreResourceNotFound = false)
public class SpringPropertysourceApplication {
@Resource
Environment environment;
@Bean
public TestBean testBean(){
TestBean testBean = new TestBean();
// 读取application.properties中的name
testBean.setName(environment.getProperty("com.spring.name"));
// 读取application.properties中的age
testBean.setAge(Integer.valueOf(environment.getProperty("com.spring.age")));
System.out.println("testBean = " + testBean);
return testBean;
}
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringPropertysourceApplication.class);
TestBean testBean = (TestBean)applicationContext.getBean("testBean");
}
}
输出:
testBean = TestBean{name='liuXuan', age=18}
Refreshing the spring context
public class SpringMainClass {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
// 注解扫描,和@ComponentScan 和 基于XML的配置相同
context.scan("com.spring.propertysource.app");
// 刷新上下文环境
context.refresh();
System.out.println("Refreshing the spring context");
// 获取DBConnection这个Bean,调用其中的方法
DBConnection dbConnection = context.getBean(DBConnection.class);
dbConnection.printDBConfigs();
// 关闭容器(可以省略,容器可以自动关闭)
context.close();
}
}
输出:
Refreshing the spring context
Db Driver Class = com.mysql.jdbc.Driver
Db url = jdbc:mysql://localhost:3306/test
Db username = cxuan
Db password = 111111
Input Argument 1 = Test
Input Argument 2 = another variable
Home Directory = /Users/mr.l
Default Configuration Name = Default DBConfiguration
Default App Name = Default
Java Home = /Library/Java/JavaVirtualMachines/jdk1.8.0_191.jdk/Contents/Home/jre
Home dir = /Users/mr.l
Boolean = true
Int = 10
Refreshing the spring context
Db Driver Class = com.mysql.jdbc.Driver
Db url = jdbc:mysql://localhost:3306/test
Db username = cxuan
Db password = 111111
// 导入BasicPropertyWithJavaConfig类
@Import(BasicPropertyWithJavaConfig.class)
public class PropertySourceReadApplication {
@Resource
private Environment env;
@Value("${com.spring.name}")
private String name;
@Bean("context")
public PropertySourceReadApplication contextLoadInitialized(){
// 用environment 读取配置文件
System.out.println(env.getProperty("com.spring.age"));
// 用@Value 读取配置文件
System.out.println("name = " + name);
return null;
}
public static void main(String[] args) {
// AnnotationConnfigApplicationContext 内部会注册Bean
new AnnotationConfigApplicationContext(PropertySourceReadApplication.class);
}
}
新建一个BasicPropertyWithJavaConfig 类,用于配置类并加载配置文件
@Configuration
@PropertySource(value = "classpath:application.properties")
public class BasicPropertyWithJavaConfig {
public BasicPropertyWithJavaConfig(){
super();
}
}