3、组件注册-@ComponentScan-自动扫描组件&指定扫描规则
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-自动扫描组件&指定扫描规则的更多相关文章
- 【Spring注解驱动开发】组件注册-@ComponentScan-自动扫描组件&指定扫描规则
写在前面 在实际项目中,我们更多的是使用Spring的包扫描功能对项目中的包进行扫描,凡是在指定的包或子包中的类上标注了@Repository.@Service.@Controller.@Compon ...
- 5、组件注册-@Scope-设置组件作用域
5.组件注册-@Scope-设置组件作用域 IOC容器默认都是单实例的 /** * * {@link ConfigurableBeanFactory#SCOPE_SINGLETON SCOPE_SIN ...
- spring02-组件注册-@ComponentScan-自动扫描组件&指定扫描规则
上一篇我们讲到,讲@Bean注解标在某个方法上,那么ioc容器启动的时候就会将方法返回值放到ioc容器中 在开发中,实际上包扫描用的比较多,接下来我们会介绍两种方式一种是基于xml,一种是基于注解. ...
- 二、Spring中的@ComponentScan自动扫描组件
在以往采用xml配置的方式中,我们通常需要配置<context:component-scan>标签 比如这样: <!-- 包扫描.只要标注了@Controller.@Service. ...
- spring注解扫描组件注册
最近对单点系统进行微服务拆分,被各个springboot的组件注册搞得云里雾里的.(有的是通过springboot的自动配置进IOC容器的,有的是自己添加构造方法添加进IOC容器.)决定抽时间将spr ...
- Spring注解开发系列Ⅰ--- 组件注册(上)
传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...
- Spring Boot 自动扫描组件
使用@ComponentScan自动扫描组件 案例准备 1.创建一个配置类,在配置类上添加 @ComponentScan 注解.该注解默认会扫描该类所在的包下所有的配置类,相当于之前的 <con ...
- Spring注解驱动——组件注册系列
1.@Configuration 从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被Annot ...
- 【spring 注解驱动开发】spring组件注册
尚学堂spring 注解驱动开发学习笔记之 - 组件注册 组件注册 1.@Configuration&@Bean给容器中注册组件 2.@ComponentScan-自动扫描组件&指定扫 ...
随机推荐
- 《Mysql 索引 - 概述》
一:索引的目的 - 索引的出现其实就是为了提高数据查询的效率,就像书的目录一样. 二:InnoDB 索引模型 - InnoDB 采用 B+树 的数据结构进行存储. - 例如,我们建立一张表,分析他的数 ...
- Spring Cloud Ribbon说明
浅谈Spring Cloud Ribbon的原理 Spring Cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具,它基于Netflix Ribbon实现.通过Spring Clo ...
- C 统计用户输入的总行数和字符长度
C 统计用户输入的总行数和字符长度 #include <stdio.h> #include <Windows.h> int main(void) { ]; ; ; printf ...
- 『Linux』第二节: 安装Linux系统
一. 准备工具 1. centOS系统下载 http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1810.is ...
- Lieges of Legendre CodeForces - 603C (博弈论,SG找规律)
大意: 给定$n$堆石子, 两人轮流操作, 每次操作两种选择 $(1)$任选非空堆拿走一个石子 $(2)$任选石子数为$2x(x>0)$的一堆, 替换为$k$堆$x$个石子. ($k$给定) 最 ...
- java 字符串处理的
@Test public void testString(){ /*字符串处理 */ String email="123456789@qq.com"; System.out.pri ...
- jacascript Math (算数)对象
前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 实际应用中用的比较多的有:round(); random(); floor(); ceil(); 其次还有 ...
- Base64编码为什么会使数据量变大
现在工作中把视频转成base64发现数据量过大无法下载. 1.为什么base64编码会使数据量变大呢? Base64编码的思想是是采用64个基本的ASCII码字符对数据进行重新编码.它将需要编码的数据 ...
- Vue字符串padStart和padEnd方法
padStart()用于头部补全,padEnd()用于尾部补全. 'a'.padStart(3, '0') // '00a' 'x'.padEnd(5, 'ab') // 'xabab' 'x'.pa ...
- 2019年6月SAP发布的未来ABAP平台的发展方向
未来ABAP平台将始终是这些产品的技术平台: S/4HANA On-Premises和Cloud将基于一个统一的ABAP codeline: SAP云平台上的ABAP编程环境: 什么是SAP Clou ...