【spring】之基于注解@ComponentScan的一些使用
基于xml形式ComponentScan的使用如下
<context:component-scan base-package="com.luna" use-default-filters="false">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
基于注解@ComponentScan的使用
@Configuration
/*@ComponentScan(value = "com.luna", includeFilters = {
// @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class}),
// @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value ={com.luna.service.PersonService.class} )
@ComponentScan.Filter(type = FilterType.CUSTOM,value = {MyFilter.class})
},useDefaultFilters = false)*/
@ComponentScan(value = "com.luna",
//排除某些类
excludeFilters = {
//排除Controller注解标注的类
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class}),
//排除指定类型的类
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value ={com.luna.service.PersonService.class} )
})
public class ScanConfig {
}
测试
@Test
public void test(){
AnnotationConfigApplicationContext applicationContext =
new AnnotationConfigApplicationContext(ScanConfig.class);
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
Arrays.asList(beanDefinitionNames).stream().forEach(x->System.out.println("扫描到的Bean---"+x));
}
@ComponentScan一些常用参数
//基本属性 value/basePackages:指定一组要扫描的包,将扫描这些包及其子包下的文件.(默认基包为配置类所在的包)
classes:直接指定扫描的一些类,这些类所在的包及其子包也会被扫描。
nameGenerator:指定一个默认扫描组件的命名类, 默认命名使用组件类名第一个字小写。
excludeFilters:指定扫描的时候排除某些类,按什么规则,需要一组@ComponentScan.Filter的注解配置,每个@Filter可以配置一组过滤规则,多个@Filter可以基于正则/注解/具体类配置不同的过滤规则。
includeFilters:指定扫描的时候,只包含某些类。
useDefaultFilters 自动扫描Controller Component Service Resposity 这四个注解
所以在不指定明确包的情况下如 com.luna 全包扫描,会扫描到Controller Component Service Resposity 四个注解
| FilterType.ANNOTATION(默认) | 一组注解,命中所有使用该注解的类,{MyAnno.class} | - |
| FilterType.ASSIGNABLE_TYPE | 一组具体类 | - |
| FilterType.ASPECTJ | - | 一组表达式,使用Aspectj表达式命中类 |
| FilterType.REGEX | - | 一组表达式,使用正则命中类 |
| FilterType.CUSTOM | 自定义的TypeFilter. | - |
【spring】之基于注解@ComponentScan的一些使用的更多相关文章
- Spring:基于注解的Spring MVC
什么是Spring MVC Spring MVC框架是一个MVC框架,通过实现Model-View-Controller模式来很好地将数据.业务与展现进行分离.从这样一个角度来说,Spring MVC ...
- Spring boot 基于注解方式配置datasource
Spring boot 基于注解方式配置datasource 编辑 Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...
- Spring+Mybatis基于注解整合Redis
基于这段时间折腾redis遇到了各种问题,想着整理一下.本文主要介绍基于Spring+Mybatis以注解的形式整合Redis.废话少说,进入正题. 首先准备Redis,我下的是Windows版,下载 ...
- SpringMVC4 + Spring + MyBatis3 基于注解的最简配置
本文使用最新版本(4.1.5)的springmvc+spring+mybatis,采用最间的配置方式来进行搭建. 1. web.xml 我们知道springmvc是基于Servlet: Dispatc ...
- spring mvc 基于注解的使用总结
本文转自http://blog.csdn.net/lufeng20/article/details/7598801 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sprin ...
- Spring IoC — 基于注解的配置
基于XML的配置,Bean定义信息和Bean实现类本身是分离的,而采用基于注解的配置方式时,Bean定义信息即通过在Bean实现类上标注注解实现. @Component:对类进行标注,Spring容器 ...
- 【Spring】基于注解的实现SpringMVC+MySQL
目录结构: // contents structure [-] SprinigMVC是什么 SpringMVC工作原理 @Controller和@RequestMapping注解 @Controlle ...
- SpringMvc+Spring+MyBatis 基于注解整合
最近在给学生们讲Spring+Mybatis整合,根据有的学生反映还是基于注解实现整合便于理解,毕竟在先前的工作中团队里还没有人完全舍弃配置文件进行项目开发,由于这两个原因,我索性参考spring官方 ...
- Spring:基于注解的依赖注入的使用
1.什么是pojo?什么是bean? 首先,在之前几篇Spring的介绍文章当中,自己都提到了一个名词叫做POJO类,但是在回顾Spring的注解的使用的时候,去形容java当中的对象还有一个名词是叫 ...
随机推荐
- 基于Python——实现解压文件夹中的.zip文件
[背景]当一个文件夹里存好好多.zip文件需要解压时,手动一个个解压再给文件重命名是一件很麻烦的事情,基于此,今天介绍一种使用python实现批量解压文件夹中的压缩文件并给文件重命名的方法—— [代码 ...
- Nginx配置中的log_format用法梳理(设置详细的日志格式)
nginx服务器日志相关指令主要有两条:一条是log_format,用来设置日志格式:另外一条是access_log,用来指定日志文件的存放路径.格式和缓存大小,可以参加ngx_http_log_mo ...
- QSplineSeries QChartView绘制曲线
参考资料: https://www.qtdebug.com/qtbook-paint-smooth-curve-qchart/ https://blog.csdn.net/liang19890820/ ...
- vue教程自学笔记(二)
三.模板语法 1.文本 数据绑定最常见的形式就是使用“Mustache”语法 (双大括号) 的文本插值. 通过v-once指令,你也能执行一次性地插值,当数据改变时,插值处的内容不会更新. 2.原始H ...
- zabbix3.4.7页面中文乱码
无须重启任何服务,刷新页面即可.
- asp.net针对SQLSERVER数据库备份和恢复的一揽子问题解决
一.备份 先备份到服务端,用gridview显示出来,点击可以下载到本地 二.恢复 这是很麻烦的事情,本人查阅不少资料,耗费不少时间才搞定. 直接恢复数据库时不行的,会提示数据库正在使用,恢复失败等信 ...
- java通过StringToKenizer获取字符串中的单词根据空格分离-详情版
public class DaXie { public static void main(String[] args) { String strin = "Hello Java World! ...
- CodeForces - 1015 D.Walking Between Houses
Description Natasha is planning an expedition to Mars for nn people. One of the important tasks is t ...
- SSH登录异常(someone is doing something nasty)
在平时工作中,有时候需要SSH登陆到别的Linux主机上去,但有时候SSH登陆会被禁止,并弹出如下类似提示: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...
- .net core WebApi Mutex实现并发同步
Mutex,中文译为互斥体,在.net中也是作为一种线程或进程之间的互斥体存在.即在同一时刻,一个共享资源只允许被某一个线程或进程访问,其他线程或进程需要等待(直至获取互斥锁为止). Mutex的使用 ...