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提供一个默认的构造函数(无参构造函数),并为 ...
随机推荐
- h5 rem
<script> (function(){ setRem(); window.addEventListener('orientation' in window?"deviceor ...
- 升级R语言
Linux (RedHat, CentOS)上升级R语言: sudo yum install R Windows上升级R语言: install.packages("installr" ...
- JavaScript世界的一等公民—— 函数
简介 在很多传统语言(C/C++/Java/C#等)中,函数都是作为一个二等公民存在,你只能用语言的关键字声明一个函数然后调用它,如果需要把函数作为参数传给另一个函数,或是赋值给一个本地变量,又或是作 ...
- linux 将终端进行换行
原始 修改 修改方法 vim .bashrc fi if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debia ...
- python 搜索引擎Whoosh中文文档和代码 以及jieba的使用
注意, 数据库的表最好别有下划线 中文文档链接: https://mr-zhao.gitbooks.io/whoosh/content/%E5%A6%82%E4%BD%95%E7%B4%A2%E5%B ...
- SpagoBI 教程 Lesson 4: BIRT Reports
SpagoBI Lesson 4: BIRT Reports BIRT BIRT is the acronym for Business Intelligence and Reporting Tool ...
- 第三百六十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作、增、删、改、查
第三百六十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作.增.删.改.查 elasticsearch(搜索引擎)基本的索引 ...
- 第三百二十七节,web爬虫讲解2—urllib库爬虫—基础使用—超时设置—自动模拟http请求
第三百二十七节,web爬虫讲解2—urllib库爬虫 利用python系统自带的urllib库写简单爬虫 urlopen()获取一个URL的html源码read()读出html源码内容decode(& ...
- oc总结 --oc基础语法相关知识
m是OC源文件扩展名,入口点也是main函数,第一个OC程序: #import <Foundation/Foundation.h> int main(int argc, const cha ...
- Java如何处理异常方法?
在Java编程中,如何处理异常方法? 本例展示了如何使用System类的System.err.println()方法处理异常方法. package com.yiibai; public class E ...