Spring应用 2 profile加载不同环境配置
profile的应用
通过Spring profile属性可以帮助我们快速方便的切换环境。使得开发、测试、生产环境得到快速切换。
1.不同环境加载不同的配置文件(数据库连接地址、api接口、用户密码等)
2.不同环境加载不同的接口实现类(数据源、业务逻辑、结果处理)
3.不同环境定义不同的日志级别
通过下面的示例中,
可以通过profile可以选择加载不同的配置文件,需要将不同环境的配置文件放置在不同的目录下。
可以在不同环境下加载不同的数据源和工具。例如在开发环境,数据源可能是txt文本,但是在线上环境的数据源就能自动替换成mysql 的数据源。
可以提供不同的工具,例如在开发环境需要将数据落地到本地txt,在线上环境则需要更新到mysql数据库中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<beans profile="dev">
<bean id="instance" class="com.mdq.util.DevInstance" />
<bean id="dataSourse" class="com.mdq.data.TxtDataSource" />
<context:property-placeholder location="classpath*:dev/*.properties" ignore-unresolvable="true" />
</beans>
<beans profile="prod">
<bean id="instance" class="com.mdq.util.ProdInstance" />
<bean id="dataSourse" class="com.mdq.data.MysqlDataSource" />
<context:property-placeholder location="classpath*:test/*.properties" ignore-unresolvable="true" />
</beans>
//以下两个实例不受profile的影响,和正常情况下加载实例一样。
<bean id="common1" class="com.mdq.util.Common1" />
<bean id="common2" class="com.mdq.util.Common2" />
</beans>
激活profile
spring提供了多种方式去帮助我们去激活profile。可以通过代码、系统环境变量、JVM参数、servlet上下文、Junit下的标注方式
1.EVN方式
注意:在创建按的Application的时候,不能将configLocation传入ClassPathXmlApplicationContext的构造函数,需要通过set方法传入。
因为调用ClassPathXmlApplicationContext(String... configLocations)的 构造方法会在最后默认调用refresh方法。这样就不能让activeProfile生效,需要设置activeProfile完以后,我们手动调用refresh方法。
String activeProfile = "local";
ClassPathXmlApplicationContext application = new ClassPathXmlApplicationContext();
application.getEnvironment().setActiveProfiles(activeProfile);
application.setConfigLocation("classpath:applicationContext-spring.xml");
application.refresh();
GetBeanTest getBeanTest = application.getBean(GetBeanTest.class);
getBeanTest.showBean();
上述代码一般是spring应用,而不是spring web应用。上述代码中仍存在一定的缺陷,因为activeProfile仍然是程序中的硬编码。
所以需要配合maven 多环境打包的功能。在配置文件中定义activeProfile=${activeProfile}。然后再maven 打包的过程中将环境参数存入配置文件中。
修改代码 String activeProfile = ParseProperties.getInstance().activeProfile;
这样就可以根据maven 打包指令创建不同环境下的profile。
2.JVM参数方式
在tomcat中添加JVM参数
windows : tomcat 中 catalina.bat 添加JAVA_OPS
set JAVA_OPTS="-Dspring.profiles.active=test"
linux : tomcat 中 catalina.sh添加JAVA_OPS
JAVA_OPTS="-Dspring.profiles.active=test" (不需要加set)
在编译器中添加JVM参数
用Eclipse运行项目,需要右键项目run as – run configuration – Arguments – VM arguments加入-Dspring.profiles.active="test" 运行项目即可
用IDEA运行项目,需要点击上方工具栏Run – Edit Configurations,项目第一次运行的话需要配置:点击左上角+号,选Tomcat Server – Local,在Application server配置本地的tomcat,在JRE处配置JDK,在VM options中添加-Dspring.profiles.active="test"运行项目即可
3. 在servlet上下文中进行配置(web.xml)
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>test</param-value>
</context-param>
4.使用标注 @ActiveProfiles("dev")
一般使用在Spring -Junit中
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/spring-config.xml")
@ActiveProfiles("dev")
public class MainTest {
...
}
Spring 标志@Profile
现在Spring的开发中一般通过@Component @Resource来完成,Spring对这种形式提供了@Profile标注。
注:@profile在3.2以后的版本支持方法级别和类级别,3.1版本只支持类级别。
定义了接口
public interface PageService {
public String getPageName();
}
public interface UserService {
public String getUserName();
}
定义接口实现
@Component
@Profile(value="prod")
public class UserServiceImpl implements UserService {
@Override
public String getUserName() {
return "username";
}
}
@Component
@Profile(value="test")
public class UserServiceImplTest implements UserService {
@Override
public String getUserName() {
return "username test";
}
}
@Component
public class PageServiceImpl implements PageService {
@Override
public String getPageName() {
return "this is page";
}
}
定义应用程序
@Component
public class TestController {
@Resource
private UserService userService;
@Resource
private PageService pageService;
public void printPage(){
System.out.println(pageService.getPageName());
}
public void printUser(){
System.out.println(userService.getUserName());
}
}
public class XmlBeanFactoryTest {
public static void main(String[] args) {
String activeProfile = "test";
ClassPathXmlApplicationContext application = new ClassPathXmlApplicationContext();
application.getEnvironment().setActiveProfiles(activeProfile);
application.setConfigLocation("classpath:applicationContext-spring.xml");
application.refresh();
TestController testController = application.getBean(TestController.class);
testController.printUser();
testController.printPage();
}
}
上述代码中 会根据不同activeProfile的设置对TestController中加载不同的UserService的实例,test换将会加载UserServiceImplTest,prod环境加载UserServiceImpl。
因为PageService没有设置profile,所以在任何环境下都会加载同一个实例。
@Profile不仅能对@Component标注,还能对@Configuration标注
maven 中的profile
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mdq</groupId>
<artifactId>util-support</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<profiles>
<profile>
<id>local</id>
<build>
<filters>
<filter>local.properties</filter>
</filters>
</build>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<build>
<filters>
<filter>test.properties</filter>
</filters>
</build>
</profile>
<profile>
<id>prod</id>
<build>
<filters>
<filter>prod.properties</filter>
</filters>
</build>
</profile>
</profiles>
</project>
Spring环境配置管理:profile多环境配置详解
本文详细介绍了Spring应用中如何使用profile进行多环境配置管理,包括不同环境加载不同配置文件、接口实现及日志级别。通过示例展示了如何在XML配置中定义profile,并提供了激活profile的多种方式,如ENV、JVM参数、servlet上下文和Junit标注。同时,文章也提及了Spring的@Profile注解在组件和配置类上的应用,以及maven多环境打包配置。
7474

被折叠的 条评论
为什么被折叠?



