一、使用@Profile注解来实现在不同环境下创建不同的Bean

  • 实现方式:将不同的Bean定义整理到对应环境的Profile中,当应用部署到不同的环境时(开发环境或者是QA环境或者是生产环境),激活对应的Profile,则相应环境的Bean就会在运行时被创建,非当前环境的Profile不会被创建,没有指定@Profile注解的Bean始终会被创建。
  • @Profile注解可以用在类级别上或者方法级别上。

举例:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.datasource.DriverManagerDataSource; import javax.sql.DataSource; @Configuration
public class DataSourceProfiles {
@Bean
@Profile("development")
public DataSource deveDataSource(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://11.11.11.11:3306/demodb");
dataSource.setUsername("root");
dataSource.setPassword("123456");
return dataSource;
} @Bean
@Profile("qa")
public DataSource qaDataSource(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://11.11.11.11:3306/demodb");
dataSource.setUsername("root");
dataSource.setPassword("123456");
return dataSource;
} @Bean
@Profile("product")
public DataSource productDataSource(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://11.11.11.11:3306/demodb");
dataSource.setUsername("root");
dataSource.setPassword("123456");
return dataSource;
} @Bean
public SomeOtherBean getBean(){
return new SomeOtherBean();
}
}

上面的例子中,“development”这个Profile激活时,会创建development环境的Bean和SomeOtherBean,其他两个Bean不会被创建。

  • 激活某个Profile的方式

Spring有两个独立的属性来确定哪个Profile被激活:spring.profile.active和spring.profile.default,如果设置了spring.profile.active属性,则它的值用来确定激活的Profile,如果没有设置spring.profile.active,但是设置了spring.profile.default,则spring.profile.default的值用来确定激活的Profile,如果spring.profile.active和spring.profile.default均没有设置,则没有激活的Profile,此时只会创建哪些没有定义在Profile中的Bean。有多种方式来定义这两个属性:

  1. 作为DispatcherServlet的初始化参数;
  2. 作为Web应用上下文的参数;
  3. 作为环境变量;
  4. 作为JNDI条目;
  5. 作为JVM的系统属性;
  6. 在集成测试时,使用@ActiveProfile注解设置;

二、条件化的装配Bean

可以设置不同的条件来控制Bean的创建:

  • 只有在某个特定环境变量设置之后,才创建某个Bean;
  • 应用的类路径下包含特定的库才创建某个Bean(这也是SpringBoot的自动化装配的实现方式,当引入了某个特定依赖时,相应的Bean就会被自动创建);
  • 只有某个Bean被创建后,才会创建另一个Bean;

举例:当环境变量中设置了magc属性时,才创建MagicExistCondition这个Bean,进一步的,只有MagicExistCondition创建后,才创建MagicBean这个Bean,具体实现方式如下:

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 MagicExistCondition implements Condition {
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata){
Environment env = context.getEnvironment();
return env.containsProperty("magic");
}
}

上述代码中MagicExistCondition实现了Condition接口,只有实现了这个接口的类,才可以作为条件类,用在@Conditional注解中。Condition这个接口很简单,如上面的例子,只需要实现matches方法即可。

接下来,判断MagicExistCondition被创建后,才创建MagicBean这个Bean:

    @Bean
@Conditional(MagicExistCondition.class)
public MagicBean magicBean(){
reutrn new MagicBean();
}

再次看看Condition接口中的matche方法,这个方法有两个参数:ConditionContext和AnnotatedTypeMetadata。

ConditionContext是一个接口:

package org.springframework.context.annotation;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader; public interface ConditionContext {
BeanDefinitionRegistry getRegistry(); ConfigurableListableBeanFactory getBeanFactory(); Environment getEnvironment(); ResourceLoader getResourceLoader(); ClassLoader getClassLoader();
}
  • getRegistry()方法返回的BeanDefinitionRegistry可以检查Bean的定义
  • getBeanFactory()方法返回的ConfigurableListableBeanFactory可以检查Bean是否存在,甚至进一步探查Bean的属性
  • getEnvironment()方法返回的Environment检查环境变量是否存在以及它的值是什么
  • getResourceLoader()方法返回的ResourceLoader用于探查所加载的资源
  • getClassLoader()方法返回的ClassLoader用于加载并检查类是否存在

AnnotatedTypeMetadata也是接口:

import java.util.Map;
import org.springframework.util.MultiValueMap; public interface AnnotatedTypeMetadata {
boolean isAnnotated(String var1); Map<String, Object> getAnnotationAttributes(String var1); Map<String, Object> getAnnotationAttributes(String var1, boolean var2); MultiValueMap<String, Object> getAllAnnotationAttributes(String var1); MultiValueMap<String, Object> getAllAnnotationAttributes(String var1, boolean var2);
}

该接口主要用来探查带有@bean注解的类上面是否还有其他的注解,并且检查那些注解的属性值。举个例子,回到@Profile这个注解,这个注解用于控制当前@bean注解的类在特定Profile激活时才被创建,那么,@Profile注解是如何实现这个功能的呢,这里就需要借助AnnotatedTypeMetadata这个接口了,从Spring 4开始,@Profile注解基于@Conditional和Condition来实现:

首先看看@Condtion注解:

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; @Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@Conditional({ProfileCondition.class})
public @interface Profile {
String[] value();
}

其中的ProfileCondition类实现了Condition接口的matches方法:

package org.springframework.context.annotation;

import java.util.Iterator;
import java.util.List;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.MultiValueMap; class ProfileCondition implements Condition {
ProfileCondition() {
} public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
if (context.getEnvironment() != null) {
MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());
if (attrs != null) {
Iterator var4 = ((List)attrs.get("value")).iterator(); Object value;
do {
if (!var4.hasNext()) {
return false;
} value = var4.next();
} while(!context.getEnvironment().acceptsProfiles((String[])((String[])value))); return true;
}
} return true;
}
}

可以看到:matches方法中,首先给metadata.getAllAnnotationAttributes()方法传递Profile.class.getName()这个Profile Bean的名字,得到所有注解的属性,并逐个遍历判断,借助acceptsProfiles方法来见擦好该Profile是否被激活!!!

三、处理自动装配的歧义性

当一个接口有多个实现类时,如果某个类需要注入接口类,此时会抛出NoUniqueBeanDefinitionException异常,解决方法是:

  1. 使用@Primary标注首选的Bean
  2. 使用@Qualifier注解@Qualifier(“specialBean”)限定符
  3. 创建自定义的限定符注解

Spring装配Bean的一些高级技巧的更多相关文章

  1. Spring 装配Bean

    Spring 装配Bean 装配解释: 创建应用对象之间协作关系的的行为通常称为装配(wiring),这也是依赖注入的本质 依赖注入是Spring的基础要素 一 : 使用spring装配Bean基础介 ...

  2. Spring装配bean

    Spring配置的可选方案 Spring提供了如下三种装配机制: (1)在XML中显式配置 (2)在Java中显式配置 (3)隐式的bean发现机制和自动装配 Spring有多种方式可以装配bean, ...

  3. Spring装配Bean之XML装配bean

    在Spring刚出现的时候,XML是描述配置的主要方式,在Spring的名义下,我们创建了无数行XML代码.在一定程度上,Spring成为了XML的同义词. 现在随着强大的自动化配置和Java代码的配 ...

  4. Spring装配Bean的过程补充

    对上一篇的<Spring装配Bean的过程>的过程说一下,不然真产生了误区. 误区在哪里呢?那就是spring bean的作用域问题. 说哈常用的两种作用域:默认是scope = sing ...

  5. Spring装配Bean的过程

    首先说一个概念:“懒加载” 懒加载:就是我们在spring容器启动的是先不把所有的bean都加载到spring的容器中去,而是在当需要用的时候,才把这个对象实例化到容器中. spring配置文件中be ...

  6. Spring 装配Bean入门级

    装配解释: 创建应用对象之间协作关系的的行为通常称为装配(wiring),这也是依赖注入的本质 依赖注入是Spring的基础要素 一 : 使用spring装配Bean基础介绍 1 :声明Bean  B ...

  7. 【转】spring 装配Bean中构造参数的注入

    转载自:http://www.bianceng.cn/Programming/Java/201307/37027.htm spring 装配Bean中构造参数的注入 spring装配bean中还有一种 ...

  8. Spring装配Bean之组件扫描和自动装配

    Spring从两个角度来实现自动化装配: 组件扫描:Spring会自动发现应用上下文中所创建的bean. 自动装配:Spring自动满足bean之间的依赖. 案例:音响系统的组件.首先为CD创建Com ...

  9. Spring装配Bean之Java代码装配bean

    尽管通过组件扫描和自动装配实现Spring的自动化配置很方便也推荐,但是有时候自动配置的方式实现不了,就需要明确显示的配置Spring.比如说,想要将第三方库中的组件装配到自己的应用中,这样的情况下, ...

随机推荐

  1. 第1节 HUE:14、15、16、hue与hdfs、yarn集群、hive、impala、mysql的整合

    3.hue与其他框架的集成 3.1.hue与hadoop的HDFS以及yarn集成 第一步:更改所有hadoop节点的core-site.xml配置 记得更改完core-site.xml之后一定要重启 ...

  2. 冰蝎动态二进制加密WebShell特征分析

    概述 冰蝎一款新型加密网站管理客户端,在实际的渗透测试过程中有非常不错的效果,能绕过目前市场上的大部分WAF.探针设备.本文将通过在虚拟环境中使用冰蝎,通过wireshark抓取冰蝎通信流量,结合平时 ...

  3. bug-解决微信页面input键盘不回弹问题

    pageReturn () { this.$refs.phoneValue.blur(); this.$refs.verifyCode.blur(); setTimeout(() => { wi ...

  4. C# log4net相关配置说明

    添加相关文件到工程 链接: https://pan.baidu.com/s/1o83Juo6 密码: inkg 下载附件, 把里的log4net.dll 和 log4net.config 复制到工程目 ...

  5. 开源DDD设计模式框架YMNNetCoreFrameWork第一篇

    DDD设计模式:仓储.领域模型.应用层.聚合根.事件总线,以业务模型驱动设计,从数据模型驱动脱离,不用关心数据库设计,开发效率更高 DDD领域驱动设计模型概念不再讲解,直接上技术 框架搭建: 如图所示 ...

  6. UCENTER同步登录工作原理和配置要点

    ucenter的同步登录原理: 1)Ucenter是和uc_client同步的.每个PHP应用,加入了UCENTER后,都会在主目录下有个UC_CLIENT目录.这个目录里,都有一个client.PH ...

  7. spark shuffle发展历程

    spark2.3 shuffle manager,默认sortShuffleManager // Let the user specify short names for shuffle manage ...

  8. openalyser6学习

    1.安装nvm.nvm下载地址:https://github.com/coreybutler/nvm-windows/releases使用nvm-setup安装和下载nodejs:https://ww ...

  9. php5.3不支持 ereg、ereg_replace等函数问题

    在php5.3环境下运行oscommerce,常常会出现Deprecated: Function ereg() is deprecated in...和Deprecated: Function ere ...

  10. Day 30:HTML和CSS在Java项目中常用语法

    framSet例子,其中的页面链接地址视情况而定,应为我还不知怎么弄当前文件下呢,例子主要在说明该标签如何使用 <!DOCTYPE html PUBLIC "-//W3C//DTD X ...