首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

基于注脚的依赖注入(零配置)

2012-09-15 
基于注解的依赖注入(零配置)基于注解的依赖注入(零配置) 基于注解(Annotation)的配置有越来越流行的趋势,

基于注解的依赖注入(零配置)

基于注解的依赖注入(零配置)
基于注解(Annotation)的配置有越来越流行的趋势,注解配置先对于XML配置具有很多的优势:
它可以充分利用java的反射机制获取类结构信息,这些信息可以有效减少配置的工作。
注释和java文件处于同一个文件中,而XML配置采用独立的配置文件,大多数配置文件信息在程序开发完成后不会调整,如果配置信息和java代码放在一起,有助于增强程序的内聚性。而采用独立的XML配置文件,程序员在编写一个功能时,往往需要在程序文件和配置文件中不停切换,这种思维上的不连贯会降低开发效率。
开发步骤:
导入common-annotations.jar架包
在ApplicationContext.xml中加入命名空间
Eg:

<?xml version="1.0" encoding="utf-8"?>  <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-2.5.xsd             http://www.springframework.org/schema/context             http://www.springframework.org/schema/context/spring-context-2.5.xsd">      <!-- 加入命名空间 -->      <context:annotation-config />  </beans>  

?


Resource注解
以前我们通过xml文件方式表示Bean之间的依赖关系,而现在我们可以通过@Resource方式来标识Bean之间的依赖关系。
Person.java、

package net.battier.pojo;    import java.io.Serializable;    import javax.annotation.Resource;    public class Person implements Serializable {      private static final long serialVersionUID = 1L;      private int id;      private String personName;      private boolean personSex;      private int personAge;      private String personDesc;      // 通过@Resource方式将配置文件中id为org的Bean注入给属性org;      //这种方式替代了xml文件中以ref关联Bean的方式。      @Resource(name="org")      private Organization org;        public int getId() {          return id;      }        public void setId(int id) {          this.id = id;      }                  。。。    }  

?
在配置文件ApplicationContext.xml中

<?xml version="1.0" encoding="utf-8"?>  <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-2.5.xsd             http://www.springframework.org/schema/context             http://www.springframework.org/schema/context/spring-context-2.5.xsd">      <!-- 加入命名空间 -->      <context:annotation-config />      <!-- Organization -->      <bean id="org" name="code">package net.battier.dao.impl;    import javax.annotation.PostConstruct;  import javax.annotation.PreDestroy;    import net.battier.dao.Axe;  import net.battier.dao.Person;    import org.springframework.beans.factory.DisposableBean;  import org.springframework.beans.factory.InitializingBean;    public class Chinese implements Person, InitializingBean, DisposableBean {        private Axe axe;        public Chinese() {          System.out.println("Spring实例化主调Bean:Chinese实例...");      }        // 通过设值注入      public void setAxe(Axe axe) {          this.axe = axe;      }        @Override      public void userAxe() {          // TODO Auto-generated method stub          axe.chop();      }        @Override      public void afterPropertiesSet() throws Exception {          // TODO Auto-generated method stub          System.out.println("正在初始化。。。");      }        // 定义销毁之前的特定行为      public void destroy() {          System.out.println("我快要挂了...");      }        @PostConstruct      public void init() {          System.out.println("使用注解初始化Chinese");      }        @PreDestroy      public void destroy1() {          System.out.println("使用注解销毁Chinese");      }  }  

?


ApplicationContext.xml
Java代码

<?xml version="1.0" encoding="utf-8"?>  <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-2.5.xsd             http://www.springframework.org/schema/context             http://www.springframework.org/schema/context/spring-context-2.5.xsd">      <!-- 加上命名空间 -->      <context:annotation-config />      <!-- 配置chinese的实例并加上初始化方法 init-method="init"-->      <bean id="chinese"          destroy-method="destroy">          <!-- 通过设置注入 -->          <property name="axe" ref="sleelAxe"></property>      </bean>      <!-- 配置stoneAxe实例 -->      <bean id="sleelAxe" name="code">package net.battier.pojo;    import java.io.Serializable;    import org.springframework.stereotype.Component;    @Component("org")  public class Organization implements Serializable {      private static final long serialVersionUID = 1L;      private int id;      private String orgName;      private String parent;      private String orgCode;      private String orgDesc;        public int getId() {          return id;      }        public void setId(int id) {          this.id = id;      }            ...    }  

?

Person .java

package net.battier.pojo;    import java.io.Serializable;    import javax.annotation.Resource;    import org.springframework.stereotype.Component;    @Component("person")  public class Person implements Serializable {      private static final long serialVersionUID = 1L;      private int id;      private String personName;      private boolean personSex;      private int personAge;      private String personDesc;        // 通过@Resource方式将配置文件中id为org的Bean注入给属性org;      // 这种方式替代了xml文件中以ref关联Bean的方式。      @Resource(name = "org")      private Organization org;        public int getId() {          return id;      }        public void setId(int id) {          this.id = id;      }            ...      }  

?


配置文件(注意要定义类扫描器,不需要命名空间)

<?xml version="1.0" encoding="utf-8"?>  <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-2.5.xsd             http://www.springframework.org/schema/context             http://www.springframework.org/schema/context/spring-context-2.5.xsd">      <!-- 定义类扫描器(不需要了命名空间) -->      <context:component-scan base-package="net.battier.pojo"></context:component-scan>        <!-- Organization 使用了@Component注释,不需要在XML中配置了-->      <!--  <bean id="org" name="code"><context:component-scan base-package="net.battier.pojo">      <context:include-filter type="regex"     expression="net\.battier\.service\.."/>      <context:exclude-filter type="aspectj" expression="net.battier.util.."/>  </context:component-scan>  

?




@Scope注释
默认情况下,@Component定义的Bean都是Singleton的,如果需要使用其他作用范围的Bean,可以通过@Scope来定义。

package net.battier.pojo;    import java.io.Serializable;    import javax.annotation.Resource;    import org.springframework.context.annotation.Scope;  import org.springframework.stereotype.Component;    @Component("person")  @Scope("Singleton")  public class Person implements Serializable {      private static final long serialVersionUID = 1L;      private int id;      private String personName;      private boolean personSex;      private int personAge;      private String personDesc;        // 通过@Resource方式将配置文件中id为org的Bean注入给属性org;      // 这种方式替代了xml文件中以ref关联Bean的方式。      @Resource(name = "org")      private Organization org;        public int getId() {          return id;      }        public void setId(int id) {          this.id = id;      }        ...    }     

?
其他注解
Spring2.5中除了提供@Component注解,还定义了几个拥有特殊语义的注解:@Reponsetory、@Service和@Controller.在目前的Spring版本中,这三个注解和@Component作用是等效的。但是从注解名上可以很清楚的看出3个注解分别对应:持久层(M)、业务层(V)、控制层(C),虽然目前这3个注解和@Component等效,但是Spring在以后的版本中为他们添加特殊的功能。所以如果项目分成了上述三层的话,最后在各个层中使用相应的注解。而@Component是对中立的类进行注解。

文章来源:

http://fendoubattier.iteye.com/blog/698777

?

热点排行