ホームページ  >  記事  >  Java  >  JavaのSpringフレームワークにおけるアノテーションの使用法の詳細な説明

JavaのSpringフレームワークにおけるアノテーションの使用法の詳細な説明

高洛峰
高洛峰オリジナル
2017-01-23 10:35:501286ブラウズ

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>

1.2. @Autowired アノテーションを導入する (推奨されません) @Resource を使用することをお勧めします)

クラス実装 (メンバー変数をマーク)

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

または (メソッドをマーク)

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 はメンバー変数、メソッド、およびコンストラクターをマークできます自動組立作業が完了します。上記の 2 つの異なる実装では、@Autowired アノテーションの位置が異なります。Spring が userManagerImpl Bean を初期化するときに、userDao 属性が自動的にアセンブルされます。違いは、最初の実装では、Spring が UserDao タイプの 1 つだけを直接アセンブルすることです。 Bean は userDao メンバー変数に割り当てられます。2 番目の実装では、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 もスローされます。 @Autowired とともに @Qualifier を使用すると、これらの問題を解決できます。
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 は独自の @Autowired アノテーションをサポートするだけでなく、 JSR-250 仕様で定義されているいくつかのアノテーション (@Resource、@PostConstruct、および @PreDestroy) もサポートします。
@Resource の機能は @Autowired と同等ですが、@Autowired は byType によって自動的に挿入されますが、@Resource はデフォルトで byName によって自動的に挿入されます。 @Resource には、name と type という 2 つの重要な属性があります。Spring は @Resource アノテーションの name 属性を Bean の名前に解決し、type 属性は Bean のタイプに解決します。したがって、name 属性が使用される場合は byName 自動挿入戦略が使用され、type 属性が使用される場合は byType 自動挿入戦略が使用されます。 name 属性も type 属性も指定されていない場合は、リフレクション メカニズムを通じて byName 自動挿入戦略が使用されます。
@リソースアセンブリの順序

名前と型を同時に指定すると、アセンブリのSpringコンテキストから一意に一致するBeanが検索されます。見つからない場合は、例外がスローされます。

名前が指定されている場合は、その名前がスローされます。 (id) がコンテキストから見つかります。タイプが指定されている場合、Bean が見つからない場合は、コンテキストから一致する Bean が検索されます。複数の名前が指定されていない場合、Bean はアセンブルされます。タイプが指定されていない場合は、byName メソッドに従って自動的にアセンブルされます (2 を参照)。一致すると、プリミティブ型 (UserDao) にフォールバックして一致する場合、自動的にアセンブルされます。
1.6. @PostConstruct (JSR-250)
@PostConstruct アノテーションをメソッドに追加します。 Bean の初期化後に Spring コンテナによって実行されます (注: Bean の初期化には、Bean のインスタンス化と Bean のプロパティのアセンブル (依存関係の注入) が含まれます)。
典型的なアプリケーション シナリオは、親クラスで定義されたプロパティを Bean に注入する必要があり、次のような親クラスのプロパティまたはプロパティのセッター メソッドをオーバーライドできない場合です。独自に定義した sessionFactory を、UserDaoImpl の親クラスで定義された sessionFactory プライベート プロパティに追加します (親クラスの setSessionFactory メソッドは最終的なメソッドであり、オーバーライドできません)。その後、 super.getSessionFactory() を呼び出すことでアクセスできるようになります。プロパティは削除されています。


1.7. @PreDestroy (JSR-250)

@PreDestroy アノテーションをメソッドに追加すると、このメソッドは Bean の初期化後に Spring コンテナによって実行されます。現時点ではこれを使用する必要があるシナリオがないため、ここでは説明しません。使い方は @PostConstruct と同じです。

1.8. bfe8fab089282832fed14fcb6f680c1e を使用して構成を簡素化します

Spring2.1 では、アノテーション ドライバー、プロパティ ファイルの導入、ロード時のウィービングなどの機能を提供する新しいコンテキスト スキーマ名前空間が追加されます。アノテーション自体は何も行わず、メタデータ情報を提供するだけであることがわかっています。メタデータ情報が真に役立つためには、メタデータの処理を担当するプロセッサが稼働する必要があります。

AutowiredAnnotationBeanPostProcessor と CommonAnnotationBeanPostProcessor は、これらのアノテーション メタデータを処理するプロセッサです。しかし、これらの Bean を Spring 構成ファイルで直接定義するのは不便です。 Spring は、これらの BeanPostProcessor を登録する便利な方法を提供します。これは、bfe8fab089282832fed14fcb6f680c1e:

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);
  }
  ...
}


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 までご連絡ください。