首页 > 编程语言 > 详细

从 Spring 2.5 开始就可以使用注解来配置依赖注入,而不是采用 XML 来描述一个 bean。

时间:2015-12-17 12:18:11      阅读:292      评论:0      收藏:0      [点我收藏+]

1、在 XML 注入之前进行注解注入,因此后者可以被前者重写。

2、在默认情况下注解在 Spring 容器中不打开,需要配置启动。

<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-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:annotation-config/>

</beans>

3、注解的位置有属性、方法和构造函数。

  • @Required 注解应用于 bean 属性的 setter 方法。
  • @Autowired 注解可以应用到 bean 属性的 setter 方法,非 setter 方法,构造函数和属性。
  • @Autowired 和 @Qualifier 注解通过指定确切的bean,可以用来放置混乱。
  • Spring 支持 JSR-250 的基础的注解,其中包括了 @Resource,@PostConstruct 和 @PreDestroy 注解。

 

从 Spring 2.5 开始就可以使用注解来配置依赖注入,而不是采用 XML 来描述一个 bean。

原文:http://www.cnblogs.com/wangdapeng/p/5053483.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!