您的位置:首页 > 编程语言 > Java开发

Spring应用的运行环境及属性配置综述

2017-09-06 13:46 435 查看
1.Spring Framework的Environment接口
Spring Framework核心模块中的org.springframework.core.env.Environment接口,表示Spring应用的运行环境,用以表示Spring应用的profile和properties。



2.Spring Framework的profiles
1) 定义profiles
Spring的profiles是Spring应用的properties集合,可以根据不同的运行环境将properties分组为profile,在每一个profile中为某属性配置不同的值。

在定义任何一个Spring bean时,我们都可以将其定义为属于某个profile的Spring bean,只要使用标注@Profile("a_profile_name")。如下所示:
@Configuration
@Profile("production")
public class MyConfigurationsInProduction {

// ...

}或@Configuration
public class AppConfig {

@Bean("dataSource")
@Profile("default")
public DataSource standaloneDataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL)
.addScript("classpath:com/bank/config/sql/schema.sql")
.addScript("classpath:com/bank/config/sql/test-data.sql")
.build();
}

@Bean("dataSource")
@Profile("production")
public DataSource jndiDataSource() throws Exception {
Context ctx = new InitialContext();
return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
}
}
2) 设置活动profiles
在一个Spring应用中可以存在多个profiles,那么究竟那个(或哪几个)profiles有效呢,可以通过属性spring.profiles.active设置。
可以在应用代码中设置spring.profiles.active属性如下:
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.getEnvironment().setActiveProfiles("development");
ctx.register(SomeConfig.class, StandaloneDataConfig.class, JndiDataConfig.class);
ctx.refresh();

也可以在application.properties文件中配置spring.profiles.active属性如下:
spring.profiles.active=product
...
还可以在Spring应用的启动命令行中设置启动参数如下:
java ... --spring.profiles.active=product, mysqldb注意,这里同时设置了多个active的profiles。

对于Spring Boot应用,还可以在启动应用之前,调用方法设置active的profiles。
@SpringBootApplication
public class App
{
public static void main( String[] args )
{
SpringApplication.setAdditionalProfiles("mangodb");
SpringApplication.run(App.class, args);
}
}
3) profile专有的属性文件
首先必须明确,任何Spring应用都可以采用基本的属性文件application.properties。除此之外,Spring应用还可以为不同的profile提供专有的属性文件,专有属性文件的命名规范为application-{myprofilename}.properties。如名为product的profile,其专有的属性文件为application-product.properties。
如果Spring应用启动时没有设置任何active的profile,这种情况下使用的专有属性文件为application-default.properties,即默认属性文件。
显然,profile专有的属性文件优先级高于application.properties文件。

3.Spring Framework的属性
1) 定义属性
有多种定义属性的方式,按照属性定义的优先级从高到低,主要有如下方式:

应用启动的命令行参数
如java ... --server.port=80
应用运行所在的操作系统环境变量
应用中profile相关的application-{profile}.properties文件
应用中profile无关的application.properties文件

2) 通过Environment对象引用属性
ApplicationContext ctx = new GenericApplicationContext();
Environment env = ctx.getEnvironment();
boolean containsFoo = env.containsProperty("a.prop.name");
System.out.println("Does my environment contain the 'a.prop.name' property? " + containsFoo);
3) 通过@PropertySource("classpath:/a/properties/file/path")注入Environment对象并引用属性
@Configuration
@PropertySource("classpath:/com/ericsson/myapp.properties")
public class AppConfig {
@Autowired
Environment env;

@Bean
public TestBean testBean() {
TestBean testBean = new TestBean();
testBean.setName(env.getProperty("a.prop.name"));
return testBean;
}
}
4) 通过@Value("${a_property_name}")直接注入配置属性的值
@Component
public class MyBean {

@Value("${a.prop.name}")
private String name;

}

参考链接: https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-environment https://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/htmlsingle/#boot-features-external-config https://docs.spring.io/spring/docs/4.3.9.RELEASE/javadoc-api/overview-summary.html https://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/api/org/springframework/boot/SpringApplication.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐