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的更多相关文章

  1. Spring注解的使用和组件扫描

    非常重要] 组件扫描(Component-Scan) 通过配置组件扫描,可以使得spring自动扫描package,而不必在spring的配置文件中逐一声明各个<bean> 在配置组件扫描 ...

  2. Spring注解驱动开发01(组件扫描使用详解)

    使用Spring注解代替XML的方式 以前都是通过xml配bean的方式来完成bean对象放入ioc容器,即使通过@Aotuwire自动装配bean,还是要创建一个xml文件,进行包扫描,显得过于繁琐 ...

  3. spring注解第04课 @Import

    1.beans package com.atguigu.bean; public class Blue { public Blue(){ System.out.println("blue.. ...

  4. spring注解第01课 @Configuration、@Bean

    一.原始的 xml配置方式 1.Spring pom 依赖 <dependency> <groupId>org.springframework</groupId> ...

  5. spring注解第07课 @Valid和@Validated的总结区分

    @Valid: @Valid注解用于校验,所属包为:javax.validation.Valid. ① 首先需要在实体类的相应字段上添加用于充当校验条件的注解,如:@Min,如下代码(age属于Gir ...

  6. spring注解第05课 FactoryBean

    1.工厂bean调用 @Configuration public class MainConfig2 {/** * 使用Spring提供的 FactoryBean(工厂Bean); * 1).默认获取 ...

  7. spring注解第03课 按条件加载Bean @Conditional

    package com.atguigu.config; import org.springframework.context.annotation.Bean; import org.springfra ...

  8. spring注解第06课 @Value

    1.注入<bean>中的属性 支持3种类型的赋值 <bean id="person" class="com.model.Person"> ...

  9. 为啥Spring和Spring MVC包扫描要分开?

    背景:       最近在搭建新工程的时候发现有些Spring的配置不是很了解,比如Spring 配置里面明明配置了component-scan,为啥Spring MVC配置文件还需要配置一下,这样岂 ...

随机推荐

  1. 【转】无法获得锁 /var/lib/dpkg/lock - open (11: 资源暂时不可用) ubuntu 安装vim 及遇到的错误处理

    今天,处理完问题,闲来无事,打算在虚拟机中的Ubuntu中练习shell脚本编写. 无奈,虚拟机系统所装的只有vi,这个编辑软件对于我们来说还是比较不习惯的,所以打算安装vim.好了,闲言少叙. 安装 ...

  2. QQ邮箱无限扩容 + XMind8 Update8 Crack 小记

    QQ邮箱扩容 三个月后还可以扩容 XMind8 Update8 Crack 软件地址 软件下载地址:https://www.xmind.cn/download/xmind8 补丁地址 破解补丁下载地址 ...

  3. poj1193 内存分配

    气死我了...这个毒瘤内存分配..... 优先队列 + 链表模拟,看起来搞一搞就好了却WA来WA去... 最后对拍手动找才发现错误: erase的时候不要急急忙忙插入wait!要把同一时期的erase ...

  4. Win32-API: 终于能正常的捕获焦点事件: WM_COMMAND、BN_SETFOCUS、EN_SETFOCUS

    学习和使用win32-api来写窗口程序算算也有二三年了. 以前有个需求,想捕获控件的焦点事件(SETFOCUS. KILLFOCUS),我一直认为应该捕获 WM_SETFOCUS 和 WM_KILL ...

  5. jokes

    先看效果如下 目录如下 //index.html <!DOCTYPE html> <html lang="zh-CN"> <head> < ...

  6. struts2 防止表单的重复提交

    防止表单重复提交(拦截器) 1.回顾之前的解决办法: 2.Struts2中的解决办法: 2.1.使用重定向 <result type="redirect">/succe ...

  7. 初识JSP知识

    一.jsp概述 JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP实际上就是Servlet. jsp = html ...

  8. c#反射(2)

    public void Test1() { string ClassName="Person"; string MethodName="Test1"; //得到 ...

  9. linux 下配置svn

    1.以前配置好的,没做笔记,忘记了.现在重新回忆 查找svn目录:find / -name svn 找到/projects/svn cd /projects/svn 看里面的配置: 查看配置 [roo ...

  10. parallels tools 安装

    │ - kernel-devel-2.6.32-358.el6.x86_64 │ │ - dkms