目录
1. 说明
Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能,该功能就是通过@Profile来实现;
@Profile的定义信息如下
package org.springframework.context.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.ConfigurableEnvironment;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(ProfileCondition.class)
public @interface Profile {
/**
* The set of profiles for which the annotated component should be registered.
* 定义组件的环境标记
*/
String[] value();
}
2. 注解使用
根据不同的环境引入不同的数据源
主配置类如下:
package com.yibai.spring.annotation.main.config;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Profile;
import com.alibaba.druid.pool.DruidDataSource;
public class MainConfigForProfile {
private String username;
private String password;
private String driverClass = org.postgresql.Driver.class.toString();
@Bean
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/default");
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClass);
return dataSource;
}
@Profile("default")
@Bean
public DataSource dataSourceDefault() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/default");
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClass);
return dataSource;
}
@Profile("test")
@Bean
public DataSource dataSourceTest() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/test");
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClass);
return dataSource;
}
@Profile("dev")
@Bean
public DataSource dataSourceDev() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/dev");
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClass);
return dataSource;
}
@Profile("prod")
@Bean
public DataSource dataSourceProd() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/prod");
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setDriverClassName(driverClass);
return dataSource;
}
}
启动类:
package com.yibai.spring.annotation.main;
import java.util.Arrays;
import javax.sql.DataSource;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.yibai.spring.annotation.main.config.MainConfigForProfile;
/**
* Hello world!
*
*/
public class MainClass {
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(
MainConfigForProfile.class);
String[] datasources = applicationContext.getBeanNamesForType(DataSource.class);
System.out.println(Arrays.asList(datasources));
applicationContext.close();
}
}
运行打印结果是: [dataSourceDefault, dataSourceTest]
以上是还没有指定环境标记的情况下的运行结果,说明被@Profile标记的导入的组件,只有在与环境匹配的才会被导入,默认是导入@Profile("default")标记的组件和没有被@Profile标记的组件;
3. 设置环境
- 3.1 运行的时候通过添加虚拟机参数的方式设置需要激活的环境
运行打印结果: [dataSource, dataSourceDev]
- 3.2 代码方式设置需要激活的环境
package com.yibai.spring.annotation.main;
import java.util.Arrays;
import javax.sql.DataSource;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.yibai.spring.annotation.main.config.MainConfigForProfile;
/**
* Hello world!
*
*/
public class MainClass {
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
//设置需要激活的环境
applicationContext.getEnvironment().setActiveProfiles("test", "dev");
//注册主配置类
applicationContext.register(MainConfigForProfile.class);
//刷新容器
applicationContext.refresh();
String[] datasources = applicationContext.getBeanNamesForType(DataSource.class);
System.out.println(Arrays.asList(datasources));
applicationContext.close();
}
}
运行打印结果: [dataSource, dataSourceTest, dataSourceDev]
4. 注解说明
通过注解的定义信息看,@Profile注解可以定义在方法上,也可以定义在类上,当@Profile定义在类上的时候,当与设置的环境匹配时,整个配置类里面的组件才会被加载;