用filters定制化spring的包扫描

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("18")
    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);
    }
}

 

posted @ 2017-11-29 22:13  技术让世界更精彩  阅读(380)  评论(0编辑  收藏  举报