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>

啦啦啦

    

啦啦啦

啦啦啦

啦啦啦

8 -- 深入使用Spring -- 2...1 搜索Bean类的更多相关文章

  1. Spring学习日志之Bean的装配

    Spring容器负责创建应用程序中的bean并通过依赖注入来协调这些对象之间的关系.但是,作为开发人员,要告诉Spring需要创建哪些bean并且如何将其装配在一起.当描述bean如何装配时,Spri ...

  2. 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean

    7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...

  3. spring概念简介、bean扫描与注册实现方式

    写在前面:本文作为整理,包含很多个人理解,有跳跃成份,初学者如果看晕了,可以先看其它同类文章,或者……多看几遍. 一.概念部分: 1.spring概念:网上有很多 2.spring核心:IOC(DI) ...

  4. Spring IoC 容器和 bean 对象

    程序的耦合性: 耦合性(Coupling),又叫耦合度,是对模块间关联程度的度量.耦合的强弱取决于模块间接口的复杂性.调用模块的方式以及通过界面传送数据的多少.模块间的耦合度是指模块之间的依赖关系,包 ...

  5. 【String注解驱动开发】如何按照条件向Spring容器中注册bean?这次我懂了!!

    写在前面 当bean是单实例,并且没有设置懒加载时,Spring容器启动时,就会实例化bean,并将bean注册到IOC容器中,以后每次从IOC容器中获取bean时,直接返回IOC容器中的bean,不 ...

  6. 【String注解驱动开发】面试官让我说说:如何使用FactoryBean向Spring容器中注册bean?

    写在前面 在前面的文章中,我们知道可以通过多种方式向Spring容器中注册bean.可以使用@Configuration结合@Bean向Spring容器中注册bean:可以按照条件向Spring容器中 ...

  7. Spring中常见的bean创建异常

    Spring中常见的bean创建异常 1. 概述     本次我们将讨论在spring中BeanFactory创建bean实例时经常遇到的异常 org.springframework.beans.fa ...

  8. Spring之BeanFactory及Bean生命周期

    1.spring通过BeanFactory灵活配置.管理bean,Spring对管理的bean没有任何特别的要求,完全支持对POJO的管理: 2.BeanFactory有个ApplicationCon ...

  9. Spring学习笔记(3)——Bean的注入方式

    依赖注入 依赖注入支持属性注入.构造函数注入.工厂注入. 属性注入: 属性注入即通过setXxx()方法注入Bean的属性值或依赖对象 属性注入要求Bean提供一个默认的构造函数(无参构造函数),并为 ...

随机推荐

  1. python安装新版本及pip

    Linux安装python: yum install zlib -yyum install zlib-devel -y yum install readline-devel -y yum instal ...

  2. mysql innodb_buffer_pool_size mysql占用内存大小和主从复制并行线程数量

    innodb_buffer_pool_size   set global slave_parallel_workers=4;

  3. 小米路由器刷Xiaomi Mi WiFi Mini openwrt

    Current Stable Release - OpenWrt 18.06.1,released on August, 18th 2018. there is also PandoraBox fir ...

  4. python 语法最佳实践

    1. 列表推倒 我们知道, 列表类似于数组, 列表里存储的都是对象, 所以列表中可以存储"数字","字符串" 等对象. 列表用中括号扩起, 然后逗号分隔 列表内 ...

  5. node学习笔记5——post数据传递

    上一篇有讲到get数据的传递.有了上一篇的了解,今天讲下如何获取到post传递过来的数据. 通过post传送的数据,在node里面主要是通过req.on('data',function (data) ...

  6. C#注册表读写完整操作类

    1.注册表基项静态域 /// <summary> /// 注册表基项静态域 ///1.Registry.ClassesRoot 对应于HKEY_CLASSES_ROOT 主键 ///2.R ...

  7. Namenode HA原理详解(脑裂)

    转自:http://blog.csdn.net/tantexian/article/details/40109331 Namenode HA原理详解 社区hadoop2.2.0 release版本开始 ...

  8. (笔记)如何安装Arm-linux-gcc

      如何安装Arm-linux-gcc   安装交叉编译工具链: 1.首先以root用户登入 2.复制arm-linux-gcc-4.3.2.tgz到根目录下tmp文件夹里 3.解压命令tar xvz ...

  9. JAVA代码中最常见到的关键字表

    abstract 抽象类或方法 assert 用来查找内部程序错误 break 跳出一个switch或循环 byte 8 位整数类型 case switch的一个分支 catch 捕获异常的try块子 ...

  10. IntelliJ IDEA 2017 汉化包及教程

    一.准备 官网下载IntelliJ IDEA 2017 并安装好 下载汉化包 (百度云链接:http://pan.baidu.com/s/1slS9ZMP 密码:gp79) 二.汉化 此处有两种方法, ...