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

14 篇文章 0 订阅
  正如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="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);   
   }
}
我 们来看看上面代码的含义,首先在代码 处我们分别定义了两个名为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="China Tianjin" />
  </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="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 中的 处我们分别定义了两个类 型为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、付费专栏及课程。

余额充值