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>
啦啦啦
啦啦啦
啦啦啦
啦啦啦
8 -- 深入使用Spring -- 2...1 搜索Bean类的更多相关文章
- Spring学习日志之Bean的装配
Spring容器负责创建应用程序中的bean并通过依赖注入来协调这些对象之间的关系.但是,作为开发人员,要告诉Spring需要创建哪些bean并且如何将其装配在一起.当描述bean如何装配时,Spri ...
- 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean
7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...
- spring概念简介、bean扫描与注册实现方式
写在前面:本文作为整理,包含很多个人理解,有跳跃成份,初学者如果看晕了,可以先看其它同类文章,或者……多看几遍. 一.概念部分: 1.spring概念:网上有很多 2.spring核心:IOC(DI) ...
- Spring IoC 容器和 bean 对象
程序的耦合性: 耦合性(Coupling),又叫耦合度,是对模块间关联程度的度量.耦合的强弱取决于模块间接口的复杂性.调用模块的方式以及通过界面传送数据的多少.模块间的耦合度是指模块之间的依赖关系,包 ...
- 【String注解驱动开发】如何按照条件向Spring容器中注册bean?这次我懂了!!
写在前面 当bean是单实例,并且没有设置懒加载时,Spring容器启动时,就会实例化bean,并将bean注册到IOC容器中,以后每次从IOC容器中获取bean时,直接返回IOC容器中的bean,不 ...
- 【String注解驱动开发】面试官让我说说:如何使用FactoryBean向Spring容器中注册bean?
写在前面 在前面的文章中,我们知道可以通过多种方式向Spring容器中注册bean.可以使用@Configuration结合@Bean向Spring容器中注册bean:可以按照条件向Spring容器中 ...
- Spring中常见的bean创建异常
Spring中常见的bean创建异常 1. 概述 本次我们将讨论在spring中BeanFactory创建bean实例时经常遇到的异常 org.springframework.beans.fa ...
- Spring之BeanFactory及Bean生命周期
1.spring通过BeanFactory灵活配置.管理bean,Spring对管理的bean没有任何特别的要求,完全支持对POJO的管理: 2.BeanFactory有个ApplicationCon ...
- Spring学习笔记(3)——Bean的注入方式
依赖注入 依赖注入支持属性注入.构造函数注入.工厂注入. 属性注入: 属性注入即通过setXxx()方法注入Bean的属性值或依赖对象 属性注入要求Bean提供一个默认的构造函数(无参构造函数),并为 ...
随机推荐
- redis获取当前时间精确到微秒
在redis取得当前时的方法为执行time命令 127.0.0.1:6382> time1) "1495780564"2) "894089" 第一行为以 ...
- 微信小程序文字水平垂直居中对齐问题
我们知道常用的居中对齐方式有很多种例如: text-align:center; align-items:center; justify-content: center; margin: auto; # ...
- C++自定义异常类
代码样例: #include <iostream> using namespace std; class illegalParameterValue { public: illegalPa ...
- c++ 文件增加#ifndef、#define 和 #endif 语句的意义
文件currency.h (或 currencyOverload.h) 包含了 currency类的声明和实现细节. 在文件头, 应该加上语句 #ifndef Currency_ #define Cu ...
- WEB打印大全
1.控制"纵打". 横打”和“页面的边距. (1)<script defer> function SetPrintSettings() { // -- advance ...
- 【转】【Python】Python正则表达式使用指导
1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分.正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的方法,但功能十 ...
- python一天一题(1)
#有一个文件,文件名为output_1981.10.21.txt . # 下面使用Python: 读取文件名中的日期时间信息,并找出这一天是周几. # 将文件改名为output_YYYY-MM-DD- ...
- Android 8 wifi 扫描时间间隔
wifi setting界面扫描时间间隔:10s 不在wifi setting界面,扫描时间间隔,最小20s,然后按找2倍的间隔进行递增,40s,60s..., 最大160s PNO 即Preferr ...
- e606. Determining Which Component or Window Has the Focus
// null is returned if none of the components in this application has the focus Component compFocusO ...
- Windows查看占用端口的进程及其对应的应用程序并关闭之
^_^ C:\Users\dsp> C:\Users\dsp>netstat -ano | findstr " TCP LISTENING TCP TIME_WAIT TCP T ...