一、我们自己的组件 需要使用spring ioc的底层组件的时候,
比如 ApplicationContext等 我们可以通过实现XXXAware接口来实现
@Component
public class BertComponent implements ApplicationContextAware, BeanNameAware {
private ApplicationContext applicationContext;
@Override
public void setBeanName(String name) {
System.out.println("current bean name is :【"+name+"】");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("applicationContext is :【"+applicationContext+"】");
this.applicationContext = applicationContext;
}
}
@ComponentScan
public class AwareConfigMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AwareConfigMain.class);
}
}
运行结果:
current bean name is :【bertComponent】
applicationContext is :【org.springframework.context.annotation.AnnotationConfigApplicationContext@4d405ef7, started on Wed Sep 18 09:32:55 CST 2019】
二、通过@Profile注解 来根据环境来激活标识不同的Bean @Profile标识在类上
那么只有当前环境匹配,整个配置类才会生效 @Profile标识在Bean上 ,
那么只有当前环境的Bean才会被激活,没有标志为@Profile的bean 不管在什么环境都可以被激活
在配置类中我们注入了四个Bean,分别对应开发环境、测试环境、预发布环境、生产环境。
激活切换环境的方法
方法一:通过运行时jvm参数来切换 -Dspring.profiles.active=uat
方法二:通过代码的方式来激活
db.username=dev
db.password=123456
db.jdbcUrl=jdbc:mysql://localhost:3306/dev
db.classDriver=com.mysql.jdbc.Driver
@Configuration
@PropertySource(value = {"classpath:bert.properties"})
public class ProfileConfig implements EmbeddedValueResolverAware {
@Value("${db.username}")
private String userName;
@Value("${db.password}")
private String password;
@Value("${db.jdbcUrl}")
private String jdbcUrl;
@Value("${db.classDriver}")
private String classDriver;
@Override
public void setEmbeddedValueResolver(StringValueResolver resolver) {
this.userName = resolver.resolveStringValue("${db.userName}");
this.password = resolver.resolveStringValue("${db.password}");
this.jdbcUrl = resolver.resolveStringValue("${db.jdbcUrl}");
this.classDriver = resolver.resolveStringValue("${db.classDriver}");
}
//标识开发环境才会被激活
@Bean
@Profile(value = "dev")
public BertDataSource devDb() {
return buliderDataSource(new BertDataSource());
}
//标识为测试环境才会被装配
@Bean
@Profile(value = "test")
public BertDataSource testDb() {
BertDataSource bertDataSource = buliderDataSource(new BertDataSource());
bertDataSource.setUserName("test");
return bertDataSource;
}
//标识为测试环境才会被装配
@Bean
@Profile(value = "uat")
public BertDataSource uatDb() {
BertDataSource bertDataSource = buliderDataSource(new BertDataSource());
bertDataSource.setUserName("uat");
return bertDataSource;
}
//标识生产环境才会被激活
@Bean
@Profile(value = "prod")
public BertDataSource prodDb() {
BertDataSource bertDataSource = buliderDataSource(new BertDataSource());
bertDataSource.setUserName("prod");
return bertDataSource;
}
private BertDataSource buliderDataSource(BertDataSource dataSource) {
dataSource.setUserName(userName);
dataSource.setPassword(password);
dataSource.setDriverClassName(classDriver);
dataSource.setJdbcUrl(jdbcUrl);
return dataSource;
}
}
public class ProfileConfigMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
// ctx.getEnvironment().setActiveProfiles("dev", "test", "uat", "prod");
ctx.getEnvironment().setActiveProfiles("uat", "prod");
ctx.register(ProfileConfig.class);
ctx.refresh();
for (String bean : ctx.getBeanDefinitionNames()) {
if (bean.equals("uatDb")) {
BertDataSource bd = (BertDataSource) ctx.getBean(bean);
System.out.println(bd.getUserName());
}
if (bean.equals("prodDb")) {
BertDataSource bd = (BertDataSource) ctx.getBean(bean);
System.out.println(bd.getUserName());
}
ctx.getBean(bean);
System.out.println("容器中存在的bean:" + bean);
}
}
}
运行结果:
容器中存在的bean:org.springframework.context.annotation.internalConfigurationAnnotationProcessor
容器中存在的bean:org.springframework.context.annotation.internalAutowiredAnnotationProcessor
容器中存在的bean:org.springframework.context.annotation.internalCommonAnnotationProcessor
容器中存在的bean:org.springframework.context.event.internalEventListenerProcessor
容器中存在的bean:org.springframework.context.event.internalEventListenerFactory
容器中存在的bean:profileConfig
uat
容器中存在的bean:uatDb
prod
容器中存在的bean:prodDb