Spring2.5注释语法(上)——Spring2.5注释驱动的IoC - 绝缘材料 - ...
来源:百度文库 编辑:神马文学网 时间:2024/10/03 02:55:21
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 "1.0" encoding="UTF-8"?>"..."> "foo" class="com.tony.test.Foo"> "name" value="Tony"/> "age" value="27"/> "bar" class="com.tony.test.Bar"> "address" value="China Tianjin "/> "base" class="com.tony.test.Base"> "foo"> "foo"/> "bar"> "bar"/> 代码清单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 ③ "org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor"/> "foo" class="com.tony.test.Foo"> "name" value="Tony"/> "age" value="27"/> "bar" class="com.tony.test.Bar"> "address" value="China Tianjin "/> ④ "base" class="com.tony.test.Base"/> 以上的代码清单中我们在①和②处使用了@Autowired注释,它可以对类的成员变量、方法及构造函数进行标注,完成自动装配的工作,在③处我们为了使@Autowired注释生效必须在Spring容器中声明AutowiredAnnotationBeanPostProcessor Bean它通过扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 代码清单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"bar" class="com.tony.test.Bar"> ① "address" value="China Tianjin "/> "bar2" class="com.tony.test.Bar"> ② "address" value="China Beijing "/> 代码清单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注释。 五、使用@Component注释 使用了@Autowired注释后我们发现自动注入真的非常简单,但是我们还是得在配置文件中定义相应的,如果我们能在配置文件中完全移除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 "1.0" encoding="UTF-8"?>"..."> ③ 在代码清单5.1中我们使用了一个@Component注释(①处),使用 ,其中的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 时,每次返回的都是一个新的实例了。
@Autowired
注释时就找到和其相匹配(默认按类型匹配)的 Bean,并将其注入,而此时我们在声明Base的时候(④处)就不用写它的配置信息了,更可以将Base类中的set和get方法删除。@Autowired还可以通过类的构造函数来进行自动装配。@Qualifier
注释可以对成员变量、方法入参、构造函数入参进行标注,而我们最常用也最方便的就是为成员变量使用注释。@Component
注释就可以将一个类定义成为Spring 容器中的 Bean。在代码清单5.2的②处我们也同样使用了@Component注释,而此时我们使用了它提供的一个可选的入参将Bean的名称定义为base,最后在③处我们将以前定义Bean的内容全部移除,添加了一行本文出自 “绝缘材料” 博客,转载请与作者联系!
本文出自 51CTO.COM技术博客
Spring2.5注释语法(上)——Spring2.5注释驱动的IoC - 绝缘材料 - ...
Spring2.5注释语法(上)——Spring2.5注释驱动的IoC - 绝缘材料 dfsdfsdf
Spring2.5的新特性:第一部分
InfoQ: Spring2.5的新特性:第一部分
Spring2.5 访问 Session 属性的四种策略
spring2.5事务配置5种方式
Struts2.1.6+Spring2.5+Hibernate3.2
Spring2.5、Struts2、Ibatis开发框架
Joan's javaworld - Struts2+Spring2.5+Hibernat...
Struts2.1.6+Spring2.5+Hibernate3.2qqq
ActiveMQ4.1 +Spring2.0的POJO JMS方案(上)
Spring2.X的 AOP支持
Spring2.5、Struts2、Ibatis开发框架搭建(一1)
守弱学——带注释
医学三字经—附注释
医学三字经—附注释
spring2.0和AOP
《尚书》全注释 的日志 — Windows Live
Spring2.X中AOP的使用 - 边城愚人 - BlogJava
Spring2.X中AOP的使用 - 边城愚人 - BlogJava
struts2.1.8 + spring2.5.6 + hibernate3.3.2整合的..qqq.
情人的注释
各种语言的注释
集装箱的尺寸注释