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

来源:互联网 发布:嵌入式软件培训机构 编辑:程序博客网 时间:2024/04/28 11:17

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;
publicclass 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;
publicclass 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;
publicclass 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;
publicclass 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;
publicclass 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
publicclass 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")
publicclass 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")
publicclass Base {
  @Autowired
 private Bar bar;
 public String toString(){
    return "Base : ["+ this.bar.toString()+ "]";
  }
}
在代码清单6.1中的处我们添加了一个@Scope注释,这样当我们从 Spring 容器中获取 base 时,每次返回的都是一个新的实例了。

原创粉丝点击