`
crud0906
  • 浏览: 134760 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Spring <context:component-scan base-package="">和<context:annotation-config>

阅读更多
Spring2.5中使用注解装配属性
可在Java代码中使用@Resource或者@Autowired注解进行装配,但需在XML中配置以下信息
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
然后显式的配置<context:annotation-config/>
该配置隐式注册了多个对注解进行解析的处理器,如下列举
AutowiredAnnotationBeanPostProcessor      CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor    RequiredAnnotationBeanPostProcessor
其实,注解本身做不了任何事情,和XML一样,只起到配置的作用,主要在于背后强大的处理器


另外,比较建议使用@Resource注解,而不要使用@Autowired注解
因为@Autowired注解是Spring提供的,而@Resource注解是J2EE提供的
在JDK6中就已经包含@Resource注解了,所以它没有跟Spring紧密耦合
并且在使用Spring时,若使用了JSR-250中的注解,如@Resource//@PostConstruct//@PreDestroy
那么还需要Spring安装目录中的SPRING_HOME\\lib\\j2ee\\common-annotations.jar包的支持
这里面的@Resource注解就是在SPRING_HOME\\lib\\j2ee\\common-annotations.jar中的

@Resource注解
@Resource注解和@Autowired一样,也可以标注在字段或属性的setter方法上
@Resource默认按名称装配,名称可以通过name属性指定。当找不到与名称匹配的bean时,才会按类型装配
若注解标注在字段上且未指定name属性,则默认取字段名作为bean名称寻找依赖对象
若注解标注在setter上且未指定name属性,则默认取属性名作为bean名称寻找依赖对象
如果没有指定name属性,并且按照默认的名称仍找不到依赖对象时,它就会按类型匹配
但只要指定了name属性,就只能按名称装配了

@Autowired注解
@Autowired默认是按类型装配对象的,默认情况下它要求依赖对象必须存在
如果允许null值,可以设置它的required属性为FALSE,如@Autowired(required=false)
若想要按名称装配,可以结合@Qualifier注解一起使用,如@Autowired(required=false)  @Qualifier("personDaoBean")


--------------------------------------------------------------------------------

Spring2.5的组件自动扫描
在一个稍大的项目中通常会有上百个组件,如果都使用XML的bean定义来配置组件的话
显然会增加配置文件的体积,查找及维护也不方便
而Spring2.5就为我们引入了组件自动扫描机制
它可以在classpath下寻找标注了@Service、@Repository、@Controller、@Component注解的类
并把这些类纳入Spring容器中管理,它的作用和在XML中使用bean节点配置组件是一样的
使用自动扫描机制,则需配置<context:component-scan base-package="com.jadyer"/>启动自动扫描
其中base-package指定需要扫描的包,它会扫描指定包中的类和子包里面类
@Service用于标注业务层组件
@Repository用于标注数据访问组件,即DAO组件
@Controller用于标注控制层组件,如Struts中的Action
@Component泛指组件,当组件不要好归类时,可以使用这个注解进行标注

1、可以使用诸如@Service("personDao")修改bean名称,而它默认的是将首字母小写的类名作为<bean>名称
2、若要更改<bean>作用域的话,可以使用@Scope("prototype")注解来修改<bean>作用域
3、若想让<bean>实例化之后去执行初始化方法,可以使用@PostConstruct标注在方法上
4、同样@PreDestroy注解标注在方法上,可以用来指定<bean>销毁时执行的方法
这里的@PostConstruct是EJB3里面用来初始化bean的注解,它也不是Spring中的注解
并且<context:component-scan base-package=""/>的背后注册了很多用于解析注解的处理器
其中就包括了<context:annotation-config/>配置项里面的注解所使用的处理器
所以配置了<context:component-scan base-package="">之后,便无需再配置<context:annotation-config>



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/jadyer/archive/2010/11/27/6038604.aspx
分享到:
评论

相关推荐

    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" ...

    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....

    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數據庫-...

    spring3.2+strut2+hibernate4

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

    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....

    维生药业小项目 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;

    Spring AOP配置源码

    &lt;context:component-scan base-package="com.spring.*"/&gt; &lt;aop:config&gt; &lt;aop:aspect id="aspect" ref="logIntercepter"&gt; &lt;aop:pointcut expression="execution(* com.spring.service..*(..))" id="pointCut"/&gt; ...

    Spring Context测试

    学习spring组件扫描(Component Scanning)的代码 使用方法:直接把工程导入,直接Run ...&lt;context:component-scan base-package="com.test"&gt;&lt;/context:component-scan&gt; 2.在需要装配的类的上面全部加上@Component

    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-SSH全注解

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

    spring_MVC源码

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

    轻量级高性能 RPC 框架 HRPC.zip

     &lt;context:component-scan base-package="com.yingjun.rpc.service.impl"/&gt;  &lt;context:property-placeholder location="classpath:system.properties"/&gt;  &lt;!--服务端配置--&gt;  &lt;bean id="rpcServer" ...

    java微信公众号MVC开发框架

    spring配置文件中唯一需要配置的bean是WeixinConfigurer类,&lt;context:component-scan base-package="com.github.jweixin.jwx.weixin.service" /&gt;是可选配置,但里面封装了微信接口服务类,建议一定要配置进spring...

    客户关系管理系统框架搭建(二)

    &lt;context:component-scan base-package="cn.itcast.crm"/&gt; &lt;!--3 配置本地化代理工程bean,这是spring整合hibernate的入口 --&gt; &lt;bean id="sessionFactory" class="org.springframework.orm....

    springmybatis

    查询出列表,也就是返回list, 在我们这个例子中也就是 List&lt;User&gt; , 这种方式返回数据,需要在User.xml 里面配置返回的类型 resultMap, 注意不是 resultType, 而这个resultMap 所对应的应该是我们自己配置的 ...

Global site tag (gtag.js) - Google Analytics