1、新建一个maven项目,导入如下依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.2.4.RELEASE</spring.version>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
2、建立如下目录
3、配置dev.properties中的内容为name=dev
配置pro.properties中的内容为name=pro
配置spring.xml中的内容为
<?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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<context:component-scan base-package="service"/>
<!-- 定义开发的profile -->
<beans profile="development">
<!-- 加载开发使用的配置文件 -->
<util:properties id="config" location="classpath:dev.properties"/>
</beans>
<!-- 定义生产使用的profile -->
<beans profile="produce">
<!-- 加载生产使用的配置文件 -->
<util:properties id="config" location="classpath:pro.properties"/>
</beans>
</beans>
4、写HelloService接口和实现类
public interface HelloService {
String hello();
}
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import service.HelloService;
@Service(value = "helloService")
public class HelloServiceImpl implements HelloService {
@Value("#{config.name}")
private String name;
@Override
public String hello() {
return "hello:"+name;
}
}
5、接下来,就可以测试不同profile下的内容了
编辑TestDemo
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import service.HelloService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring.xml")
@ActiveProfiles(value = "development")
public class TestDemo {
@Autowired
public HelloService hs;
@Test
public void test1(){
String val = hs.hello();
System.out.println(val);
}
}
如果@ActiveProfiles(value = “development”)这里激活的是development,则输出的结果是hello:dev
如果激活的是produce配置,则输出的结果是hello:pro
6、激活的方式不止这一种,具体有如下方式
作为SpringMVC中的DispatcherServlet的初始化参数
作为Web 应用上下文中的初始化参数
作为JNDI的入口
作为环境变量
作为虚拟机的系统参数
使用@AtivceProfile来进行激活
配置多个profile的时候,如果要打包项目,可以用mvn -Ppro来选择。如
mvn install -DskipTests -Ppro。表示跳过检查,用pro配置来打包项目