在spring中可以使用 @Component @Configuration @Bean(实例化后返回该bean)进行类实例的自动装配。

需求:

排除指定需要自动转配的类。

说明:

1、在以上注解中 @Component @Configuration 可以通过 SpringApplication(exclude/excludeName) / @ComponentScan(excludeFilters={@Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)}) 方法进行排除制定类。

2、其中可以使用类的全路径限定名,如:xxx.xxx.xxx.RedisConfig。或者class类型,如RedisConfig.class 进行排除。

3、但如果是在类中使用@Bean声明的类实例,等不到类限定名称,也得不到该类的 class 。因此无法使用以上两种排除方式。

解决:

在使用@Bean注解自动装配类实例时,使用@Conditional进行判断,如果符合某种条件,则装配该实例。

如:

@Primary
@Bean(name="dataSource")
@Conditional(CommonDbCondition.class)
public DataSource dataSource() {
DataSource datasource = null;
try {
DataSource datasource = new DataSource();
// 省略
return datasource; } catch (Exception e) {
logger.error("db configuration initialization filter", e);
}
return datasource;
}

CommonDbCondition类如下:

public class CommonDbCondition implements Condition{
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String dbtype = context.getEnvironment().getProperty("jdbc.datasource.type");
return PubString.isNullOrSpace(dbtype)
}
}

  

因此可以自由控制该实例是否装配。

以上。

Spring 自动转配类 在类中使用@Bean 注解进行转配但是需要排除该类说明的更多相关文章

  1. spring 框架通过new Object()获取applicationContext 中的bean方案

    工作中,需要手动创建一个对象,但用到了spring容器中对象的业务逻辑,这时候就要去通过获取容器中的对象.这时候,可以通过实例化一个实现了ApplicationContextAware接口的类获取sp ...

  2. Spring中的@Bean注解

    @Bean 基础概念 @Bean:Spring的@Bean注解用于告诉方法,产生一个Bean对象,然后这个Bean对象交给Spring管理.产生这个Bean对象的方法Spring只会调用一次,随后这个 ...

  3. spring源码 — 二、从容器中获取Bean

    getBean 上一节中说明了容器的初始化,也就是把Bean的定义GenericBeanDefinition放到了容器中,但是并没有初始化这些Bean.那么Bean什么时候会初始化呢? 在程序第一个主 ...

  4. Spring中的@Bean注解、@Configuration注解、@Value

    1.首先是注册bean类:@Component.@Responsitory.@Controller.@Service.Configuration这些注解是把要实例化的对象装化成一个bean,放到Ioc ...

  5. 在Bean中获取spring 容器 并通过容器获取容器中其他bean

    spring 提供了Awear 接口去 让bean 能感受到外界的环境.Awear 接口有很多实现,常用的有 ApplicationContextAware (可以通过实现这个接口去获取Applica ...

  6. Spring 框架 详解 (四)------IOC装配Bean(注解方式)

    Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component  描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注 ...

  7. Spring源码解析 – @Configuration配置类及注解Bean的解析

    在分析Spring 容器创建过程时,我们知道容器默认会加载一些后置处理器PostPRocessor,以AnnotationConfigApplicationContext为例,在构造函数中初始化rea ...

  8. Spring自动装配歧义性笔记

    Spring自动装配歧义性笔记 如果系统中存在两个都实现了同一接口的类,Spring在进行@Autowired自动装配的时候,会选择哪一个?如下: // 一下两个类均被标记为bean @Compone ...

  9. 【String注解驱动开发】如何按照条件向Spring容器中注册bean?这次我懂了!!

    写在前面 当bean是单实例,并且没有设置懒加载时,Spring容器启动时,就会实例化bean,并将bean注册到IOC容器中,以后每次从IOC容器中获取bean时,直接返回IOC容器中的bean,不 ...

随机推荐

  1. 微信企业号开发之weixin://preInjectJSBridge/fail

    最近几天遇到个奇怪的问题,目前只有在Andriod平台上出现:weixin://preInjectJSBridge/fail 不止我一个人遇到这个问题,群里也有好几个问了这个问题.这个问题直接导致我们 ...

  2. The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build

    https://jingyan.baidu.com/album/f79b7cb34f40569144023ef9.html?picindex=1

  3. unity DoTween使用

    先说插件获取,DoTween是一个开源的插件,它的代码托管在Github上[https://github.com/Demigiant/dotween].若只是单纯项目需要是可以去AssetStore获 ...

  4. PID控制器(比例-积分-微分控制器)- II

    Table of Contents Practical Process Control Proven Methods and Best Practices for Automatic PID Cont ...

  5. 可以替代putty的ssh客户端

    1. Bitvise SSH Client http://www.putty.org/ Bitvise SSH Client is an SSH and SFTP client for Windows ...

  6. 查看linux系统硬盘目录占用大小

    http://jingyan.baidu.com/article/3aed632e198ae870108091b4.html   du -sh /* 先看看根目录下面 du -sh /usr/* du ...

  7. 《学习opencv》笔记——矩阵和图像操作——cvAnd、cvAndS、cvAvg and cvAvgSdv

    矩阵和图像的操作 (1)cvAnd函数 其结构 void cvAnd( //将src1和src2按像素点取"位与运算" const CvArr* src1,//第一个矩阵 cons ...

  8. Andorid源码 4.4 TAG

    Fetching project platform/frameworks/opt/timezonepickerremote: Counting objects: 11169, doneremote: ...

  9. pycharm怎么设置代码自动补齐

    代码补全功能在PyCharm上是一流的.默认情况下PyCharm会在你输入的时候给出建议: 由于此功能可有点耗电,你可以通过选择File→Power Save Mode来禁用它.您仍然可以通过Cont ...

  10. (原)ubuntu中使用conda安装tensorflow-gpu

    转载请注明出处: https://www.cnblogs.com/darkknightzh/p/9834567.html 参考网址: https://www.anaconda.com/blog/dev ...