@Profile註解:是spring爲咱們提供的能夠根據當前環境,動態的激活和切換一系列組件的功能。java
@Conditional:按照必定的條件進行判斷,知足條件則給容器中注入beanmysql
@Profile代碼示例:spring
package common.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.EmbeddedValueResolverAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.context.annotation.PropertySource; import org.springframework.util.StringValueResolver; import javax.sql.DataSource; @Configuration @PropertySource("classpath:/dbconfig.properties") public class MainConfigProfile implements EmbeddedValueResolverAware { @Value("${db.username}") private String userName; private StringValueResolver valueResolver; private String driver; @Profile("dev") @Bean("devDataSource") public DataSource dataSourceDev(@Value("${db.password}")String pwd){ DruidDataSource dataSource = new DruidDataSource(); dataSource.setUsername(userName); dataSource.setPassword(pwd); dataSource.setUrl("dbc:mysql://localhost:3306/dev"); dataSource.setDriverClassName(driver); return dataSource; } @Profile("test") @Bean("testDataSource") public DataSource dataSourceTest(@Value("${db.password}")String pwd){ DruidDataSource dataSource = new DruidDataSource(); dataSource.setUsername(userName); dataSource.setPassword(pwd); dataSource.setUrl("dbc:mysql://localhost:3306/test"); dataSource.setDriverClassName(driver); return dataSource; } @Profile("prod") @Bean("prodDataSource") public DataSource dataSourceProd(@Value("${db.password}")String pwd){ DruidDataSource dataSource = new DruidDataSource(); dataSource.setUsername(userName); dataSource.setPassword(pwd); dataSource.setUrl("dbc:mysql://localhost:3306/prod"); dataSource.setDriverClassName(driver); return dataSource; } @Override public void setEmbeddedValueResolver(StringValueResolver resolver) { this.valueResolver = resolver; driver = valueResolver.resolveStringValue("${db.driverClass}"); } }
測試類:sql
package common; import common.config.MainConfigProfile; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class ProFileTest { //1.使用命令行動態參數: 在虛擬機參數位置加載 -Dspring.profiles.active=test //二、代碼的方式激活某種環境; @Test public void test(){ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfigProfile.class); // AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); // context.getEnvironment().setActiveProfiles("dev"); // context.register(MainConfigProfile.class); // context.refresh(); String[] names = context.getBeanDefinitionNames(); for (String s: names){ System.out.println(s); } } }
測試結果的輸出能夠看出使用了@Profile註解後,能夠指定組件在哪一個環境的狀況下才能被註冊到容器中,不指定,任何環境都能註冊這個組件。ide
@Condition示例測試
配置類:ui
package common.config; import common.bean.Red; import common.bean.Yellow; import condition.WindowsCondition; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; /** * @Conditional註解加在配置類上,不知足條件時,配置類都不會注入到容器中 */ @Configuration public class MainConfigCondition { @Bean public Red red(){ return new Red(); } @Conditional(WindowsCondition.class) @Bean public Yellow yellow(){ return new Yellow(); } }
package condition; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.env.Environment; import org.springframework.core.type.AnnotatedTypeMetadata; public class WindowsCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { Environment environment = context.getEnvironment(); String property = environment.getProperty("os.name"); if(property.contains("Windows")){ return false; } return true; } }
測試類:this
package common; import common.config.MainConfigCondition; import common.config.MainConfigProfile; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class ProFileTest { @Test public void test(){ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfigCondition.class); String[] names = context.getBeanDefinitionNames(); for (String s: names){ System.out.println(s); } } }
測試結果:spa
八月 29, 2018 4:11:43 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2a3046da: startup date [Wed Aug 29 16:11:43 CST 2018]; root of context hierarchy org.springframework.context.annotation.internalConfigurationAnnotationProcessor org.springframework.context.annotation.internalAutowiredAnnotationProcessor org.springframework.context.annotation.internalRequiredAnnotationProcessor org.springframework.context.annotation.internalCommonAnnotationProcessor org.springframework.context.event.internalEventListenerProcessor org.springframework.context.event.internalEventListenerFactory mainConfigCondition red