spring注解第02课 包扫描@ComponentScan、@ComponentScans
1.配置文件形式:
<context:component-scan base-package="com.atguigu" />
spring会扫描此包下的@Service @Repository @Component @Autoware @Resource 等注解
2.注解形式
在配置文件注解类(@Configuration)上声明@ComponentScans,里面包含多个@ComponentScan,
或是只声明@ComponentScan
package com.atguigu.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.ComponentScans; import com.atguigu.bean.Person; //配置类==配置文件
@Configuration //告诉Spring这是一个配置类 @ComponentScans(
value = {
@ComponentScan(value="com.atguigu",includeFilters = {
/* @Filter(type=FilterType.ANNOTATION,classes={Controller.class}),
@Filter(type=FilterType.ASSIGNABLE_TYPE,classes={BookService.class}),*/
@Filter(type=FilterType.CUSTOM,classes={MyTypeFilter.class})
},useDefaultFilters = false)
}
)
//@ComponentScan value:指定要扫描的包
//excludeFilters = Filter[] :指定扫描的时候按照什么规则排除那些组件
//includeFilters = Filter[] :指定扫描的时候只需要包含哪些组件
//FilterType.ANNOTATION:按照注解
//FilterType.ASSIGNABLE_TYPE:按照给定的类型;
//FilterType.ASPECTJ:使用ASPECTJ表达式
//FilterType.REGEX:使用正则指定
//FilterType.CUSTOM:使用自定义规则
public class MainConfig { //给容器中注册一个Bean;类型为返回值的类型,id默认是用方法名作为id
@Bean("person")
public Person person01(){
return new Person("lisi", 20);
} }
3.自定义过滤器(用于注解)
package com.atguigu; import java.io.IOException; import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter; public class MyTypeFilter implements TypeFilter { public boolean match(MetadataReader metadataReader,
MetadataReaderFactory metadataReaderFactory) throws IOException {
//获取当前类的注解信息
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
//获取当前类的类信息
ClassMetadata classMetadata = metadataReader.getClassMetadata(); System.out.println("------------->" + classMetadata.getClassName());
//资源信息
Resource resource = metadataReader.getResource(); if(classMetadata.getClassName().contains("Controller")){
return true;
}
return false;
} }
spring注解第02课 包扫描@ComponentScan、@ComponentScans的更多相关文章
- Spring注解的使用和组件扫描
非常重要] 组件扫描(Component-Scan) 通过配置组件扫描,可以使得spring自动扫描package,而不必在spring的配置文件中逐一声明各个<bean> 在配置组件扫描 ...
- Spring注解驱动开发01(组件扫描使用详解)
使用Spring注解代替XML的方式 以前都是通过xml配bean的方式来完成bean对象放入ioc容器,即使通过@Aotuwire自动装配bean,还是要创建一个xml文件,进行包扫描,显得过于繁琐 ...
- spring注解第04课 @Import
1.beans package com.atguigu.bean; public class Blue { public Blue(){ System.out.println("blue.. ...
- spring注解第01课 @Configuration、@Bean
一.原始的 xml配置方式 1.Spring pom 依赖 <dependency> <groupId>org.springframework</groupId> ...
- spring注解第07课 @Valid和@Validated的总结区分
@Valid: @Valid注解用于校验,所属包为:javax.validation.Valid. ① 首先需要在实体类的相应字段上添加用于充当校验条件的注解,如:@Min,如下代码(age属于Gir ...
- spring注解第05课 FactoryBean
1.工厂bean调用 @Configuration public class MainConfig2 {/** * 使用Spring提供的 FactoryBean(工厂Bean); * 1).默认获取 ...
- spring注解第03课 按条件加载Bean @Conditional
package com.atguigu.config; import org.springframework.context.annotation.Bean; import org.springfra ...
- spring注解第06课 @Value
1.注入<bean>中的属性 支持3种类型的赋值 <bean id="person" class="com.model.Person"> ...
- 为啥Spring和Spring MVC包扫描要分开?
背景: 最近在搭建新工程的时候发现有些Spring的配置不是很了解,比如Spring 配置里面明明配置了component-scan,为啥Spring MVC配置文件还需要配置一下,这样岂 ...
随机推荐
- su命令详解
-----------------------------------------------------------------su 权限设置[root@localhost ~]# vim /etc ...
- oath2
最近在补架构师图谱里的内容,OAuth2.0是其中一块,抽空看了几个文章,理解了一下. 不过我感觉多数文章都不是很直观,花费了好久才理解其中的过程,以及为什么要这么设计,也许里面还有一些为什么没搞清楚 ...
- A1072. Gas Station
A gas station has to be built at such a location that the minimum distance between the station and a ...
- Python 3 入门,看这篇就够了
文章目录 简介 基础语法 运算符 变量 数据类型 流程控制 迭代器 生成器 函数 自定义函数 参数传递 可更改与不可更改对象 参数 匿名函数 变量作用域 模块 面向对象 错误和异常 文件操作 序列化 ...
- pandas 读取excel文件对数据简单清洗并用matplotlib 将数据展示
首先我们看下数据 接下来数据分析操作 import numpy as np import pandas as pd from matplotlib import pyplot as plt if __ ...
- html 网页背景图片根据屏幕大小CSS自动缩放
https://blog.csdn.net/coslay/article/details/47109281 腾讯微博和QQ空间的登录背景图片是根据访客的屏幕大小自动缩放的,但是好像是用JQuery代码 ...
- 项目管理干货——项目管理入门级书籍(附赠5G项目管理书籍电子版)
各位项目经理我们都是在不断的学习和自我学习中不断成长的,所以我们需要多看书,才能做好一个项目经理,但是很多人,在挑选书籍的时候犯了难,今天我就把自己学习的时候看的那些书单整理出来,大家有需要的可以留言 ...
- SNP在世界地图上的频率分布
简单介绍两个网页工具,第一个是GGV,其界面如下: 第二个工具是HGDP,个人感觉画出来有点丑..都是同一所大学开发出来的:界面如下:
- R语言实现两文件对应行列字符替换(解决正负链统一的问题)
假设存在文件file1.xlsx,其内容如下: 存在文件file2.xlsx,其内容如下: 现在我想从第七列开始,将file2所有的字符替换成file1一样的,即第七.八.九.十列不需要改变,因为fi ...
- 苹果电脑利用curl下载数据集
在看tensorflow书上迁徙学习的这一部分的时候,书上说利用 curl http://download.tensorflow.org/example_images/flower_photos.tg ...