@Profile:指定组件在哪个环境下才能被注册到容器中,不指定,任何环境下都能注册这个组件
- 加了环境标识的bean,只有这个环境被激活的时候才能注册到容器中,默认是default环境
- 卸载配置类上,只有是指定的环境的时候,整个配置类里面的所有的配置才会生效,有点类似@Conditional
- 没有标注环境标识的bean在任何环境下都是加载的
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2019-2020. All rights reserved.
*/
package com.cjian.config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
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 java.beans.PropertyVetoException;
import javax.sql.DataSource;
/**
* @description:
* @author: cWX969834
* @time: 2020/10/28 9:32
*/
@PropertySource("classpath:/dbConfig.properties")
@Configuration //告诉spring这是一个配置类
public class MainConfigProfile implements EmbeddedValueResolverAware {
@Value("${db.user}")
private String user;
private String driverClass;
@Override
public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) {
driverClass = stringValueResolver.resolveStringValue("${db.driverClass}");
}
@Profile("dev")
@Bean("dev")
public DataSource dataSourceDev(@Value("${db.password}") String password) throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(user);
dataSource.setPassword(password);
dataSource.setDriverClass(driverClass);
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/dev");
return dataSource;
}
@Profile("test")
@Bean("test")
public DataSource dataSourceTest(@Value("${db.password}") String password) throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setUser(user);
dataSource.setPassword(password);
dataSource.setDriverClass(driverClass);
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
return dataSource;
}
}
dbConfig.properties:
db.user=root
db.password=123456
db.driverClass=jdbc.mysql.jdbc.Driver
测试类:
package com.cjian;
import com.cjian.config.MainConfigProfile;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @description:
* @author: cWX969834
* @time: 2020/11/3 11:13
*/
public class TestProfile {
static AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(
MainConfigProfile.class);
public static void main(String[] args) {
String[] beanDefinitionNames = annotationConfigApplicationContext.getBeanDefinitionNames();
/* annotationConfigApplicationContext.getEnvironment().setActiveProfiles("dev");
annotationConfigApplicationContext.register(MainConfigProfile.class);
annotationConfigApplicationContext.refresh();*/
for(String name:beanDefinitionNames){
System.out.println(name);
}
}
}
第一种启用方式为:通过jvm参数
输出:
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfigProfile
dev
第二种方式为:通过代码
/**
* @description:
* @author: cWX969834
* @time: 2020/11/3 11:13
*/
public class TestProfile {
static AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
public static void main(String[] args) {
annotationConfigApplicationContext.getEnvironment().setActiveProfiles("test");
annotationConfigApplicationContext.register(MainConfigProfile.class);
annotationConfigApplicationContext.refresh();
String[] beanDefinitionNames = annotationConfigApplicationContext.getBeanDefinitionNames();
for (String name : beanDefinitionNames) {
System.out.println(name);
}
}
}
输出:
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfigProfile
test