ComponetScan 定义扫描规则

  1. value:指定要扫描的包
  2. excludeFilters=Filter[] 指定扫描的时候按照什么规则排除哪些组件。
  3. includeFilters=Filter[] 指定扫描的时候只需要包含哪些组件。(注意的是包扫描默认的是扫描所有的,也就是use-default-filters=”true”,所以需要设置为false。 这个和配置文件一样)
  • FilterType.ANNOTATION :按照注解的方式
  • FilterType.ASSIGNABLE_TYPE:按照给定的类型
  • FilterType.CUSTOM:使用自定义规则。 使用这个需要是TypeFilter的实现类
  • FilterType.CUSTOM使用 案例如下:
  • FilterType.ASPECTJ:使用ASPECTJ表达式(基本用不到)
  • FilterType.REGEX :使用正则表达式

例子

@Configuration
@ComponentScan(value = "feilong.example", useDefaultFilters = true, excludeFilters = {
//过滤掉具体的注解下所有类
@ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class}),
// //过滤掉具体的类
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {ConfigTest2.class})
},
//includeFilter里面的Filter是并集
includeFilters = {
@ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class})
}
)
public class MainConfig { @Bean
public Person person() {
return new Person("feilong", 20);
}
}

类MyTypeFilter  是自定义Filter, 注意: 之前被过滤掉的类如果再自定义Filter中符合要求, 也会被重新放到IOC容器中.

public class MyTypeFilter implements TypeFilter {
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { //获取注解metaData
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
Resource resource = metadataReader.getResource(); //获取当前类信息
ClassMetadata classMetadata = metadataReader.getClassMetadata();
String strClassName = classMetadata.getClassName(); System.out.println("通过自定义规则 ==>>>> " + strClassName); //返回true的类会被注册进IOC
return strClassName.contains("Controller"); }
}

@Import[快速的给容器中导入一个组件]

@Import的三种用法:

(1)、 @Import(要导入容器中的组件);容器中就会自动的注册这个组件,id默认是全类名
(2)、 ImportSelector :返回需要的组件的全类名的数组;
(3)、 ImportBeanDefinitionRegistrar : 手动注册bean到容器中

1. 组件注册-@Import-给容器中快速导入一个组件

定义两个POJO类

public class Color {
} public class Red {
}

将此类对应的Bean导入IOC容器

@Configuration
@ComponentScan(value = "feilong.example")
@Import({Color.class, Red.class})
public class MainConfig {
}

2. 使用ImportSelector

创建自定义MyImportSelector , 在方法selectImports 中返回要注入的类

public class MyImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"feilong.example.springbootlearn.Color","feilong.example.springbootlearn.Red"};
}
}

将自定义ImportSeletor加入配置文件

@Configuration
@ComponentScan(value = "feilong.example")
@Import(MyImportSelector.class)
public class MainConfig {
}

3. 使用 ImportBeanDefinitionRegistrar接口,

public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
registry.registerBeanDefinition("green", new RootBeanDefinition(Green.class));
}
}

将自定义ImportSeletor加入配置文件

@Configuration
@ComponentScan(value = "feilong.example")
@Import({MyImportSelector.class,MyImportBeanDefinitionRegistrar.class})
public class MainConfig {
}

执行结果如下

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
mainConfig
feilong.example.springbootlearn.Color
feilong.example.springbootlearn.Red
person
person3
green

使用FactoryBean注解

创建自定义的一个FactoryBean

public class MyColorFactoryBean implements FactoryBean<Color> {
@Override
public Color getObject() throws Exception {
System.out.println("Creating Color Object by MyColorFactoryBean...");
return new Color();
} //控制是否为单例
// true:表示的就是一个单实例,在容器中保存一份
// false:多实例,每次获取都会创建一个新的bean
@Override
public boolean isSingleton() {
return true;
} @Override
public Class<?> getObjectType() {
return Color.class;
}
}

配置为Bean

@Configuration
@ComponentScan(value = "feilong.example")
public class MainConfig { @Bean
public Person person() {
return new Person("feilong", 20);
} @Bean("person3")
public Person personCondition() {
return new Person("feilong", 30);
} @Bean
public MyColorFactoryBean colorFactoryBean(){
return new MyColorFactoryBean();
}
}

测试

public class MainTest {
public static void main(String[] args) throws InterruptedException { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class); String[] strName = ((AnnotationConfigApplicationContext) applicationContext).getBeanDefinitionNames(); for (String s : strName) {
System.out.println(s);
}     //此处获取的是getObject对象, 即Color对象
Color c = (Color) applicationContext.getBean("colorFactoryBean");
     //此处获取的是BeanFactory工厂对象本身
MyColorFactoryBean myColorFactoryBean =(MyColorFactoryBean) applicationContext.getBean("&colorFactoryBean");
System.out.println(c);
System.out.println(myColorFactoryBean);
}
} /*
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
mainConfig
person
person3
colorFactoryBean
Creating Color Object by MyColorFactoryBean...
feilong.example.springbootlearn.Color@2a32de6c
feilong.example.springbootlearn.MyColorFactoryBean@7692d9cc
*/

注意, 使用FactoryBean(工厂Bean) 默认获取到的是工厂Bean调用方法getObject()获取到的对象, 如果要想获取到原来的Bean工厂本身,需要加上&,


Bean生命周期

* 容器管理bean的生命周期:
  * 我们可以自定义初始化方法和销毁的方法:容器在bean进行到当前的生命周期的时候,来调用我们自定义的初始化方法和销毁方法
* 构造(对象创建):
  * 单实例:在容器启动的时候创建对象
  * 多实例:在每次获取的时候来创建对象
* 初始化方法:
  * 对象创建完成,并赋值好,调用初始化方法
* 销毁方法:
  * 单实例的bean:在容器关闭的时候进行销毁
  * 多实例的bean:容器不会管理这个bean,容器不会调用销毁的方法

生命周期-InitializingBeanDisposableBean

定义测试类:

public class CarLifeCycle implements InitializingBean {
public CarLifeCycle() {
System.out.println("CarLifeCycle constructor...");
} public void init() {
System.out.println("CarLifeCycle...init...");
} public void destroy() {
System.out.println("CarLifeCycle...destroy...");
} @Override
public void afterPropertiesSet() throws Exception {
System.out.println("接口 InitializingBean # afterPropertiesSet ");
}
}

定义MyBeanPostProcessor

public class MyBeanPostProcessor implements BeanPostProcessor {

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println(beanName + " => MyBeanPostProcessor .. before");
return bean;
} public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println(beanName + " => MyBeanPostProcessor .. after");
return bean;
}
}

注入IOC

@Configuration
@ComponentScan(value = "feilong.example")
public class MainConfig { @Bean(initMethod = "init")
public CarLifeCycle carLifeCycle(){
return new CarLifeCycle();
}
}

执行程序

CarLifeCycle constructor...
carLifeCycle => MyBeanPostProcessor .. before
接口 InitializingBean # afterPropertiesSet
CarLifeCycle...init...
carLifeCycle => MyBeanPostProcessor .. after

@ComponentScan比较的更多相关文章

  1. spring源码分析之<context:component-scan/>vs<annotation-config/>

    1.<context:annotation-config/> xsd中说明: <xsd:element name="annotation-config"> ...

  2. context:component-scan" 的前缀 "context" 未绑定。

    SpElUtilTest.testSpELLiteralExpressiontestSpELLiteralExpression(cn.zr.spring.spel.SpElUtilTest)org.s ...

  3. 为什么applicationContext.xml和spring-servlet.xml中都有注解过滤<context:component-scan base-package="myproject"> 和<context:component-scan base-package="myproject.controller" />

    在刚学习SpringMVC框架整合时,你也许会产生疑问为什么Spring.xml和SpringMVC.xml中都有注解过滤. <context:component-scan base-packa ...

  4. context:component-scan标签的use-default-filters属性的作用以及原理分析

    一.背景 我们在Spring+SpringMVC+Mybatis的集成开发中,经常会遇到事务配置不起作用等问题,那么本文就来分析下出现这种问题可能的原因以及解决方式. 二.分析及原理窥探 1.项目结构 ...

  5. spring <context:component-scan>使用说明(转)

    在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类 ...

  6. [转载]Difference between <context:annotation-config> vs <context:component-scan>

    在国外看到详细的说明一篇,非常浅显透彻.转给国内的筒子们:-) 原文标题: Spring中的<context:annotation-config>与<context:componen ...

  7. <context:component-scan>使用说明

    Spring组件扫描<context:component-scan/>使用详解 在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫 ...

  8. 关于context:component-scan配置中use-default-filters参数的作用

    参考了多篇文章都说明了use-default-filters参数的基本用途,但有些主要点没有说到,这里补充记录下: <context:component-scan base-package=&q ...

  9. annotation-config vs component-scan – Spring Core--转

    原文地址:http://techidiocy.com/annotation-config-vs-component-scan-spring-core/ <context:annotation-c ...

  10. context:component-scan扫描使用的use-default-filters

    如下方式可以成功扫描到@Controller注解的Bean,不会扫描@Service/@Repository的Bean. <context:component-scan base-package ...

随机推荐

  1. postman 基本应用

    前言 进行post高级应用的一个整理. 正文 批量测试和简单自动化测试 在点击collects的列表中,会弹出下面这个选项. 上面有3个按钮,分别是分享.运行.展示在网页中. 那么就看下这个运行吧. ...

  2. 升级的华为云“GaussDB”还能战否?

    摘要:芯片.操作系统.数据库是现代信息技术领域的三大核心基础,做数据库,不仅需要技术和投入,对华为这种做通讯起家的企业,更需要的是一种并非玩票性质的态度. GaussDB,不仅蕴含着华为对数学和科学的 ...

  3. python爬虫-爬取百度图片

    python爬虫-爬取百度图片(转) #!/usr/bin/python# coding=utf-8# 作者 :Y0010026# 创建时间 :2018/12/16 16:16# 文件 :spider ...

  4. c#值类型引用类型第一章

    概要 本篇文章主要简单扼要的讲述值类型和引用类型更进阶的理解和使用.如果希望更多的了解和技术讨论请记得看文章末尾,望各位看官多多支持多多关注,关注和支持是我更新文章的最大动力.在这里谢谢大家.温馨提示 ...

  5. C++ Templates (2.2 使用Stack类模板 Use of Class Template Stack )

    返回完整目录 目录 2.2 使用Stack类模板 Use of Class Template Stack 2.2 使用Stack类模板 Use of Class Template Stack 在C++ ...

  6. 用Python发一封图文并茂的邮件

    最近使用了不少通讯工具的接口, 比如企业微信机器人,钉钉,微信公众号的接口(未认证的订阅公众号),相对于邮件来说,它们的表现形式太弱.比如没有更丰富的版本方式.当然了,并不是说表现形式越棒就是约好的通 ...

  7. Sql 注入----学习笔记

    先了解下CRLF,CRLF常用在分隔符之间,CR是carriage retum(ASCII 13,\r) LF是Line Feed (ASCII 10,\n), \r\n这两个字符类似于回车是用于换行 ...

  8. PHP to .NET Compiler

    官网 https://github.com/peachpiecompiler/peachpie 非官方简介 https://blog.csdn.net/sD7O95O/article/details/ ...

  9. 【pytest】(三) pytest运行多个文件

    1.运行多个测试文件 pytest 会运行 test_ 开头 或者 _test 结尾的文件,在当前目录和子目录中 2. 一个类下的多个用例的运行, pytest会找到 test_ 开头的方法 impo ...

  10. 良许被百万大V安排得服服帖帖,还跟美女小姐姐合影了……

    大家好,我是良许. 很多人问我说,良许,你在工作之余还花这么多时间精力去写公众号运营自媒体,到底是为了什么? 其实原因很简单,就是想做个副业,万一到了 35 岁真的失业了,我至少还有另外一份收入,不至 ...