首頁  >  文章  >  Java  >  詳解Java的Spring框架中的註解的用法

詳解Java的Spring框架中的註解的用法

高洛峰
高洛峰原創
2017-01-23 10:35:501287瀏覽

1. 使用Spring註解來注入屬性

1.1. 使用註解以前我們是怎樣注入屬性的 
類的實作:

class UserManagerImpl implements UserManager {
  private UserDao userDao;
  public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
  }
  ...
}

設定檔:

<bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl">
  <property name="userDao" ref="userDao" />
</bean>
<bean id="userDao" class="com.kedacom.spring.annotation.persistence.UserDaoImpl">
  <property name="sessionFactory" ref="mySessionFactory" />
</bean>

引入


設定檔:

public class UserManagerImpl implements UserManager {
  @Autowired
  private UserDao userDao;
  ...
}

引入

1.wi. ,建議使用@Resource) 

類別的實作(對成員變數進行標註)

UserManagerImpl implements UserManager {
  private UserDao userDao;
  @Autowired
  public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
  }
  ...
}

或(對方法進行標註)

<bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl" />
<bean id="userDao" class="com.kedacom.spring.annotation.persistence.UserDaoImpl">
  <property name="sessionFactory" ref="mySessionFactory" />
</bean>

或組合

建構函式進行標註,來完成自動裝配的工作。以上兩種不同實作方式中,@Autowired的標註位置不同,它們都會在Spring在初始化userManagerImpl這個bean時,自動裝配userDao這個屬性,區別是:第一種實作中,Spring會直接將UserDao類型的唯一一個bean賦值給userDao這個成員變數;第二種實作中,Spring會呼叫setUserDao方法來將UserDao類型的唯一一個bean組裝到userDao這個屬性。

1.3. 讓@Autowired工作起來 

要讓@Autowired能夠運作,也需要在設定檔中加入以下程式碼

class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />



1.4. @Qualifier 

@Autowired是依照型別進行自動組裝的。在上面的範例中,如果當Spring上下文中存在不只一個UserDao類型的bean時,就會拋出BeanCreationException異常;如果Spring上下文中不存在UserDao類型的bean,也會拋出BeanCreationException異常。我們可以使用@Qualifier配合@Autowired來解決這些問題。 

1. 可能存在多個UserDao實例

@Autowired
public void setUserDao(@Qualifier("userDao") UserDao userDao) {
  this.userDao = userDao;
}


這樣,Spring會找到id為userDao的bean進行組裝。

2. 可能不存在UserDao實例

   
@Autowired(required = false)
public void setUserDao(UserDao userDao) {
  this.userDao = userDao;
}



1.5. @Resource(JSR-250標準註解,建議使用它來取代Spring專有的@Autowired註解) 
Spring 不但支援自己定義的SpringAutoredred註解,幾個由JSR-250規範定義的註解,它們分別是@Resource、@PostConstruct以及@PreDestroy。 

@Resource的作用相當於@Autowired,只不過@Autowired按byType自動注入,而@Resource默認按byName自動注入罷了。 @Resource有兩個屬性是比較重要的,分別是name和type,Spring將@Resource註解的name屬性解析為bean的名字,而type屬性則解析為bean的類型。所以如果使用name屬性,則使用byName的自動注入策略,而使用type屬性時則使用byType自動注入策略。如果既不指定name也不指定type屬性,這時將透過反射機制使用byName自動注入策略。

@Resource組裝順序

如果同時指定了name和type,則從Spring上下文中找到唯一匹配的bean進行裝配,找不到則拋出異常
如果指定了name,則從上下文中查找名稱(id)匹配的bean進行裝配,找不到則拋出異常
如果指定了type,則從上下文中找到類型匹配的唯一bean進行裝配,找不到或者找到多個,都會​​拋出異常
如果既沒有指定name ,又沒有指定type,則自動按照byName方式進行裝配(見2);如果沒有匹配,則回退為一個原始類型(UserDao)進行匹配,如果匹配則自動裝配;
1.6. @PostConstruct(JSR-250 ) 

在方法上加上註解@PostConstruct,這個方法就會在Bean初始化之後被Spring容器執行(註:Bean初始化包括,實例化Bean,並組裝Bean的屬性(依賴注入))。

它的一個典型的應用場景是,當你需要往Bean裡注入一個其父類中定義的屬性,而你又無法複寫父類的屬性或屬性的setter方法時,如: 

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
  private SessionFactory mySessionFacotry;
  @Resource
  public void setMySessionFacotry(SessionFactory sessionFacotry) {
    this.mySessionFacotry = sessionFacotry;
  }
  @PostConstruct
  public void injectSessionFactory() {
    super.setSessionFactory(mySessionFacotry);
  }
  ...
}

這裡透過@PostConstruct,為UserDaoImpl的父類別裡定義的一個sessionFactory私有屬性,注入了我們自己定義的sessionFactory(父類別的setSessionFactory方法為final,不可複寫),之後我們就可以透過呼叫super.getSessionFactory()來存取該屬性了。

1.7. @PreDestroy(JSR-250) 

在方法上加上註解@PreDestroy,這個方法就會在Bean初始化之後被Spring容器執行。由於我們目前還沒有需要用到它的場景,這裡不不去示範。其用法同@PostConstruct。

1.8. 使用簡化設定 

Spring2.1新增了一個新的context的Schema命名空間,該命名空間對註解驅動、屬性檔案引入、載入期織入等功能提供了便捷的配置。我們知道註釋本身是不會做任何事情的,它僅提供元資料資訊。要讓元資料資訊真正發揮作用,必須讓負責處理這些元資料的處理器工作起來。 

AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor就是處理這些註解元資料的處理器。但是直接在Spring設定檔中定義這些Bean顯得比較笨拙。 Spring為我們提供了一個方便的註冊這些BeanPostProcessor的方式,這就是

<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>
🎜🎜

02b5e3170f8825be11991279b556a682将隐式地向Spring容器注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、 PersistenceAnnotationBeanPostProcessor以及RequiredAnnotationBeanPostProcessor这4个BeanPostProcessor。

2. 使用Spring注解完成Bean的定义 
以上我们介绍了通过@Autowired或@Resource来实现在Bean中自动注入的功能,下面我们将介绍如何注解Bean,从而从XML配置文件中完全移除Bean定义的配置。

2.1. @Component(不推荐使用)、@Repository、@Service、@Controller 
只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean了:

@Component
public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
  ...
}

使用@Component注解定义的Bean,默认的名称(id)是小写开头的非限定类名。如这里定义的Bean名称就是userDaoImpl。你也可以指定Bean的名称: 
@Component("userDao") 
@Component是所有受Spring管理组件的通用形式,Spring还提供了更加细化的注解形式:@Repository、@Service、@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean。目前版本(2.5)中,这些注解与@Component的语义是一样的,完全通用,在Spring以后的版本中可能会给它们追加更多的语义。所以,我们推荐使用@Repository、@Service、@Controller来替代@Component。

2.2. 使用5fac6a8af37bdf6785fe01c166cb3779让Bean定义注解工作起来

<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="com.kedacom.ksoa" />
</beans>

这里,所有通过60e23eb984d18edbb092da6b8f295aba元素定义Bean的配置内容已经被移除,仅需要添加一行5fac6a8af37bdf6785fe01c166cb3779配置就解决所有问题了——Spring XML配置文件得到了极致的简化(当然配置元数据还是需要的,只不过以注释形式存在罢了)。5fac6a8af37bdf6785fe01c166cb3779的base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。 
5fac6a8af37bdf6785fe01c166cb3779还允许定义过滤器将基包下的某些类纳入或排除。Spring支持以下4种类型的过滤方式:

过滤器类型 表达式范例 说明
注解 org.example.SomeAnnotation 将所有使用SomeAnnotation注解的类过滤出来
类名指定 org.example.SomeClass 过滤指定的类
正则表达式 com\.kedacom\.spring\.annotation\.web\..* 通过正则表达式过滤一些类
AspectJ表达式 org.example..*Service+ 通过AspectJ表达式过滤一些类

以正则表达式为例,我列举一个应用实例:

<context:component-scan base-package="com.casheen.spring.annotation">
  <context:exclude-filter type="regex" expression="com\.casheen\.spring\.annotation\.web\..*" />
</context:component-scan>

值得注意的是5fac6a8af37bdf6785fe01c166cb3779配置项不但启用了对类包进行扫描以实施注释驱动Bean定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),因此当使用5fac6a8af37bdf6785fe01c166cb3779后,就可以将bfe8fab089282832fed14fcb6f680c1e移除了。

2.3. 使用@Scope来定义Bean的作用范围 
在使用XML定义Bean时,我们可能还需要通过bean的scope属性来定义一个Bean的作用范围,我们同样可以通过@Scope注解来完成这项工作:

@Scope("session")
@Component()
public class UserSessionBean implements Serializable {
  ...
}

3.在使用annotation之前定义三个bean之间的关系是这样的

package com.baobaotao;
public class Office {
  private String officeNo =”001”;
  
  //省略 get/setter
  
  @Override
  public String toString() {
    return "officeNo:" + officeNo;
  }
}
 
ackage com.baobaotao;
  
public class Car {
  private String brand;
  private double price;
  
  // 省略 get/setter
  
  @Override
  public String toString() {
    return "brand:" + brand + "," + "price:" + price;
  }
}
 
package com.baobaotao;
  
public class Boss {
  private Car car;
  private Office office;
  
  // 省略 get/setter
  
  @Override
  public String toString() {
    return "car:" + car + "\n" + "office:" + office;
  }
}

配置文件如下:

   
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  <bean id="boss" class="com.baobaotao.Boss">
    <property name="car" ref="car"/>
    <property name="office" ref="office" />
  </bean>
  <bean id="office" class="com.baobaotao.Office">
    <property name="officeNo" value="002"/>
  </bean>
  <bean id="car" class="com.baobaotao.Car" scope="singleton">
    <property name="brand" value=" 红旗 CA72"/>
    <property name="price" value="2000"/>
  </bean>
</beans>

测试文件如下:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AnnoIoCTest {
  
  public static void main(String[] args) {
    String[] locations = {"beans.xml"};
    ApplicationContext ctx =
      new ClassPathXmlApplicationContext(locations);
    Boss boss = (Boss) ctx.getBean("boss");
    System.out.println(boss);
  }
}

 

4.接下来我们可以使用autowired来注解,他可以对成员变量,方法及构造函数进行标准,完成自动装配的工作。

autoware来注解成员变量的用法

package com.baobaotao;
import org.springframework.beans.factory.annotation.Autowired;
  
public class Boss {
  
  @Autowired
  private Car car;
  
  @Autowired
  private Office office;
  
  …
}

相应的配置文件如下:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  
  <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->
  <bean class="org.springframework.beans.factory.annotation.
    AutowiredAnnotationBeanPostProcessor"/>
  
  <!-- 移除 boss Bean 的属性注入配置的信息 -->
  <bean id="boss" class="com.baobaotao.Boss"/>
  
  <bean id="office" class="com.baobaotao.Office">
    <property name="officeNo" value="001"/>
  </bean>
  <bean id="car" class="com.baobaotao.Car" scope="singleton">
    <property name="brand" value=" 红旗 CA72"/>
    <property name="price" value="2000"/>
  </bean>
</beans>

autoware也可以用在setter方法及构造函数上

package com.baobaotao;
  
public class Boss {
  private Car car;
  private Office office;
  
   @Autowired
  public void setCar(Car car) {
    this.car = car;
  }
  
  @Autowired
  public void setOffice(Office office) {
    this.office = office;
  }
  …
}
 
package com.baobaotao;
  
public class Boss {
  private Car car;
  private Office office;
  
  @Autowired
  public Boss(Car car ,Office office){
    this.car = car;
    this.office = office ;
  }
  
  …
}

当候选bean的数目为0时,我们可以使用@Autowired(required = false)来防止spring找不到bean时报错。

当有多个候选bean的时候,我们可以通过@Qualifier 注释指定注入 Bean 的名称。

@Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变成 byName 了。@Autowired 可以对成员变量、方法以及构造函数进行注释,而 @Qualifier 的标注对象是成员变量、方法入参、构造函数入参。正是由于注释对象的不同,所以 Spring 不将 @Autowired 和 @Qualifier 统一成一个注释类。

5.@resorce是按照名字来进行反射,他有两个参数,name和type,使用name即按照byname来映射,使用type即按照bytype来进行映射。

package com.baobaotao;
  
import javax.annotation.Resource;
  
public class Boss {
  // 自动注入类型为 Car 的 Bean
  @Resource
  private Car car;
  
  // 自动注入 bean 名称为 office 的 Bean
  @Resource(name = "office")
  private Office office;
}
@postconstructor和preDestory是用来注解类初始化后和销毁前的方法。
 
 
package com.baobaotao;
  
import javax.annotation.Resource;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
  
public class Boss {
  @Resource
  private Car car;
  
  @Resource(name = "office")
  private Office office;
  
  @PostConstruct
  public void postConstruct1(){
    System.out.println("postConstruct1");
  }
  
  @PreDestroy
  public void preDestroy1(){
    System.out.println("preDestroy1");
  }
  …
}

6.@compent可以直接定义bean,这样xml配置文件中就不需要配置bean了

package com.baobaotao;
  
import org.springframework.stereotype.Component;
  
@Component
public class Car {
  …
}
 
package com.baobaotao;
  
import org.springframework.stereotype.Component;
  
@Component
public class Office {
  private String officeNo = "001";
  …
}
 
package com.baobaotao;
  
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
  
@Component("boss")
public class Boss {
  @Autowired
  private Car car;
  
  @Autowired
  private Office office;
  …
}

@Component 有一个可选的入参,用于指定 Bean 的名称,在 Boss 中,我们就将 Bean 名称定义为“boss”。一般情况下,Bean 都是 singleton 的,需要注入 Bean 的地方仅需要通过 byType 策略就可以自动注入了,所以大可不必指定 Bean 的名称。

<?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="com.baobaotao"/>
</beans>

7.@scope可以用来指定其目标

package com.baobaotao;
import org.springframework.context.annotation.Scope;
…
@Scope("prototype")
@Component("boss")
public class Boss {


Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository、@Service 和 @Controller。在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层、业务层和控制层(Web 层)相对应。虽然目前这 3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能。所以,如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释,而用 @Component 对那些比较中立的类进行注释。

更多详解Java的Spring框架中的注解的用法相关文章请关注PHP中文网!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn