8 -- 深入使用Spring -- 2...1 搜索Bean类

      8.2.1 搜索Bean类

        既然不再使用Spring配置文件来配置任何Bean实例,那么只能希望Spring会自动搜索某些路径下的Java类,并将这些Java类注册成Bean实例。

        tips:Rails框架的处理比较简单,它采用一种所谓的“约定优于配置”的方式,它要求将不同组件放在不同路径下,而Rails框架中是加载固定路径下的所有组件。

        Spring没有采用“约定优于配置”的策略,Spring依赖要求程序员显式指定搜索那些路径下的Java类,Spring将会把合适的Java类全部注册成Spring Bean。

        Spring通过使用一些特殊的Annotation来标注Bean类,使Spring识别被标注的Java类当成Bean处理。

        Spring提供了如下几个Annotation来标注SpringBean:

          ⊙ @Component : 标注一个普通的Spring Bean类。

          ⊙ @Controller : 标注一个控制器组件类。

          ⊙ @Service : 标注一个业务逻辑组件类。

          ⊙ @Repository : 标注一个DAO组件类。

        如果需要定义一个普通的Spring Bean ,则直接使用@Component 标注即可。但如果用@Repostory、@Service、@Controller来标注这些Bean类,这些Bean类将被作为特殊的Java EE组件对待,也许能更好地被工具处理,或与切面进行关联。例如,这些典型化的Annotation可以成为理想的切入点目标。

        指定了某些类可作为Spring Bean类使用后,最后还需要让Spring搜索指定路径,此时需要在Spring配置文件中导入context Schema,并指定一个简单的搜索路径。

        Class : SteelAxe

package edu.pri.lime._8_2_1.bean;

import org.springframework.stereotype.Component;

@Component
public class SteelAxe implements Axe {

    public String chop() {
        return "使用钢斧砍材真快";
    }

}

        Class : Chinese

package edu.pri.lime._8_2_1.bean;

import org.springframework.stereotype.Component;

@Component
public class Chinese {
    
    private Axe axe;

    public Axe getAxe() {
        return axe;
    }

    public void setAxe(Axe axe) {
        this.axe = axe;
    }
    
}

        XML : 

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!-- 自动扫描指定包及其子包下的所有Bean类。 -->
    <context:component-scan base-package="edu.pri.lime._8_2_1.bean" />

</beans>

        Class : BeanTest

package edu.pri.lime._8_2_1.bean.main;

import java.util.Arrays;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BeanTest {

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("app_8_2_1.xml");
        /*获取Spring容器中的所有Bean实例的名称*/
        System.out.println("-------------" + Arrays.toString(ctx.getBeanDefinitionNames()));
    }
}

        Console :

-------------[chinese, steelAxe, stoneAxe, org.springframework.context.annotation.internalConfigurationAnnotationProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor, org.springframework.context.annotation.internalRequiredAnnotationProcessor, org.springframework.context.annotation.internalCommonAnnotationProcessor, org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor, org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor]

        在基于Annotation的方式下,Spring采用约定的方式来为Bean实例指定名称,默认是Bean类的首字母小写,其他部分不变。

        Spring也允许使用@Component标注是指定Bean实例的名称:

package edu.pri.lime._8_2_1.bean.impl;

import org.springframework.stereotype.Component;

import edu.pri.lime._8_2_1.bean.Axe;

@Component("axe")
public class StoneAxe implements Axe{

    public String chop() {
        return "使用石斧看材真慢";
    }

}

        默认情况下,Spring会自动搜索所有以@Component、@Controller、@Service和@Repository标注的Java类,并将它们当成Spring Bean处理。除此之外,还可通过为<component-scan.../>元素添加<include-filter.../>或<exclude-filter.../>子元素来指定Spring Bean类,只要位于指定路径下的Java类满足这种规则,即时这些Java类没有使用任何Annotation标注,Spring一样会将他们当成Bean类来处理。

        <include-filter.../>元素用于指定满足该规则的Java类会被当成Bean类处理,<exclude-filter.../>指定满足该规则的Java类不会被当成Bean类处理。

        使用这两个元素时都要求指定如下两个属性:

          ⊙ type : 指定过滤器类型

          ⊙ expression : 指定过滤器所需要的表达式。

        Spring 内建支持如下4中过滤器:

        ⊙ annotation : Annotation过滤器,该过滤器需要指定一个Annotation名,如lime.AnnotationTest。

        ⊙ assignable : 类名过滤器,该过滤器直接指定一个Java 类。

        ⊙ regex : 正则表达式过滤器,该过滤器指定一个正则表达式,匹配该正则表达式的Java类将满足该过滤规则,如com\.example\.Default.*。

        ⊙ aspectj : AspectJ过滤器,如com.example..*Service+。    

        XML :

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    
    <!-- 指定所有以Chinese结尾的类、以Axe结尾的类都将被当成Spring Bean处理 -->
    <context:component-scan base-package="edu.pri.lime._8_2_1.bean">
        <context:include-filter type="regex" expression=".*Chinese"/>
        <context:include-filter type="regex" expression=".*Axe"/>
    </context:component-scan>

</beans>

啦啦啦

    

啦啦啦

啦啦啦

啦啦啦

posted @ 2017-02-10 21:51  limeOracle  阅读(400)  评论(0编辑  收藏  举报