zyskm用梦想丈量人生,用奔跑丈量激情

spring3系列 二、Spring配置项解释说明

这篇也是转载,改了中间部分内。

在基于注解方式配置
Spring的配置文件中,你可能会见到<context:annotation-config/>这样一条配置,他的作用是式地向 Spring 容器注册

AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor

PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor  4 BeanPostProcessor

注册这4 BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。

例如:

如果你想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。传统声明方式如下:

  1. <bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/> 

如果想使用@ Resource @ PostConstruct@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor

如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessorBean

如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessorBean。同样,传统的声明方式如下:

  1. <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> 

一般来说,这些注解我们还是比较常用,尤其是Antowired的注解,在自动注入的时候更是经常使用,所以如果总是需要按照传统的方式一条一条配置显得有些繁琐和没有必要,于是spring给我们提供<context:annotation-config/>的简化配置方式,自动帮你完成声明。

   不过,呵呵,我们使用注解一般都会配置扫描包路径选项

  1. <context:component-scan base-package=”XX.XX”/> 

    该配置项其实也包含了自动注入上述processor的功能,因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。

本文转载:http://mushiqianmeng.blog.51cto.com/3970029/723880

posted on 2012-04-13 15:14 zyskm 阅读(2561) 评论(4)  编辑  收藏

评论

# re: spring3系列 二、Spring配置项解释说明 2012-04-13 15:50 周转箱模具

博客越来越少了……博主你一定要顶住啊!我支持你!  回复  更多评论   

# re: spring3系列 二、Spring配置项解释说明 2012-04-25 17:00 路人甲

楼主,你知道spring怎么配置或者怎么实现,在使用serviceBean的时候,只需要在action里建一个serviceBean的属性,就可以直接使用,而不用通过spring的bean工厂去getBean(“xxx”),如下代码

。。。

private UserInfoManagement userInfoManagement;

public String loginSys(){
UUserInfo user = userInfoManagement.getUserByName(username);

。。。  回复  更多评论   

# re: spring3系列 二、Spring配置项解释说明 2012-04-26 16:10 zyskm

自动装配一共两种方式实现:
1.在xml显示配置;
2.通过注解方式实现,如在serviceBean增加@Service("xxxServiceBean"),在action中就可以实现自动装配。@Component("xxxServiceBean")也可以,@service更准确些。@路人甲
  回复  更多评论   

# re: spring3系列 二、Spring配置项解释说明 2012-05-23 13:29 瘦腿精油

Spring很强大  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: