当前位置:编程学习 > JAVA >>

Spring零配置通过注解实现Bean依赖注入总结

12.2  注解实现Bean依赖注入
12.2.1  概述
       注解实现Bean配置主要用来进行如依赖注入、生命周期回调方法定义等,不能消除XML文件中的Bean元数据定义,且基于XML配置中的依赖注入的数据将覆盖基于注解配置中的依赖注入的数据。
 
Spring3的基于注解实现Bean依赖注入支持如下三种注解:
• Spring自带依赖注入注解: Spring自带的一套依赖注入注解;
• JSR-250注解:Java平台的公共注解,是Java EE 5规范之一,在JDK6中默认包含这些注解,从Spring2.5开始支持。
• JSR-330注解:Java 依赖注入标准,Java EE 6规范之一,可能在加入到未来JDK版本,从Spring3开始支持;
• JPA注解:用于注入持久化上下文和尸体管理器。
 
这三种类型的注解在Spring3中都支持,类似于注解事务支持,想要使用这些注解需要在Spring容器中开启注解驱动支持,即使用如下配置方式开启:
 

Java代码    
1. <beans xmlns="http://www.springframework.org/schema/beans" 
2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
3.     xmlns:context="http://www.springframework.org/schema/context" 
4.     xsi:schemaLocation=" http://www.springframework.org/schema/beans  
5.        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
6.        http://www.springframework.org/schema/context  
7.        http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
8.  
9.     <context:annotation-config/>  
10.  
11. </beans>  
12.    
13.   
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation=" http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config/>

</beans>
 
 
   这样就能使用注解驱动依赖注入了,该配置文件位于“resources/ chapter12/dependecyInjectWithAnnotation.xml”。
 
12.2.2  Spring自带依赖注入注解
一、@Required:依赖检查;
对应于基于XML配置中的依赖检查,但XML配置的依赖检查将检查所有setter方法,详见【3.3.4  依赖检查】;
基于@Required的依赖检查表示注解的setter方法必须,即必须通过在XML配置中配置setter注入,如果没有配置在容器启动时会抛出异常从而保证在运行时不会遇到空指针异常,@Required只能放置在setter方法上,且通过XML配置的setter注入,可以使用如下方式来指定:
 

Java代码    
1. @Requried 
2. setter方法 
@Requried
setter方法
 
1、准备测试Bean
 

Java代码    
1. package cn.javass.spring.chapter12;  
2. public class TestBean {  
3.     private String message;  
4.     @Required 
5.     public void setMessage(String message) {  
6.         this.message = message;  
7.     }  
8.     public String getMessage() {  
9.         return message;  
10.     }  
11. } 
package cn.javass.spring.chapter12;
public class TestBean {
    private String message;
    @Required
    public void setMessage(String message) {
        this.message = message;
    }
    public String getMessage() {
        return message;
    }
}
 
 
2、在Spring配置文件(chapter12/dependecyInjectWithAnnotation.xml)添加如下Bean配置:
 

Java代码    
1. <bean id="testBean" class="cn.javass.spring.chapter12.TestBean">  
2. <property name="message" ref="message"/>  
3. </bean>  
4. <bean id="message" class="java.lang.String">  
5.     <constructor-arg index="0" value="hello"/>  
6. </bean> 
<bean id="testBean" class="cn.javass.spring.chapter12.TestBean">
<property name="message" ref="message"/>
</bean>
<bean id="message" class="java.lang.String">
    <constructor-arg index="0" value="hello"/>
</bean>
 
3、测试类和测试方法如下:
 

Java代码    
1. package cn.javass.spring.chapter12;  
2. //省略import  
3. public class DependencyInjectWithAnnotationTest {  
4.     private static String configLocation = "classpath:chapter12/dependecyInjectWithAnnotation.xml";  
5.     private static ApplicationContext ctx = new ClassPathXmlApplicationContext("configLocation");  
6.     //1、Spring自带依赖注入注解  
7.      @Test 
8.     public void testRequiredForXmlSetterInject() {  
9.         TestBean testBean = ctx.getBean("testBean", TestBean.class);  
10.         Assert.assertEquals("hello", testBean.getMessage());  
11.     }  
12. } 
package cn.javass.spring.chapter12;
//省略import
public class DependencyInjectWithAnnotationTest {
    private static String configLocation = "classpath:chapter12/dependecyInjectWithAnnotation.xml";
    private static ApplicationContext ctx = new ClassPathXmlApplicationContext("configLocation");
    //1、Spring自带依赖注入注解
     @Test
    public void testRequiredForXmlSetterInject() {
        TestBean testBean = ctx.getBean("testBean", TestBean.class);
        Assert.assertEquals("hello", testBean.getMessage());
    }
}
 
在XML配置文件中必须指定setter注入,否则在Spring容器启动时将抛出如下异常:
 

Java代码    
1. org.springframework.beans.factory.BeanCreationException:  
2. Error creating bean with

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,