`

Spring配置项之<context:component-scan base-package="..."/>

阅读更多
使用 @Component

虽然我们可以通过@Autowired或@Resource在 Bean 类中使用自动注入功能,但是 Bean 还是在 XML 文件中通过 <bean> 进行定义 —— 也就是说,在 XML 配置文件中定义 Bean,通过@Autowired或@Resource为 Bean 的成员变量、方法入参或构造函数入参提供自动注入的功能。能否也通过注释定义 Bean,从 XML 配置文件中完全移除 Bean 定义的配置呢?答案是肯定的,我们通过 Spring 2.5 提供的@Component注释就可以达到这个目标了。

下面,我们完全使用注释定义 Bean 并完成 Bean 之间装配:


清单 20. 使用 @Component 注释的 Car.java


package com.baobaotao;

import org.springframework.stereotype.Component;

@Componentpublic class Car { …}




仅需要在类定义处,使用@Component注释就可以将一个类定义了 Spring 容器中的 Bean。下面的代码将Office定义为一个 Bean:


清单 21. 使用 @Component 注释的 Office.java


package com.baobaotao;

import org.springframework.stereotype.Component;

@Componentpublic class Office {

private String officeNo = "001";



}




这样,我们就可以在 Boss 类中通过@Autowired注入前面定义的Car和Office Bean了。


清单 22. 使用 @Component 注释的 Boss.java


package com.baobaotao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component("boss")
public class Boss {
    @Autowired
    private Car car;

    @Autowired
    private Office office;
    …
}




@Component有一个可选的入参,用于指定 Bean 的名称,在 Boss 中,我们就将 Bean 名称定义为“boss”。一般情况下,Bean 都是 singleton 的,需要注入 Bean 的地方仅需要通过 byType 策略就可以自动注入了,所以大可不必指定 Bean 的名称。

在使用@Component注释后,Spring 容器必须启用类扫描机制以启用注释驱动 Bean 定义和注释驱动 Bean 自动注入的策略。Spring 2.5 对 context 命名空间进行了扩展,提供了这一功能,请看下面的配置:


清单 23. 简化版的 beans.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <context:component-scan base-package="com.baobaotao"/>
</beans>

这里,所有通过 <bean> 元素定义 Bean 的配置内容已经被移除,仅需要添加一行 <context:component-scan/> 配置就解决所有问题了——Spring XML 配置文件得到了极致的简化(当然配置元数据还是需要的,只不过以注释形式存在罢了)。<context:component-scan/> 的 base-package 属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。

<context:component-scan/> 还允许定义过滤器将基包下的某些类纳入或排除。Spring 支持以下 4 种类型的过滤方式,通过下表说明:


表 1. 扫描过滤方式
注释假如 com.baobaotao.SomeAnnotation 是一个注释类,我们可以将使用该注释的类过滤出来。类名指定通过全限定类名进行过滤,如您可以指定将 com.baobaotao.Boss 纳入扫描,而将 com.baobaotao.Car 排除在外。正则表达式通过正则表达式定义过滤的类,如下所示: com\.baobaotao\.Default.*AspectJ 表达式通过 AspectJ 表达式定义过滤的类,如下所示: com. baobaotao..*Service+

下面是一个简单的例子:

<context:component-scan base-package="com.baobaotao">
    <context:include-filter type="regex"
        expression="com\.baobaotao\.service\..*"/>
    <context:exclude-filter type="aspectj"
        expression="com.baobaotao.util..*"/>
</context:component-scan>


值得注意的是 <context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。

默认情况下通过@Component定义的 Bean 都是 singleton 的,如果需要使用其它作用范围的 Bean,可以通过@Scope注释来达到目标,如以下代码所示:


清单 24. 通过 @Scope 指定 Bean 的作用范围
package com.baobaotao;
import org.springframework.context.annotation.Scope;

@Scope("prototype")
@Component("boss")
public class Boss {
    …
}


这样,当从 Spring 容器中获取bossBean 时,每次返回的都是新的实例了。


参考:http://hi.baidu.com/blue1255/item/ded2dc155c88b20ad0d66d85

分享到:
评论
3 楼 javadyl 2015-04-15  
简单 易懂
2 楼 wangxiao5530 2013-03-25  
讲的简单易懂,很好
1 楼 chenjinbo1983 2012-12-18  
好,谢谢,对我很有用

相关推荐

    Spring扫描器—spring组件扫描使用详解

    NULL 博文链接:https://gaozzsoft.iteye.com/blog/1523898

    springboot 基础简易实例, maven项目

    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt; &lt;version&gt;2.1.4.RELEASE&lt;/version&gt; &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt; &lt;/parent&gt; &lt;groupId&gt;com.example&lt;/groupId&gt; &lt;artifactId&gt;...

    springmvcmybatis

    &lt;context:component-scan base-package="com.flong.*" /&gt; &lt;!-- 引入配置文件 --&gt; &lt;bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; ...

    springweb3.0MVC注解(附实例)

    &lt;context:component-scan base-package="com.baobaotao.web"/&gt; &lt;!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 --&gt; &lt;bean class="org.springframework.web.servlet.mvc.annotation. ...

    spring applicationContext 配置文件

    &lt;context:component-scan base-package="com.ccc"/&gt; &lt;bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0" /&gt; &lt;!-- 配置事务管理器 針對MES數據庫-...

    struts hibernate spring 集成时使用依赖注解的方式的秘籍

    //applicationContext.xml文件中添加 &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=... &lt;context:component-scan base-package="com.haijian" /&gt;

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    &lt;context:component-scan base-package="com.mvc" /&gt; &lt;mvc:annotation-driven /&gt; &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt; &lt;mvc:default-servlet-handler /&gt; &lt;aop:config proxy-...

    springmvc-ibatis

    &lt;context:component-scan base-package="com.org"/&gt; &lt;bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="viewClass" value="org....

    springjdbc

    &lt;context:component-scan base-package="com.test.controller" /&gt; &lt;!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --&gt; &lt;bean class="org.springframework.web.servlet.mvc.annotation....

    spring3.2+strut2+hibernate4

    &lt;context:component-scan base-package="*" /&gt; &lt;!-- &lt;aop:config&gt;--&gt; &lt;!-- execution第一个星号代表任何返回类型,第二个星号代表com.sbz.service下的所有包,第三个星号代表所有方法,括号中的两个点代表任何...

    struts2.3+hibernate3.6+spring3.1整合的纯xml配置的小项目

    &lt;context:component-scan base-package="org.whvcse"&gt;&lt;/context:component-scan&gt; &lt;tx:annotation-driven transaction-manager="txManager" /&gt; &lt;!-- &lt;aop:config&gt; &lt;aop:pointcut id="defaultServiceOperation" ...

    基于MyEclipse搭建maven+springmvc整合图文教程(含源码0

    &lt;context:component-scan base-package="Controller" /&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/WEB-INF/views/" /&gt; ...

    spring-context-support-1.0.10-API文档-中文版.zip

    赠送jar包:spring-context-support-1.0.10.jar; 赠送原API文档:spring-context-support-1.0.10-javadoc.jar; 赠送源代码:spring-context-support-1.0.10-sources.jar; 赠送Maven依赖信息文件:spring-context-...

    quartz 定时任务

    &lt;context:component-scan base-package="cn.ly.quartz.service" /&gt; &lt;!-- job --&gt; &lt;bean id="helloJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"&gt; &lt;property name="job...

    SpringMVC+Hibernate实例

    &lt;context:component-scan base-package="com.bbs"/&gt; &lt;!--注解支持--&gt; &lt;mvc:annotation-driven/&gt; &lt;!--视图解析--&gt; &lt;bean id="viewResolver" class="org.springframework.web.servlet.view....

    维生药业小项目 SSH简单学习项目

    维生药业小项目 SSH简单学习项目 &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" ... &lt;context:component-scan base-package="com.sixth" /&gt; &lt;/beans&gt;

    Maven拆分代码.zip

    &lt;context:component-scan base-package="com.itheima.service"/&gt; &lt;!--aop面向切面编程,切面就是切入点和通知的组合--&gt; &lt;!--配置事务管理器--&gt; &lt;bean id="transactionManager" class="org.springframework.jdbc...

    SpringMVC+Hibernate全注解整合

    &lt;context:component-scan base-package="com.org.*" /&gt; &lt;bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="viewClass" value="org....

    Spring 报错:元素 "context:component-scan" 的前缀 "context" 未绑定的问题解决

    主要介绍了Spring 报错:元素 "context:component-scan" 的前缀 "context" 未绑定的问题解决的相关资料,需要的朋友可以参考下

Global site tag (gtag.js) - Google Analytics