• 从头认识Spring-2.7 自己主动检測Bean(2)-过滤器<context:include-filter/>


    这一章节我们来讨论一下过滤器<context:include-filter/>的使用。

    1.domain

    Person接口:

    package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_20;
    
    public interface Person {
    
    }
    

    拳击手类:

    package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_20;
    
    import org.springframework.beans.factory.annotation.Value;
    
    public class Fighter implements Person {
    
    	@Value("mike")
    	private String name = "";
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    }
    

    厨师类:

    package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_20;
    
    import org.springframework.beans.factory.annotation.Value;
    
    public class Chief implements Person {
    
    	@Value("jack")
    	private String name = "";
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    }
    

    在上面的类里面我们有益没有使用不论什么标记bean的注解,并且我们再配置文件中面也不会显示标记bean


    2.測试类:

    package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_20;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {
    		"/com/raylee/my_new_spring/my_new_spring/ch02/topic_1_20/ApplicationContext-test.xml" })
    public class ChiefTest {
    
    	@Autowired
    	private ApplicationContext applicationContext;
    
    	@Test
    	public void testChief() {
    		Chief jack = applicationContext.getBean(Chief.class);
    		System.out.println(jack.getName());
    		Fighter mike = applicationContext.getBean(Fighter.class);
    		System.out.println(mike.getName());
    	}
    }
    

    3.配置文件(重点)

    <?

    xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd 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"> <context:component-scan base-package="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_20"> <context:include-filter type="assignable" expression="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_20.Person" /> </context:component-scan> </beans>


    在配置文件中面:

    (1)我们配置自己主动扫描<context:component-scan/>

    (2)在自己主动扫描里面我们在配置<context:include-filter/>,然后type=“assignable”,这里的意思是,Person接口所派生出来的全部类。都自己主动注冊bean

    type的四种參数:

    assignable-指定扫描某个接口派生出来的类

    annotation-指定扫描使用某个注解的类

    aspectj-指定扫描AspectJ表达式相匹配的类

    custom-指定扫描自己定义的实现了org.springframework.core.type.filter.TypeFilter接口的类

    regex-指定扫描符合正則表達式的类


    4.使用场景

    上面我们仅仅是演示了第一种,由于有些时候我们是没有源代码或者一个接口派生n多实现类的情况。这样的场景使用这个最为合适。


    測试输出:

    jack
    mike


    总结:这一章节我们主要介绍了过滤器<context:include-filter/>的用法与使用场景。


    文件夹:http://blog.csdn.net/raylee2007/article/details/50611627 

    我的github:https://github.com/raylee2015/my_new_spring


  • 相关阅读:
    [ css 计数器 counter ] css中counter计数器实例演示三
    [ css 计数器 counter ] css中counter计数器实例演示二
    Leetcode 15. 三数之和
    Leetcode 13. 罗马数字转整数
    Leetcode 19. 删除链表的倒数第 N 个结点
    Leetcode 12. 整数转罗马数字
    Leetcode 11. 盛最多水的容器 双指针
    剑指 Offer 68
    剑指 Offer 68
    面试题 04.02. 最小高度树
  • 原文地址:https://www.cnblogs.com/zsychanpin/p/7197691.html
Copyright © 2020-2023  润新知