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-自动扫描组件&指定扫 ...
随机推荐
- C++:顺序表类实现约瑟夫问题_密码不同
//.h #pragma once #include <iostream> using namespace std; #define MAXSIZE 100 template <cl ...
- 车牌识别代码OpenCV
#include<opencv2\opencv.hpp> #include<iostream> using namespace cv; using namespace std; ...
- GoF 的 23 种设计模式的分类和功能
1. 根据目的来分 根据模式是用来完成什么工作来划分,这种方式可分为创建型模式.结构型模式和行为型模式 3 种. 创建型模式:用于描述“怎样创建对象”,它的主要特点是“将对象的创建与使用分离”.GoF ...
- Windows下命令行Git无法显示中文问题解决方案
Windows下Git设置编码正常显示中文: 在 CMD 下设置环境变量 set LESSCHARSET=utf-8 在 PowerShell 下设置环境变量 $env:LESSCHARSET='ut ...
- 『Python基础』第4节:基础数据类型初识
本节只是对基础数据类型做个简单介绍, 详情会在之后慢慢介绍 什么是数据类型? 我们人类可以分清数字与字符串的区别, 可是计算机不能. 虽然计算机很强大, 但在某种程度上又很傻, 除非你明确告诉它数字与 ...
- (十五)SpringBoot之使用Redis做缓存数据
一.添加Redis依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...
- Spring IOC原理分析
IOC IOC(Inversion of Control)控制反转:所谓的控制反转,就是把原先需要我们代码自己实现对象的创建和依赖,反转给容器来实现.那么必然Spring需要创建一个容器,同时需要创建 ...
- wince如何扫描条码并且在浏览器上查询数据
这个挺简单的,winform也适用 public override void OnGetBarcode(string scanStr) { try { Process.Start("iesa ...
- cygwin_exception::open_stackdumpfile: Dumping stack trace to HttpServer.exe.stackdump错误
本来,我在Windows下使用Cygwin编译运行c程序,在执行*.exe时报出如题错误,我在Linux环境下使用gcc编译运行,则正常. 所以,当你无法解决上述问题时,换系统吧!
- MySQL全同步复制基于GR集群架构实现(Centos7)
目录 一. 理论概述 概述 二. 部署 向组加入新节点 测试 三.总结 一. 理论概述 概述 本案例操作的是针对于MySQL的复制类型中的全同步复制,对几种复制类型简单总结下: 异步复制:MySQL默 ...