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>

啦啦啦

    

啦啦啦

啦啦啦

啦啦啦

最新文章

  1. C#四种深拷贝方法
  2. jQuery 中 attr() 和 prop() 方法的区别
  3. (原创)LAMP搭建之二:apache配置文件详解(中英文对照版)
  4. Python: 测试函数是否被调用
  5. IT电子书网站下载
  6. 设计模式之单例(singleton)设计模式代码详解
  7. Picasso 修改缓存路径
  8. ReactJS入门2:组件状态
  9. xml格式字符串转为Map
  10. 史上最全 40 道 Dubbo 面试题及答案,看完碾压面试官!
  11. MySQL 基础知识梳理学习(二)----记录在页面层级的组织管理
  12. Linux 学习 (八) Shell
  13. ajax的get和post请求 -- 基于flask 简单示例
  14. java static关键字的使用
  15. 从 Confluence 5.3 及其早期版本中恢复空间
  16. 项目Alpha冲刺 1
  17. BZOJ.2434.[NOI2011]阿狸的打字机(AC自动机 树状数组 DFS序)
  18. 第三百四十四节,Python分布式爬虫打造搜索引擎Scrapy精讲—craw母版l创建自动爬虫文件—以及 scrapy item loader机制
  19. PL/SQL Developer 如何记住密码
  20. 20165227 学习基础和C语言基础调查

热门文章

  1. hbase源码系列(二)HTable 探秘
  2. git 服务器新建仓库 远程仓库
  3. PySpider问题记录http599
  4. Qt安装过程中: configure 时发生的经典出错信息之”Basic XLib functionality test failed!”(Z..z..) 之 MySQL support cannot be enabled due to functionality test!
  5. USB2.0相关应用笔记集锦
  6. (笔记)Mysql命令show databases:显示所有数据库
  7. elasticsearch系列四:搜索详解(搜索API、Query DSL)
  8. InteliJ Idea pom.xml不自动提示的解决
  9. (转)OpenGL ES编程入门资源集合
  10. springboot form 提交集合 list