譯文連接:http://websystique.com/spring/spring-profile-example/java
本文將探索Spring中的@Profile註解,能夠實現不一樣環境(開發、測試、部署等)使用不一樣的配置。一樣,除了使用註解也會給出基於XML配置的示例做爲對比。
mysql
假設你有一個應用涉及數據庫交互,你可能但願在開發環境上使用mysql數據庫,在生產環境上使用oracle數據庫,那麼使用Spring的Profiles
,能夠輕鬆達到這個目的,接下來咱們將給出一個實例詳細介紹這種狀況。web
<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配置類是指用@Configuration
註解標註的類,這些類包含了用@Bean
標註的方法。這些被@Bean
標註的方法能夠生成bean並交由spring容器管理。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。sql
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
標註。apache
被@Profile
標註的組件只有當指定profile值匹配時才生效。oracle
能夠經過如下方式設置profile值:app
一、設置spring.profiles.active
屬性(經過JVM參數、環境變量或者web.xml中的Servlet context參數)maven
二、ApplicationContext.getEnvironment().setActiveProfiles(「ProfileName」)
根據你的實際環境設置profile值,而後被profile標註(並且value=設置值)的bean纔會被註冊到spring容器。
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
替換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(); } }
運行程序,會獲得相同的結果。
http://websystique.com/?smd_process_download=1&download_id=799