3、组件注册-@ComponentScan-自动扫描组件&指定扫描规则

3.1 xml方式 benas.xml 导入context命名空间

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--包扫描 , 只要标注了 @Controller 、@Service、@Repository、@Component的类 都会被扫描-->
<context:component-scan base-package="com.hw.springannotation"></context:component-scan> <bean id="pension" class="com.hw.springannotation.beans.Pension">
<property name="name" value="hw"></property>
<property name="age" value="18"></property>
</bean>
</beans>

3.2 注解方式

3.2.1 默认规则
@ComponentScan(value = "com.hw.springannotation")
3.2.2 指定排除扫描
@ComponentScan(value = "com.hw.springannotation",
excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class, Service.class})}
)
3.2.3 指定扫描(需要关闭默认配置)
@ComponentScan(value = "com.hw.springannotation", useDefaultFilters = false,
includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})}
) // 指定只扫描

3.3 注意

  • 如果是jdk8 ,@Repeatable(ComponentScans.class) 重复组件,可以多写几次
  • 如果不是,可以使用 @ComponentScans 指定多个扫描组件
@ComponentScans(value = {
@ComponentScan(value = "com.hw.springannotation", useDefaultFilters = false,
includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class})}
), @ComponentScan(value = "com.hw.springannotation",
excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class, Service.class})})
})

3、组件注册-@ComponentScan-自动扫描组件&指定扫描规则的更多相关文章

  1. 【Spring注解驱动开发】组件注册-@ComponentScan-自动扫描组件&指定扫描规则

    写在前面 在实际项目中,我们更多的是使用Spring的包扫描功能对项目中的包进行扫描,凡是在指定的包或子包中的类上标注了@Repository.@Service.@Controller.@Compon ...

  2. 5、组件注册-@Scope-设置组件作用域

    5.组件注册-@Scope-设置组件作用域 IOC容器默认都是单实例的 /** * * {@link ConfigurableBeanFactory#SCOPE_SINGLETON SCOPE_SIN ...

  3. spring02-组件注册-@ComponentScan-自动扫描组件&指定扫描规则

    上一篇我们讲到,讲@Bean注解标在某个方法上,那么ioc容器启动的时候就会将方法返回值放到ioc容器中 在开发中,实际上包扫描用的比较多,接下来我们会介绍两种方式一种是基于xml,一种是基于注解. ...

  4. 二、Spring中的@ComponentScan自动扫描组件

    在以往采用xml配置的方式中,我们通常需要配置<context:component-scan>标签 比如这样: <!-- 包扫描.只要标注了@Controller.@Service. ...

  5. spring注解扫描组件注册

    最近对单点系统进行微服务拆分,被各个springboot的组件注册搞得云里雾里的.(有的是通过springboot的自动配置进IOC容器的,有的是自己添加构造方法添加进IOC容器.)决定抽时间将spr ...

  6. Spring注解开发系列Ⅰ--- 组件注册(上)

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

  7. Spring Boot 自动扫描组件

    使用@ComponentScan自动扫描组件 案例准备 1.创建一个配置类,在配置类上添加 @ComponentScan 注解.该注解默认会扫描该类所在的包下所有的配置类,相当于之前的 <con ...

  8. Spring注解驱动——组件注册系列

    1.@Configuration 从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被Annot ...

  9. 【spring 注解驱动开发】spring组件注册

    尚学堂spring 注解驱动开发学习笔记之 - 组件注册 组件注册 1.@Configuration&@Bean给容器中注册组件 2.@ComponentScan-自动扫描组件&指定扫 ...

随机推荐

  1. @ConfigurationProperties和@Value的区别

    @ConfigurationProperties @Value 功能: 批量注入配置文件中的属性 一个个指定,多个属性多个@Value 松散绑定: 支持 不支持 SpEL: 不支持    支持 JSR ...

  2. PHP中类成员的访问控制

    类成员访问控制: 1.public 默认的,任何地方都可以访问,类内,类外,子类中 2.protected 受保护的,对外是封闭的,但是类内部和子类可以访问 3.private  私有的,仅限于本类中 ...

  3. yii2中 选择布局的方式,可以设置不使用布局

    ###yii2中 选择布局的方式,可以设置不使用布局 控制器内成员变量 public $layout = false; //不使用布局 public $layout = "main" ...

  4. Spring框架ioc概括

    什么是Spring且能做什么 Spring是一个开源框架,它由Rod Johnson创建.它是为了解决企业应用开发的复杂性而创建的. Spring使用基本的JavaBean来完成以前只可能由EJB完成 ...

  5. ggalluvial|TCGA临床数据绘制桑基图(Sankey)

    本文首发于”生信补给站“,https://mp.weixin.qq.com/s/yhMgkST-rVD6SaQS7R-eoA 桑基图(Sankey diagram),是一种特定类型的流程图,图中延伸的 ...

  6. (六)授权(下):自定义permission

    一.Authorizer.PermissionResolver及RolePermissionResolver Authorizer的职责是进行授权(访问控制),是Shiro API中授权核心的入口点, ...

  7. Java MergeSort

    Java MergeSort /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternational ...

  8. Flash播放控件属性详解

    Flash 播放控件属性详解 一.属性篇 1.AlignMode(读写)  语法:AlignMode As Long  说明:对齐方式(与SAlign 属性联动).当控件的长宽比例与影片不一致且WMo ...

  9. 结合python实现的netcat与python实现的tcp代理,建立一个流量隧道

    在proxy中 python2 proxy.py 127.0.0.1 3334 192.158.1.111 80 true 作为服务器在本地3334端口进行监听, 作为客户端连接远程web服务器192 ...

  10. 基于【 MySql 】一 || 主从复制

    一.centos7安装mysql 1. 先检查系统是否装有mysql rpm -qa | grep mysql 2. 下载mysql的repo源 wget http://repo.mysql.com/ ...