Spring2.5注释语法(上)——Spring2.5注释驱动的IoC

Spring2.5 注释语法()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

            ——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功能吧。在此之前我们先来看看以前我们是怎么做的。

二、SpringIoC

代码清单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="<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />ChinaTianjin"/>

  </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);   

   }

}

我们来看看上面代码的含义,首先在代码处我们分别定义了两个名为FooBarBean,在处我们通过set方法将两个Bean注入进Base类中,并且在Base类中定义了toString方法来打印出FooBar的信息,处我们定义了一个MainClass来执行我们的代码,处我们通过getBean获得配置文件中配置的idbaseBean并在出将其信息打印至控制台,控制台输出信息如下:

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="ChinaTianjin"/>

  </bean>

  <!-- 此时移除了Base的配置信息 -->

  <bean id="base" class="com.tony.test.Base"/>

以上的代码清单中我们在处使用了 @Autowired 注释, 它可以对类的成员变量、方法及构造函数进行标注,完成自动装配的工作,在处我们为了使 @Autowired 注释 生效必须在Spring容器中声明 AutowiredAnnotationBeanPostProcessor Bean 它通过 扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired 注释时就找到和其相匹配(默认按类型匹配)的 Bean,并将其注入 , 而此时我们在声明Base的时候()就不用写它的配置信息了,更可以将Base类中的setget方法删除。 @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注释( 处)使其可以自动将FooBar两个成员变量装配进来。

四、使用@Qualifier注释

有时我们会遇到这样一种情况,我们定义了两个类型相同数据不同的Bean,我们此时需要用到其中一个Bean来供我们使用,使用@Qualifier注释就可以满足我们的要求,当使用@Qualifier注释时 自动注入的策略就从 byType 转变成 byName 了。

代码清单4.1

<bean id="bar" class="com.tony.test.Bar">

  <property name="address" value="ChinaTianjin"/>

</bean>

<bean id="bar2" class="com.tony.test.Bar">

  <property name="address" value="ChinaBeijing"/>

</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中的 处我们分别定义了两个类型为BarBeanBaraddressChina Beijing并且Bean的名称为bar2,在代码清单4.2处我们同样使用了@Autowired注释为Bar的构造函数进行自动装配,可是在处我们通过@Qualifier("bar2")来明确指定我们需要将idbar2Bean装配进来。我们还可以为成员变量使用@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 时,每次返回的都是一个新的实例了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值