Spring--Java注解注入

   基于Java配置选项,可以编写大多数的Spring不用配置XML,但有几个基于Java的注释的帮助下解释。从Spring3.0开始支持使用java代码来代替XML来配置Spring,基于Java配置Spring依靠Spring的JavaConfig项目提供的很多优点。通过使用@Configuration, @Bean ,@Importand,@DependsOnannotations来实现Java的配置Spring.

本文工程下载

@Configuration & @Bean 注解:

         在Spring的新的Java-Configuration的中间产物是基于类的@Configuration的注解和基于方法的@Bean注解。
         @Bean注解是用来指明方法的实例化,配置和初始化一个对象是通过Spring的IoC容器来管理的。对于那些熟悉使用以XML配置Spring的<beans /> 标签,@Bean注解和<bean />标签是起相同作用的。你能和Spring的@Component注解的组件一起使用@Bean注解方法, 然而,这些@Bean注解的方法通常是和@Configuration的Bean。
        @Configuration注解的类指明该类主要是作为一个bean的来源定义。此外,@Configurationd定义的classes允许在同一个类中使用@Bean定义的方法来定义依赖的bean 

注释类与@Configuration表示这个类可以使用Spring IoC容器为bean定义的来源。在@Bean 注解告诉Spring的注解为@Bean的一个方法将返回应注册为在Spring应用程序上下文中的bean对象。最简单可行的@Configuration类将如下所示: 

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. @Configuration  
  2. public class CompanyConfig {  
  3.     @Bean  
  4.     public Employee employee(){  
  5.         return new Employee();  
  6.     }  
  7.   
  8. }  


上面的代码将等同于下面的XML配置:

 

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <bean id="companyConfig" class="com.mucfc.anno.CompanyConfig"/>  

下面注解为@Bean的方法名称作为工作bean的id,它创建并返回实际的bean。配置类可以有声明多个@Bean。一旦配置类定义,可以加载和提供他们使用AnnotationConfigApplicationContext 如下,以Spring容器:

 

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. public static void main(String[] args) {  
  2.     ApplicationContext ctx= new AnnotationConfigApplicationContext(CompanyConfig.class);  
  3.     Employee employee=ctx.getBean(Employee.class);  
  4.     employee.setName("笨笨");  
  5.     employee.setId(2012);  
  6.     System.out.println(employee);  
  7.   
  8. }  

也可以这样写:

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. package com.mucfc.anno;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. public class Test {  
  8.     public static void main(String[] args) {  
  9.   
  10.         AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();  
  11.         ctx.register(CompanyConfig.class);  
  12.         ctx.refresh();    
  13.         Employee employee1 = ctx.getBean(Employee.class);  
  14.         Employee employee2 = ctx.getBean(Employee.class);  
  15.           
  16.         employee1.setName("笨笨");  
  17.         employee1.setId(2012);  
  18.           
  19.         employee2.setName("狂人");  
  20.         employee2.setId(34546);  
  21.           
  22.         System.out.println("exployee1"+employee1);  
  23.         System.out.println("exployee2"+employee2);  
  24.         System.out.print("employee1==employee2?:");  
  25.         System.out.print(employee1==employee2);  
  26.   
  27.     }  
  28.   
  29. }  

 

 


把上面的@Bean加个@scope
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. package com.mucfc.anno;  
  2.   
  3. import org.springframework.context.annotation.Bean;  
  4. import org.springframework.context.annotation.Configuration;  
  5. import org.springframework.context.annotation.Scope;  
  6.   
  7. @Configuration  
  8. public class CompanyConfig {  
  9.     @Bean  
  10.     @Scope("singleton")  
  11.     public Employee employee(){  
  12.         return new Employee();  
  13.     }  
  14.   
  15. }  
其它不变。运行结果:

 

说明默认返回的是singleton类型,这里改成了prototpye

这么做有什么好处呢?

     1.使用纯java代码,不在需要xml

     2.在配置中也可享受OO带来的好处

     3.类型安全对重构也能提供良好的支持

     4.依旧能享受到所有springIoC容器提供的功能

        ApplicationContext 接口的最常用的实现类是 ClassPathXmlApplicationContext 和 FileSystemXmlApplicationContext,以及面向 Portlet 的 XmlPortletApplicationContext 和面向 web 的 XmlWebApplicationContext,它们都是面向 XML 的。Spring 3.0 新增了另外两个实现类:AnnotationConfigApplicationContext 和 AnnotationConfigWebApplicationContext。从名字便可以看出,它们是为注解而生,直接依赖于注解作为容器配置信息来 源的 IoC 容器初始化类。由于 AnnotationConfigWebApplicationContext 是 AnnotationConfigApplicationContext 的 web 版本,其用法与后者相比几乎没有什么差别,因此本文将以 AnnotationConfigApplicationContext 为例进行讲解。
        AnnotationConfigApplicationContext 搭配上 @Configuration 和 @Bean 注解,自此,XML 配置方式不再是 Spring IoC 容器的唯一配置方式。两者在一定范围内存在着竞争的关系,但是它们在大多数情况下还是相互协作的关系,两者的结合使得 Spring IoC 容器的配置更简单,更强大。之前,我们将配置信息集中写在 XML 中,如今使用注解,配置信息的载体由 XML 文件转移到了 Java 类中。我们通常将用于存放配置信息的类的类名以 “Config” 结尾,比如 AppDaoConfig.java、AppServiceConfig.java 等等。我们需要在用于指定配置信息的类上加上 @Configuration 注解,以明确指出该类是 Bean 配置的信息源。

注意:Spring 对标注 Configuration 的类有如下要求
配置类不能是 final 的;配置类不能是本地化的,亦即不能将配置类定义在其他类的方法内部;配置类必须有一个无参构造函数。AnnotationConfigApplicationContext 将配置类中标注了 @Bean 的方法的返回值识别为 Spring Bean,并注册到容器中,受 IoC 容器管理。@Bean 的作用等价于 XML 配置中的 标签。

 

@Bean 具有以下四个属性:  
name -- 指定一个或者多个 Bean 的名字。这等价于 XML 配置中 的 name 属性。

initMethod -- 容器在初始化完 Bean 之后,会调用该属性指定的方法。这等价于 XML 配置中 的 init-method 属性。

destroyMethod -- 该属性与 initMethod 功能相似,在容器销毁 Bean 之前,会调用该属性指定的方法。这等价于 XML 配置中 的 destroy-method 属性。

autowire -- 指定 Bean 属性的自动装配策略,取值是 Autowire 类型的三个静态属性。Autowire.BY_NAME,Autowire.BY_TYPE,Autowire.NO。与 XML 配置中的 autowire 属性的取值相比,这里少了 constructor,这是因为 constructor 在这里已经没有意义了。@Bean 没有直接提供指定作用域的属性,可以通过 @Scope 来实现该功能。

 

由于@configureation注解类本身已经标注了@component注解,所以任何标注了@configuration的类,本身也相当于标注了@component,即它们可以像普通的bean一样被注入到其它bean中去。

如下面:

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. package com.mucfc.anno;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.context.annotation.Configuration;  
  5.   
  6. @Configuration  
  7. public class Company {  
  8.     @Autowired  
  9.     //自动装配Configuration定义好的 CompanyConfig   
  10.     private CompanyConfig companyConfig;  
  11.   
  12.     public CompanyConfig getCompanyConfig() {  
  13.         return companyConfig;  
  14.     }  
  15.   
  16.     public void setCompanyConfig(CompanyConfig companyConfig) {  
  17.         this.companyConfig = companyConfig;  
  18.     }  
  19.   
  20. }  

使用:

 

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. ctx.register(CompanyConfig.class);  
  2.     ctx.register(Company.class);  
  3.     ctx.refresh();    
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. Employee employee3 = ctx.getBean(Company.class).getCompanyConfig().employee();  
  2.         employee3.setName("红色革命");  
  3.         employee3.setId(2342);  
  4.         System.out.println("exployee3"+employee3);  


输出结果:

 

exployee3员工姓名:红色革命  员工编号 :2342

 

使用基于java类的配置信息启动Spring容器:

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. /*  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); 
  2.         ctx.register(CompanyConfig.class); 
  3.         ctx.register(Company.class); 
  4.         ctx.refresh();  */  
  5.         ApplicationContext  ctx=new AnnotationConfigApplicationContext(CompanyConfig.class);   

其中注释的代码和不注释的代码可以看成是相同的,运行后结果不

 

@Import 注解:

@ import的注解允许加载@Bean从另一个配置类定义。考虑一个配置类,如下所示: 

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. @Configuration  
  2. public class ConfigA {  
  3.    @Bean  
  4.    public A a() {  
  5.       return new A();   
  6.    }  
  7. }  

 

您可以在另一个bean声明导入上述bean声明如下

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. @Configuration  
  2. @Import(ConfigA.class)  
  3. public class ConfigB {  
  4.    @Bean  
  5.    public B a() {  
  6.       return new A();   
  7.    }  
  8. }  

 

现在,不需要实例化的前提下,当同时指定配置A.class和配置B.class,只有Config B类需要如下提供:

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. public static void main(String[] args) {  
  2.    ApplicationContext ctx =   
  3.    new AnnotationConfigApplicationContext(ConfigB.class);  
  4.    // now both beans A and B will be available...  
  5.    A a = ctx.getBean(A.class);  
  6.    B b = ctx.getBean(B.class);  
  7. }  

 

生命周期回调:

@Bean注解支持指定任意的初始化和销毁​​回调方法,就像Spring的XML的初始化方法和bean元素销毁方法的属性:

 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. public class Foo {  
  2.    public void init() {  
  3.       // initialization logic  
  4.    }  
  5.    public void cleanup() {  
  6.       // destruction logic  
  7.    }  
  8. }  
  9.   
  10. @Configuration  
  11. public class AppConfig {  
  12.    @Bean(initMethod = "init", destroyMethod = "cleanup" )  
  13.    public Foo foo() {  
  14.       return new Foo();  
  15.    }  
  16. }  

 

指定Bean的适用范围:

默认范围是单例,但可以使用@Scope注解来覆盖此如下:

[java] view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. @Configuration  
  2. public class AppConfig {  
  3.    @Bean  
  4.    @Scope("prototype")  
  5.    public Foo foo() {  
  6.       return new Foo();  
  7.    }  
  8. }  

三种配置bean方式的对比总结

猜你喜欢

转载自harborchung.iteye.com/blog/2229707