http://tonyaction.blog.51cto.com/227462/107451
Spring2.5
注释语法(
上)
——Spring2.5
注释驱动的IoC
正如Spring
框架的名称一样,它真的为我们JavaEE
开发带来了一阵春风,而其著名的IoC (
依赖注入)
更是为它的经久不衰注入了无限活力。在新发布的2.5
版本中Spring
的开发者为我们这些使用Java5
或更高版本Java
的开发人员提供了进一步简化而强大的新特性。其中就包括了注释驱动的依赖性注入(annotation-driven dependency injection
)和新的web
控制器模型将请求映射到加注释的方法上,等等。
今天我首先要向大家介绍的就是Spring2.5
中新特性之一---
注释驱动的依赖性注入,在正式介绍之前我想先让大家了解一下什么是注释,请看下面的代码清单:
一、了解Java
注释
代码清单1.1
import
java.lang.annotation.Retention;
import
java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME
)
public
@interface
MyAnnotation {
①定义一个注释
int
init();
}
代码清单1.2
import
java.lang.annotation.Annotation;
public
class
TestAnnotation {
@MyAnnotation(init = 2)
②使用注释
public
void
print(){
System.out
.println(TestAnnotation.class
.getName());
}
public
static
void
main(String[] args) throws
Exception{
TestAnnotation ta = new
TestAnnotation();
Annotation[] annotations = ta.getClass().getMethod("print").getAnnotations();
③
for
(Annotation annotation : annotations)
{
System.out
.println("MyAnnotation.init : " +
((MyAnnotation)annotation).init());
④打印出init
的值
}
}
}
在代码清单1.1
中我们定义了一个名为MyAnnotation
的注释而这个注释中只有一个类型为int
名为init
的属性,代码清单1.2
中我们在②
处使用了我们刚刚定义的注释并且为init
赋值为2
,在③
处我们通过反射机制获得print
方法上定义的所有注释然后通过迭代将其值init
打印至控制台。
最终在控制台输出以下信息:
MyAnnotation.init : 2
至此我们对Java
中的注释有了一个简单的了解,下面我们来看看Spring2.5
是如何使用这些注释来完成它神奇的IoC
功能吧。在此之前我们先来看看以前我们是怎么做的。
二、Spring
的IoC
代码清单2.1
public class Foo {
①
private
String name;
private
int
age;
public
String toString(){
return
"The Foo's Name is : " + this
.name + " The Foo's Age is : " + this
.age;
}
public
String getName() {...}
public
void
setName(String name) {...}
public
int
getAge() {...}
public
void
setAge(int
age) {...}
}
代码清单2.2
public class Bar {
②
private
String address;
public
String toString(){
return
"The Bar's Address is : " + this
.address;
}
public
String getAddress() {...}
public
void
setAddress(String address) {...}
}
代码清单2.3
public class Base {
③
private
Foo foo;
private
Bar bar;
public
String toString(){
return
"Base : [" + this
.foo.toString() +" "+ this
.bar.toString()+ "]";
}
public
Foo getFoo() {...}
public
void
setFoo(Foo foo) {...}
public
Bar getBar() {...}
public
void
setBar(Bar bar) {...}
}
代码清单2.4
<?xml version="1.0"
encoding="UTF-8"
?>
<beans xmlns="..."
>
<bean id="foo"
class="com.tony.test.Foo"
>
<property name="name"
value="Tony"
/>
<property name="age"
value="27"
/>
</bean >
<bean id="bar"
class="com.tony.test.Bar"
>
<property name="address"
value="China Tianjin"
/>
</bean>
<bean id="base"
class="com.tony.test.Base"
>
<property name="foo"
>
<ref local="foo"
/>
</property>
<property name="bar"
>
<ref local="bar"
/>
</property>
</bean>
</beans>
代码清单2.5
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainClass {
④
public
static
void
main(String[] args) {
String[] locations = {"spring-config-beans.xml"};
ApplicationContext ctx = new
ClassPathXmlApplicationContext(locations);
Base main = (Base) ctx.getBean("base");
⑤
System.out.println(main);
⑥
}
}
我们来看看上面代码的含义,首先在代码①
和②
处我们分别定义了两个名为Foo
和Bar
的Bean
,在③
处我们通过set
方法将两个Bean
注入进Base
类中,并且在Base
类中定义了toString
方法来打印出Foo
和Bar
的信息,
在④
处我们定义了一个MainClass
来执行我们的代码,
在⑤
处我们通过getBean
获得配置文件中配置的id
为base
的Bean
并在⑥
出将其信息打印至控制台,
控制台输出信息如下:
Base : [The Foo's Name is : Tony The Foo's Age is : 27 The Bar's Address is : China Tianjin]
看到上面习以为常的配置信息和set get
方法我们根本不会有任何想法,可是当我们看到了Spring2.5
注释特性的时候我们发现自己真的错了,程序竟然还可以写成这么简单。
三、使用@Autowired
注释
经过了一番整理我们把改好了。
代码清单3.1
import
org.springframework.beans.factory.annotation.Autowired;
public
class
Base {
@Autowired
① 使用了一个名为Autowired
的注释
private
Foo foo;
@Autowired
②
private
Bar bar;
public
String toString(){
return
"Base : [" + this
.foo.toString() +" "+ this
.bar.toString()+ "]";
}
}
代码清单3.2
<!--
该 BeanPostProcessor
将自动对标注 @Autowired
的 Bean
进行注入 -->
③
<bean class="org.springframework.beans.factory.annotation.
AutowiredAnnotationBeanPostProcessor"
/>
<bean id="foo"
class="com.tony.test.Foo"
>
<property name="name"
value="Tony"
/>
<property name="age"
value="27"
/>
</bean>
<bean id="bar"
class="com.tony.test.Bar"
>
<property name="address"
value="China Tianjin"
/>
</bean>
<!--
此时移除了Base
的配置信息 -->
④
<bean id="base"
class="com.tony.test.Base"
/>
以上的代码清单中我们在①
和②
处使用了
@Autowired
注释,
它可以对类的成员变量、方法及构造函数进行标注,完成自动装配的工作,在③
处我们为了使
@Autowired
注释
生效必须在Spring
容器中声明
AutowiredAnnotationBeanPostProcessor
Bean
它通过
扫描 Spring
容器中所有 Bean
,当发现 Bean
中拥有
@Autowired
注释时就找到和其相匹配(默认按类型匹配)的 Bean
,并将其注入
,
而此时我们在声明Base
的时候(
④
处)
就不用写它的配置信息了,更可以将Base
类中的set
和get
方法删除。
@Autowired
还可以通过类的构造函数来进行自动装配。
代码清单3.3
import
org.springframework.beans.factory.annotation.Autowired;
public
class
Base {
private
Foo foo;
private
Bar bar;
@Autowired
public
Base(Foo foo,Bar bar){
①
this
.foo = foo;
this
.bar = bar;
}
public
String toString(){
return
"Base : [" + this
.foo.toString() +" "+ this
.bar.toString()+ "]";
}
}
在代码清单3.3
中我们增加了一个构造函数,通过它来对我们的成员变量进行赋值,我们同时也为这个构造函数添加了@Autowired
注释(
①
处)使其可以自动将Foo
和Bar
两个成员变量装配进来。
四、使用@Qualifier
注释
有时我们会遇到这样一种情况,我们定义了两个类型相同数据不同的Bean
,我们此时需要用到其中一个Bean
来供我们使用,使用@Qualifier
注释就可以满足我们的要求,当使用@Qualifier
注释时
自动注入的策略就从 byType
转变成 byName
了。
代码清单4.1
<bean id="bar"
class="com.tony.test.Bar"
>
①
<property name="address"
value="China Tianjin"
/>
</bean>
<bean id="bar2"
class="com.tony.test.Bar"
>
②
<property name="address"
value="China Beijing"
/>
</bean>
代码清单4.2
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.beans.factory.annotation.Qualifier;
public
class
Base {
private
Bar bar;
@Autowired
③
public
Base(@Qualifier("bar2")Bar bar){
④
this
.bar = bar;
}
public
String toString(){
return
"Base : ["+ this
.bar.toString()+ "]";
}
}
代码清单4.1
中的
①
和②
处我们分别定义了两个类型为Bar
的Bean
,②
处Bar
的address
为China Beijing
并且Bean
的名称为bar2
,在代码清单4.2
的③
处我们同样使用了@Autowired
注释为Bar
的构造函数进行自动装配,可是在④
处我们通过@Qualifier("bar2")
来明确指定我们需要将id
为bar2
的Bean
装配进来。我们还可以为成员变量使用@Qualifier
注释。
代码清单4.3
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.beans.factory.annotation.Qualifier;
public
class
Base {
@Autowired
①
@Qualifier("bar2")
②
private
Bar bar;
public
String toString(){
return
"Base : ["+ this
.bar.toString()+ "]";
}
}
代码清单4.3
中我们为成员变量bar
添加了两个注释①
处是我们已经很熟悉的@Autowired
注释,紧随其后的就是@Qualifier
注释。
@Qualifier
注释可以对成员变量、方法入参、构造函数入参进行标注,而我们最常用也最方便的就是为成员变量使用注释。
五、使用@Component
注释
使用了@Autowired
注释后我们发现自动注入真的非常简单,但是我们还是得在配置文件中定义相应的<Bean>
,如果我们能在配置文件中完全移除Bean
的定义那就更好了,Spring2.5
就为我们提供了这一可能。
代码清单5.1
import
org.springframework.stereotype.Component;
@Component
①
public
class
Bar {
private
String address = "China Tianjin";
public
String toString(){
return
"The Bar's Address is : " + this
.address;
}
}
代码清单5.2
import
org.springframework.stereotype.Component;
@Component("base")
②
public
class
Base {
@Resource
private
Bar bar;
public
String
toString(){
return "Base : ["+ this.bar.toString()+ "]";
}
}
代码清单5.3
<?xml version="1.0"
encoding="UTF-8"
?>
<beans xmlns="..."
>
<context:component-scan base-package="com.tony.test"/>
③
</beans>
在代码清单5.1
中我们使用了一个
@Component
注释(①
处),
使用
@Component
注释就可以将一个类定义成为Spring
容器中的 Bean
。在代码清单5.2
的
②
处我们也同样使用了@Component
注释,而此时我们使用了它提供的一个可选的入参将Bean
的名称定义为base
,最后在③
处我们将以前定义Bean
的内容全部移除,添加了一行
<context:component-scan/>
,其中的
base-package
属性
指定了需要扫描的类包,它会自动递归下面的子包。
六、使用@Scope
注释
代码清单6.1
import
org.springframework.beans.factory.annotation.Autowired; import
org.springframework.context.annotation.Scope;
import
org.springframework.stereotype.Component;
@Scope("prototype")
①
@Component("base")
public
class
Base {
@Autowired
private
Bar bar;
public
String toString(){
return
"Base : ["+ this
.bar.toString()+ "]";
}
}
在代码清单6.1 中的① 处我们添加了一个@Scope 注释,这样当我们从 Spring 容器中获取 base 时,每次返回的都是一个新的实例了。
另外参见:http://www.infoq.com/cn/articles/spring-2.5-part-1