码迷,mamicode.com
首页 > 编程语言 > 详细

Spring - Annotation Based Configuration

时间:2017-07-18 23:13:19      阅读:320      评论:0      收藏:0      [点我收藏+]

标签:field   rom   setter   case   pretty   pen   ons   3.0   ase   

Starting from Spring 2.5 it became possible to configure the dependency injection using annotations. So instead of using XML to describe a bean wiring, you can move the bean configuration into the component class itself by using annotations on the relevant class, method, or field declaration.

Annotation injection is performed before XML injection. Thus, the latter configuration will override the former for properties wired through both approaches.

Annotation wiring is not turned on in the Spring container by default. So, before we can use annotation-based wiring, we will need to enable it in our Spring configuration file. So consider the following configuration file in case you want to use any annotation in your Spring application.

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

   <context:annotation-config/>
   <!-- bean definitions go here -->

</beans>

Once <context:annotation-config/> is configured, you can start annotating your code to indicate that Spring should automatically wire values into properties, methods, and constructors. Let us look at a few important annotations to understand how they work ?

Sr.No.Annotation & Description
1 @Required

The @Required annotation applies to bean property setter methods.

2 @Autowired

The @Autowired annotation can apply to bean property setter methods, non-setter methods, constructor and properties.

3 @Qualifier

The @Qualifier annotation along with @Autowired can be used to remove the confusion by specifiying which exact bean will be wired.

4 JSR-250 Annotations

Spring supports JSR-250 based annotations which include @Resource, @PostConstruct and @PreDestroy annotations.

Spring - Annotation Based Configuration

标签:field   rom   setter   case   pretty   pen   ons   3.0   ase   

原文地址:http://www.cnblogs.com/liandy0906/p/7203195.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!