Spring自动装配注解之@Profile

目录

1. 说明

2. 注解使用

3. 设置环境

4. 注解说明


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定义在类上的时候,当与设置的环境匹配时,整个配置类里面的组件才会被加载;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值