Fiter的信息如下:

Filter的类型有:annotation(这是spring默认的),assignable,aspectj, regex,custom

首先看一下我这个demo的目录结构:

上图中所有被红色圆圈圈住的都是本demo要写的代码:

AppConfig的代码如下:

  1. package com.timo.resourceJavaConfig;
  2. import com.timo.entity.Master;
  3. import org.springframework.context.annotation.ComponentScan;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.context.annotation.FilterType;
  6. import org.springframework.stereotype.Repository;
  7.  
  8. /**
  9. * The following example shows the configuration ignoring all @Repository annotations and using
  10. "stub" repositories instead.
  11. */
  12. @Configuration
  13. @ComponentScan(basePackageClasses = Master.class,
  14. includeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern =
  15. ".*Stub.*Repository"),
  16. excludeFilters = @ComponentScan.Filter(Repository.class))
  17. public class AppConfig {
  18. }

等价于用xml写的:appconfig.xml的代码如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:c="http://www.springframework.org/schema/c"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xmlns:context="http://www.springframework.org/schema/context"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context.xsd">
  14. <!--<context:annotation-config/> only looks for annotations on beans in the same application context in which
  15. it is defined RequiredAnnotationBeanPostProcessor AutowiredAnnotaionBeanPostProcessor
  16. CommonAnnotationBeanPostProcessor PersistenceAnnotaionBeanPostProcessor-->
  17. <context:component-scan base-package="com.timo.entity" annotation-config="true">
  18. <context:include-filter type="regex" expression=".*Sub.*Respository"/>
  19. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
  20. </context:component-scan>
  21. </beans>

com.timo.entity包下

Dog的代码如下:

  1. package com.timo.entity;
  2.  
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.stereotype.Repository;
  5. import org.springframework.stereotype.Service;
  6.  
  7. /**
  8. * 这里可以写@Component,@Service,@Controller注解,写@Repository注解会报错
  9. * 报错的原因是在配置文件或者注解排除了@Repository.
  10. */
  11. @Service
  12. public class Dog {
  13. @Value("pug")
  14. private String name;
  15. @Value("")
  16. private Integer age;
  17.  
  18. public String getName() {
  19. return name;
  20. }
  21.  
  22. public void setName(String name) {
  23. this.name = name;
  24. }
  25.  
  26. public Integer getAge() {
  27. return age;
  28. }
  29.  
  30. public void setAge(Integer age) {
  31. this.age = age;
  32. }
  33. }

Master的代码如下:

  1. package com.timo.entity;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Component;
  5.  
  6. @Component
  7. public class Master {
  8. @Autowired
  9. private Dog dog;
  10.  
  11. public Dog getDog() {
  12. return dog;
  13. }
  14.  
  15. public void setDog(Dog dog) {
  16. this.dog = dog;
  17. }
  18. public void showDogInfo(){
  19. System.out.println("name="+dog.getName());
  20. System.out.println("age="+dog.getAge());
  21. }
  22. }

测试用xml的代码如下:

Test4的代码如下:

  1. package com.timo.test2;
  2.  
  3. import com.timo.entity.Dog;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. public class Test4 {
  7. public static void main(String[] args) {
  8. ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("appconfig.xml");
  9. Dog dog = applicationContext.getBean(Dog.class);
  10. System.out.println("name="+dog.getName());
  11. System.out.println("age="+dog.getAge());
  12. }
  13. }

测试用注解写的Test3的代码如下:

  1. package com.timo.test2;
  2.  
  3. import com.timo.entity.Dog;
  4. import com.timo.resourceJavaConfig.AppConfig;
  5. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  6.  
  7. public class Test3 {
  8. public static void main(String[] args) {
  9. AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
  10. Dog dog = applicationContext.getBean(Dog.class);
  11. System.out.println("dog="+dog);
  12. }
  13. }

用filters定制化spring的包扫描的更多相关文章

  1. Spring和Spring MVC包扫描

    在Spring整体框架的核心概念中,容器是核心思想,就是用来管理Bean的整个生命周期的,而在一个项目中,容器不一定只有一个,Spring中可以包括多个容器,而且容器有上下层关系,目前最常见的一种场景 ...

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

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

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

    开始学习springmvc各种小白问题 根据例子配置了spring扫描包,但是一直提示404错误,经过大量搜索,发现,扫描包的配置应该写在springmvc的配置文件中,而不是springmvc 配置 ...

  4. spring boot包扫描不到controller层

    启动类代码 package com.maven.demo; import org.mybatis.spring.annotation.MapperScan; import org.springfram ...

  5. fpm定制化RPM包之nginx rpm包的制作

    fpm定制化RPM包之nginx rpm包的制作 1.安装ruby模块 # yum -y install ruby rubygems ruby-devel 2.添加阿里云的Rubygems仓库,国外资 ...

  6. Spring自动扫描无法扫描jar包中bean的解决方法(转)

    转载自:http://www.jb51.net/article/116357.htm 在日常开发中往往会对公共的模块打包发布,然后调用公共包的内容.然而,最近对公司的公共模块进行整理发布后.sprin ...

  7. 自动化部署必备技能—定制化RPM包[转载]

    回顾下安装软件的三种方式: 1.编译安装软件,优点是可以定制化安装目录.按需开启功能等,缺点是需要查找并实验出适合的编译参数,诸如MySQL之类的软件编译耗时过长. 2.yum安装软件,优点是全自动化 ...

  8. 自动化部署必备技能—定制化RPM包

    回顾下安装软件的三种方式: 1.编译安装软件,优点是可以定制化安装目录.按需开启功能等,缺点是需要查找并实验出适合的编译参数,诸如MySQL之类的软件编译耗时过长. 2.yum安装软件,优点是全自动化 ...

  9. 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:11.定制化Log输出

    欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 前言 在<迷你微信>服务器中,我们用了Log4J来进行输出,这可以在我们程序出现异常的时候找到错误发生时 ...

随机推荐

  1. SSH远程登录和端口转发详解

     SSH远程登录和端口转发详解   介绍 SSH 是创建在应用层和传输层基础上的安全协议,为计算机上的 Shell(壳层)提供安全的传输和使用环境. SSH 只是协议,有多种实现方式,本文基于其开源实 ...

  2. 抽象类实验:SIM卡抽象

    抽象SIM: package sim_package; public abstract class SIM { public abstract String giveNumber(); public ...

  3. 毕业2年 Summary

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/135 看了下去年写毕业一周年总结的时间:2017-6-16,今天 ...

  4. 贪心算法之Huffman

    Huffman编码,权重越大,离根节点越大.所以就是不断的选取两个最小的树,然后组成一颗新树,加入集合,然后去除已选的两棵树.不断的循环,直到最后的树的集合只剩下一棵,则构建完成,最后输出Huffma ...

  5. 【財務会計】BS科目とは・PL科目とは

    「BS科目」「PL科目」という言葉がありますが.聞いたことあるけどよくわからん!っていう人は多いと思います.なので.簡単にご説明を. BS科目は「いくらあるか」 「BS科目」は.「B/S科目」と書くこ ...

  6. FTP 主动模式与被动模式

    项目中涉及到媒资传输的地方,均有ftp应用,而关于媒资传输故障的排查中,FTP主被动模式问题占了较高比例,但又容易被忽略, 特此收集相关资料介绍,同时整理了如何通wget.tcpdum分辨FTP的主被 ...

  7. MSSQL如何查看当前数据库的连接数 【转】

    - [SQL Server]版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明 http://ai51av.blogbus.com/logs/52955622.html   如果我们发布 ...

  8. C++各种类型的简单排序大汇总~

    啊,排序的技能点也太多了吧!!!LITTLESUN快要**在排序的技能场了啊!(划掉)经历了两天48小时2880分钟172800秒的艰苦奋斗,终于终于终于学的差不多了!明天就可以去打排序的小怪喽!(撒 ...

  9. CentOS7安装Oracle 11gR2 图文详解

    注:Oracle11gR2 X64安装 一.环境准备 安装包: 1.VMware-workstation-full-11.1.0-2496824.exe 2.CentOS-7-x86_64-DVD-1 ...

  10. 接口测试工具postman(二)创建新项目

    1.此次添加一个request,可以点击左上角的New的下拉选择Request,或者点击New弹出选项框点击Request 2.弹出新增request页面 3.添加请求的参数等 4.也可以直接添加新请 ...