Spring的Propertysource和Environment

从3.1版本开始,Spring提供了@PropertySource注解来获取properties文件中的配置。接下来,我们就演示如何通过@PropertySource读配置,并且如何通过@Value@Environment来获取数据。

@PropertySource and @Value

读取配置文件和通过@Value(${})获取配置

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@Configuration
@ComponentScan(basePackages = { "com.sina.ddl.*" })
@PropertySource("classpath:config.properties")
public class AppConfigMongoDB {
// 1.2.3.4
@Value("${mongodb.url}")
private String mongodbUrl;
// hello
@Value("${mongodb.db}")
private String defaultDb;
@Bean
public MongodbProperties mongodbByValue() {
MongodbProperties mongodb = new MongodbProperties();
mongodb.setUrl(mongodbUrl);
mongodb.setDb(defaultDb);
return mongodb;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = FavourApplication.class)
@WebAppConfiguration
public class AppConfigMongoDBTest {
@Autowired
MongodbProperties mongodbByValue;
@Test
public void testValue1() {
System.out.println(mongodbByValue.getUrl() + "| " + mongodbByValue.getDb());
}
}

配置

1
2
mongodb.url=1.2.3.4
mongodb.db=hello

结果

1.2.3.4|hello

PropertySourcesPlaceholderConfigurer必须要进行加载初始化

@PropertySource and Environment

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Configuration
@ComponentScan(basePackages = { "com.sina.ddl.*" })
@PropertySource("classpath:config.properties")
public class AppConfigMongoDBByEnv {
@Autowired
private Environment env;
@Bean
public MongodbProperties mongodbByEnv() {
String mongodbUrl = env.getProperty("mongodb.url2");
String defaultDb = env.getProperty("mongodb.db2");
MongodbProperties mongodb = new MongodbProperties();
mongodb.setUrl(mongodbUrl);
mongodb.setDb(defaultDb);
return mongodb;
}
}
@Autowired
@Qualifier("mongodbByEnv")
MongodbProperties mongodbByEnv;
@Test
public void testValue2() {
System.out.println(mongodbByEnv.getUrl() + "|" + mongodbByEnv.getDb());
}

配置

1
2
mongodb.url2=5.6.7.8
mongodb.db2=world

结果

5.6.7.8|world

Profile

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@Configuration
public class AppConfigMysql {
@Bean
@Profile("dev")
public MysqlProperties mysqlDev() {
MysqlProperties mysqlProp = new MysqlProperties();
mysqlProp.setUsername("u1");
mysqlProp.setPassword("p1");
return mysqlProp;
}
@Bean
@Profile("prod")
public MysqlProperties mysqlProd() {
MysqlProperties mysqlProp = new MysqlProperties();
mysqlProp.setUsername("u2");
mysqlProp.setPassword("p2");
return mysqlProp;
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = FavourApplication.class)
@WebAppConfiguration
@ActiveProfiles("prod")
public class AppConfigMysqlTest {
@Autowired
MysqlProperties mysql;
@Test
public void testProfile1() {
System.out.println("u:" + mysql.getUsername() + ", p:" + mysql.getPassword());
}
}

结果

@ActiveProfiles(“dev”)

u:u1, p:p1

@ActiveProfiles(“prod”)

u:u2, p:p2

Springboot配置文件

结论

  • spring.profiles.active设置的profile的配置的优先级更高
  • 如果该profile中找不到该配置,则使用application.properties中的配置

示例

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = FavourApplication.class)
@WebAppConfiguration
public class ProfileTest {
@Value("${env}")
String env;
@Test
public void testEnv() {
System.out.println(env);
}
}

配置

1
2
3
4
5
6
7
8
9
# application.properties
spring.profiles.active=cacaca
env=env
# application-cacaca.properties
env=cacacaenv
# application-dev.properties
env=devenv

执行结果

  1. spring.profiles.active=cacaca时,testEnv执行结果是cacacaenv*
  2. spring.profiles.active=dev时,testEnv执行结果是devenv
  3. spring.profiles.active=dev,并且注释掉application-dev.properties中的env=devenv时,testEnv执行结果是env