Fiter的信息如下:

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

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

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

AppConfig的代码如下:

package com.timo.resourceJavaConfig;
import com.timo.entity.Master;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Repository; /**
* The following example shows the configuration ignoring all @Repository annotations and using
"stub" repositories instead.
*/
@Configuration
@ComponentScan(basePackageClasses = Master.class,
includeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern =
".*Stub.*Repository"),
excludeFilters = @ComponentScan.Filter(Repository.class))
public class AppConfig {
}

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--<context:annotation-config/> only looks for annotations on beans in the same application context in which
it is defined RequiredAnnotationBeanPostProcessor AutowiredAnnotaionBeanPostProcessor
CommonAnnotationBeanPostProcessor PersistenceAnnotaionBeanPostProcessor-->
<context:component-scan base-package="com.timo.entity" annotation-config="true">
<context:include-filter type="regex" expression=".*Sub.*Respository"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
</beans>

com.timo.entity包下

Dog的代码如下:

package com.timo.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service; /**
* 这里可以写@Component,@Service,@Controller注解,写@Repository注解会报错
* 报错的原因是在配置文件或者注解排除了@Repository.
*/
@Service
public class Dog {
@Value("pug")
private String name;
@Value("")
private Integer age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}

Master的代码如下:

package com.timo.entity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public class Master {
@Autowired
private Dog dog; public Dog getDog() {
return dog;
} public void setDog(Dog dog) {
this.dog = dog;
}
public void showDogInfo(){
System.out.println("name="+dog.getName());
System.out.println("age="+dog.getAge());
}
}

测试用xml的代码如下:

Test4的代码如下:

package com.timo.test2;

import com.timo.entity.Dog;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test4 {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("appconfig.xml");
Dog dog = applicationContext.getBean(Dog.class);
System.out.println("name="+dog.getName());
System.out.println("age="+dog.getAge());
}
}

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

package com.timo.test2;

import com.timo.entity.Dog;
import com.timo.resourceJavaConfig.AppConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Test3 {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
Dog dog = applicationContext.getBean(Dog.class);
System.out.println("dog="+dog);
}
}

用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. 字典树(Trie)的学习笔记

    按照一本通往下学,学到吐血了... 例题1 字典树模板题吗. 先讲讲字典树: 给出代码(太简单了...)! #include<cstdio> #include<cstring> ...

  2. 用ssh进行git clone出现 fatal: Could not read from remote repository.

    问题:在通过MobaXterm进行ssh连接的服务器上用ssh进行git clone出现 fatal: Could not read from remote repository. 解决方法:prox ...

  3. ffmpeg使用笔记

    1.从mp4中提取h264:ffmpeg -i 264.mp4 -codec copy -bsf h264_mp4toannexb -f h264 output.h2642.从mp4中提取hevc:f ...

  4. 最短路径问题 3.Bellman-Ford算法

    简要:Bellman-Ford算法计算的仍然是从一个点到其他所有点的最短路径算法,其时间复杂度是O(NE),N表示点数,E表示边数,不难看出,当一个图稍微稠密一点,边的数量会超过点数那么实际上效率是低 ...

  5. 20145202马超 2016-2017-2 《Java程序设计》第一次实验

    之前做的(http://www.cnblogs.com/tuolemi/p/5707098.html) 其余的 断点的使用 行断点 条件断点 参考(http://www.cnblogs.com/roc ...

  6. SQL Server中2008及以上 ldf 文件过大的解决方法

    在SQL Server中经常遇到事务日志变大的情况,除了将数据库设置为“自动收缩”外,还可以使用下面的SQL命令进行快速清除数据库中的事务日志,命令如下:  - 第一步:清空日志  ALTER DAT ...

  7. Django 2.0官方文档中文 渣翻 总索引(个人学习,欢迎指正)

    Django 2.0官方文档中文 渣翻 总索引(个人学习,欢迎指正) 置顶 2017年12月08日 11:19:11 阅读数:20277 官方原文: https://docs.djangoprojec ...

  8. 安装一个apk文件源代码

     /**   * 安装一个apk文件   *   * @param file   * 要安装的完整文件名   */  protected void installApk(File file) {   ...

  9. 多个excel合并(excel2007)

    1.新建一个空的excel文件,在需要合并的目录下. 2.右键点击sheet1,点击查看代码 3.执行此段代码 Sub 合并当前目录下所有工作簿的全部工作表() Dim MyPath, MyName, ...

  10. MyCAT+MySQL 搭建高可用企业级数据库集群——第2章 MyCat入门

    2-1 章节综述 2-2 什么是MyCat 2-3 什么是数据库中间层 2-4 MyCat的主要作用 2-5 MyCat基本元素 2-6 MyCat的安装 2-1 章节综述 1.掌握Mycat的基础概 ...