首先说一下为什么要使用这个@profile注解。@profile注解是spring提供的一个用来标明当前运行环境的注解。我们正常开发的过程中经常遇到的问题是,开发环境是一套环境,qa测试是一套环境,线上部署又是一套环境。这样从开发到测试再到部署,会对程序中的配置修改多次,尤其是从qa到上线这个环节,让qa的也不敢保证改了哪个配置之后能不能在线上运行。
为了解决上面的问题,我们一般会使用一种方法,就是配置文件,然后通过不同的环境读取不同的配置文件,从而在不同的场景中跑我们的程序。
那么,spring中的@profile注解的作用就体现在这里。在spring使用DI来依赖注入的时候,能够根据当前制定的运行环境来注入相应的bean。最常见的就是使用不同的DataSource了。
涉及技术及开发工具
- Spring 4.0.6.RELEASE
- Maven 3
- JDK 1.6
- Eclipse JUNO Service Release 2
工程目录结构
步骤一:往pom.xml中添加依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.websystique.spring</groupId> <artifactId>Spring4ProfilesExample</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>Spring4ProfilesExample</name> <properties> <springframework.version>4.0.6.RELEASE</springframework.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${springframework.version}</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </pluginManagement> </build> </project>
步骤二:创建Spring配置类
Spring配置类是指用@Configuration
注解标注的类,这些类包含了用@Bean
标注的方法。这些被@Bean
标注的方法可以生成bean并交由spring容器管理。
package com.websystique.spring.configuration; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan(basePackages = "com.websystique.spring") public class AppConfig { @Autowired public DataSource dataSource; }
以上配置只有一个属性被自动注入,接下来我们将展示这个dataSource属性可以根据不同的环境(开发环境或生产环境)注入不同的bean。
package com.websystique.spring.configuration;
import javax.sql.DataSource;
public interface DatabaseConfig {
DataSource createDataSource();
}
一个简单的接口,可以被所有可能的环境配置实现
package com.websystique.spring.configuration;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
@Profile("Development")
@Configuration
public class DevDatabaseConfig implements DatabaseConfig {
@Override
@Bean
public DataSource createDataSource() {
System.out.println("Creating DEV database");
DriverManagerDataSource dataSource = new DriverManagerDataSource();
/*
* Set MySQL specific properties for Development Environment
*/
return dataSource;
}
}
package com.websystique.spring.configuration;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
@Profile("Production")
@Configuration
public class ProductionDatabaseConfig implements DatabaseConfig {
@Override
@Bean
public DataSource createDataSource() {
System.out.println("Creating Production database");
DriverManagerDataSource dataSource = new DriverManagerDataSource();
/*
* Set ORACLE specific properties for Production environment
*/
return dataSource;
}
}
以上两个配置类都实现了DatabaseConfig接口,特殊的地方在于它们都用@Profile
标注。
被@Profile
标注的组件只有当指定profile值匹配时才生效。
可以通过以下方式设置profile值:
1、设置spring.profiles.active
属性(通过JVM参数、环境变量或者web.xml中的Servlet context参数)
2、ApplicationContext.getEnvironment().setActiveProfiles(“ProfileName”)
根据你的实际环境设置profile值,然后被profile标注(而且value=设置值)的bean才会被注册到spring容器。
步骤三:运行main方法测试
package com.websystique.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class AppMain { public static void main(String args[]){ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); //Sets the active profiles context.getEnvironment().setActiveProfiles("Development"); //Scans the mentioned package[s] and register all the @Component available to Spring context.scan("com.websystique.spring"); context.refresh(); context.close(); } }
注意以上代码,context.scan(
"com.websystique.spring"
)
扫描到该包并开始注册所有被@Component标注的bean时,如果同时遇到被@Profile注解标注的bean时,会与profile值做比较,profile值匹配则注册到spring容器,否则直接跳过。
在我们这个例子中,DevDatabaseConfig会被注册到Spring容器中。
运行以上程序,结果如下:
Creating DEV database
附:基于XML的配置
替换DevelopmentDatabaseConfig配置为dev-config-context.xml (src/main/resources/dev-config-context.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/websystique" /> <property name="username" value="myuser" /> <property name="password" value="mypassword" /> </bean> </beans>
替换ProductionDatabaseConfig配置为prod-config-context.xml (src/main/resources/prod-config-context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value=" oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@PRODHOST:PRODPORT/websystique" />
<property name="username" value="myproduser" />
<property name="password" value="myprodpassword" />
</bean>
</beans>
替换AppConfig配置为app-config.xml (src/main/resources/app-config.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.websystique.spring"/>
<beans profile="Development">
<import resource="dev-config-context.xml"/>
</beans>
<beans profile="Production">
<import resource="prod-config-context.xml"/>
</beans>
</beans>
根据实际的profile配置,相应的config-context.xml文件会被加载,其它的会被忽略。
最后,main方法如下:
package com.websystique.spring;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppMain {
public static void main(String args[]){
AbstractApplicationContext context = new ClassPathXmlApplicationContext("app-config.xml");
//Sets the active profiles
context.getEnvironment().setActiveProfiles("Development");
/*
* Perform any logic here
*/
context.close();
}
}
运行程序,会得到相同的结果。
首先是新建maven工程
mvn archetype:generate -DarchetypeCatalog=internal
下面是pom文件:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springframework.version>4.3.7.RELEASE</springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springframework.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.xueyou.demo</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id><!-- this is used for inheritance merges -->
<phase>package</phase><!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
整体看一下工程中的类和接口:
首先是Person类中有一个speak的方法,这个方法是MoveFactor这个借口提供的。Chinese、English和German都实现了这个接口。但是这三个类的@profile中的值是不同的。通过SpringTest中分配不同的activeprofile就能够实现调用不同的speak方法。
下面看代码:
MoveFactor.interface
package com.xueyou.demo;
public interface MoveFactor {
void speak();
}
Person.java
package com.xueyou.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Person {
@Autowired
private MoveFactor moveFactor;
public void speak(){
moveFactor.speak();
}
}
Chinese.java
package com.xueyou.demo;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
@Configuration
@Profile(value = "dev")
@Component
public class Chinese implements MoveFactor {
@Override
public void speak() {
System.out.println("我是中国人");
}
}
English.java
package com.xueyou.demo;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
@Component
@Profile("qa")
public class English implements MoveFactor{
@Override
public void speak() {
System.out.println("i am an English");
}
}
German.java
package com.xueyou.demo;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
@Component
@Profile("prod")
public class German implements MoveFactor{
@Override
public void speak() {
System.out.println("i am a German");
}
}
使用springtest进行测试
package com.xueyou.demo;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = App.class)
@ActiveProfiles("dev")
public class SpringTest {
@Autowired
Person p;
@Test
public void testProfile(){
p.speak();
}
}
运行结果:
当修改@ActiveProfile中的值时,输出的内容也会随之改变。
如果使用的是main函数进行真正的开发、测试和上线时,我们需要设置一下运行参数:
-D 后面加上需要设置的spring的属性,就能够在main函数中使用了。
App.java
package com.xueyou.demo;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Hello world!
// */
@Configuration
@ComponentScan(basePackages = {"com.xueyou.demo"})
public class App {
public static void main(String[] args) {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(com.xueyou.demo.App.class);
Person p = context.getBean(Person.class);
p.speak();
}
}
运行结果:
如果需要得到当前的activeprofile可以通过ConfigurableApplicationContext的实例来的到。
最后提一下,如果是在web的后台项目中如何进行设置。这个时候我们通过xml的方式进行设置:
-
<context-param>
-
<param-name>spring.profiles.active</param-name>
-
<param-value>DOUBLEUPMINT</param-value>
-
</context-param>