Spring 3.0以降、Springは標準のhttp://www.jcp.org/ja/jsr/detail?id=330[JSR 330:Javaの依存性注入]をサポートしています。 Spring 3アプリケーションでは、標準

  1. Beanを挿入するためにSpringの

    @ Autowired`の代わりに

    @Inject`を使います.

  2. Beanを宣言するには、Springの

    @Component`の代わりに

    @ Named`を使います.

これらのJSR-330標準アノテーションは、Springアノテーションと同じ方法でスキャンされ、取得されます。統合はクラスパスの次のjarである限り、自動的に行われました。

pom.xml

    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>

1.春の注釈

通常のSpringの注釈の例を見てみましょう –

@ Autowired`と

@ Component`

__P.S

@ Component

、` @ Repository`と `@Service`は同じですが、Spring IocコンテキストでBeanを宣言するだけです。

CustomerDAO.java

package com.mkyong.customer.dao;

import org.springframework.stereotype.Repository;

@Repository
public class CustomerDAO
{
    public void save() {
        System.out.println("CustomerDAO save method...");
    }
}

CustomerService.java

package com.mkyong.customer.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mkyong.customer.dao.CustomerDAO;

@Service
public class CustomerService
{
    @Autowired
    CustomerDAO customerDAO;

    public void save() {

        System.out.println("CustomerService save method...");
        customerDAO.save();

    }

}

2. JSR-330注釈

基本的には、

@ Inject`と

@ Named`という別々のアノテーションを使って同じように動作します。

CustomerDAO.java

package com.mkyong.customer.dao;

import javax.inject.Named;

@Named
public class CustomerDAO
{

    public void save() {
        System.out.println("CustomerDAO save method...");
    }
}

CustomerService.java

package com.mkyong.customer.services;

import javax.inject.Inject;
import javax.inject.Named;

import com.mkyong.customer.dao.CustomerDAO;

@Named
public class CustomerService
{
    @Inject
    CustomerDAO customerDAO;

    public void save() {

        System.out.println("CustomerService save method...");
        customerDAO.save();

    }

}

3.それを実行する

SpringとJSR330アノテーションの両方で、コンポーネントのスキャンが必要です。

Spring-AutoScan.xml – 自動的に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-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:component-scan base-package="com.mkyong.customer"/>

</beans>

App.java – それを実行する

package com.mkyong;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mkyong.customer.services.CustomerService;

public class App
{
    public static void main( String[]args )
    {
        ApplicationContext context =
            new ClassPathXmlApplicationContext(new String[]{"Spring-AutoScan.xml"});

        CustomerService cust = (CustomerService)context.getBean("customerService");
        cust.save();

    }
}

上記の2つの例は同じ出力を生成します

CustomerService save method...
CustomerDAO save method...

4. JSR-330の制限事項

Springと比較すると、JSR-330にはいくつかの制限があります。

  1. `@Inject`には、Beanが存在することを確認するための”

    required

    “属性はありません

成功した。

  1. Springコンテナでは、デフォルトでJSR-330にスコープシングルトンが設定されていますが、

Springの `@Scope`を使って他のものを定義することができます。

  1. Springの

    @Value

    、` @ Required`、 `@Lazy`に相当するものはありません.


Spring references

をご覧ください。

5.JSR-330に行く

実際、Springのアノテーションはより強力ですが、Springフレームワークでのみ利用可能です。 JSR-330は標準仕様であり、JSR-330仕様に準拠するすべてのJ2EE環境でサポートされています。

新規プロジェクトやマイグレーションプロジェクトの場合は、JSR-330アノテーションを使用することを常にお勧めします。忘れずに、Spring 3でも機能します。

ソースコードをダウンロードする

ダウンロードする –

Spring-JSR330-Example.zip

(27kb)

参考文献

参照:JSR 330標準注釈の使用]。

http://www.jcp.org/ja/jsr/detail?id=330

[JSR 330:依存性注入

Java用]


依存性注入


spring

link://タグ/spring 3/[spring 3]