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配置文件还需要配置一下,这样岂 ...
随机推荐
- Codeforces | CF1033D 【Divisors】
题目大意:给定\(n(1\leq n\leq500)\)个数\(a_1,a_2\cdots,a_n(1\leq a_i\leq2\cdot10^{18})\),每个数有\(3\sim5\)个因数,求\ ...
- asp.net处理事件
从来不用也从来不研究这事件.但为了写那种CGI式的接口不得已研究一下. 环境 W10 VS2017 测试方法:写一个实现IHttpModule接口的类,在Init方法中加载所有事件然后打出日志,看看 ...
- iis express添加虚拟目录
在调试WEB时,还是使用IIS EXPRESS比较方便, 在IIS中,选择网站,右击,添加虚拟目录或者应用程序,就能添加虚拟目录了.. 在IIS EXPRESS中,添加虚拟目录如下 1.右击IIS E ...
- 洛谷 P4705 玩游戏 解题报告
P4705 玩游戏 题意:给长为\(n\)的\(\{a_i\}\)和长为\(m\)的\(\{b_i\}\),设 \[ f(x)=\sum_{k\ge 0}\sum_{i=1}^n\sum_{j=1}^ ...
- Haunted Graveyard ZOJ - 3391(SPFA)
从点(n,1)到点(1,m)的最短路径,可以转换地图成从(1,1)到(n,m)的最短路,因为有负权回路,所以要用spfa来判负环, 注意一下如果负环把终点包围在内的话, 如果用负环的话会输出无穷,但是 ...
- 仓鼠找sugar(LCA)
小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而他的基友同时要从他的卧室(c)到图书馆( ...
- hdu2795(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题目大意:有一个h*w的公告牌,要在其上贴公告.现在有n个公告,每个公告的尺寸为1*wi,即高度 ...
- Tutorial: Build a Spring WebMVC App with Primefaces
Tutorial: Build a Spring WebMVC App with Primefaces by Team Stormpath | September 7, 2016 | Java Pri ...
- 用tkinter制作签名设计窗口
效果如下: from tkinter import * from tkinter import messagebox import requests import re from PIL import ...
- nohup报错
1 这是脚本编码的问题 解决办法: (1)用vi打开对应的脚本 (2)在命令行下(:set ff?),看一下当前文档的编码格式 ,有两种情况 fileformat=unix和fileformat=do ...