2017-03-02 19 views
3

私はSpringMVC Webアプリケーションでセッター注入について学びたいと思っています。すべての例は、xmlを使って表示されています。しかし、私はxmlが推奨されなくなり、すべての新しいアプリケーションはjava構成で行うべきだと言われました。これは間違っていますか?xmlを使用してアプリケーションを設定する必要がありますか?SpringでJava設定でセッター注入を使用するにはどうすればよいですか?

どこで宣言する必要がありますか?

これは私が見た例の1つですが、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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans.xsd"> 

    <bean id="message" 
     class="org.springbyexample.di.xml.SetterMessage"> 
    <property name="message" value="Spring is fun." /> 
    </bean> 

</beans> 

答えて

2

基本的なこと(注入など)がどのように機能するかを知るには、最初にプレーンなSpring設定を調べることをお勧めします。あなたがSpringでハングアップすることができれば、プロセスはSpring MVC/Spring Boot/etcで非常によく似ています。個人的には、一度に複数のコンセプト(ビューリゾルバ、さまざまな設定ファイル、ビュー、リポジトリ、複数の注釈、複数の設定方法など)を混乱させることに非常に不満を感じるので、私は単純な概念から始めて、方法をアップ。インジェクションの仕組みに慣れたら、他の場所でこの知識を簡単に適用できます。

Javaの設定と注釈については、はるかに迅速かつクリーンな開発が可能です。 XMLは非常に冗長で、保守が難しく、エラーが発生しやすいです。これは、IDEが一般的にJavaベースの設定で作業するときに役立つからです。おそらくそれが、XMLが非推奨となっていることを読んでいる理由です。あなたが本当に必要としていない限り(またはそれに興味がある場合を除いて)、XML /自動構成ではなくXMLにすることをお勧めします。

これを行う方法を今すぐにしてください。完全な(しかし、最小限の)作業春の例:

package main.java.org.example; 
import main.java.org.example.GreetingCollector; 
import main.java.org.example.Greeting; 
import org.springframework.context.annotation.Configuration; 

@Configuration 
public class Config { 
    @Bean 
    public Greeting greeting() { 
     return new Greeting(); 
    } 

    @Bean 
    public GreetingCollector greetingCollector(Greeting greeting) { 
     return new GreetingCollector(greeting); 
    } 
} 

そして、あなたはそれがどのように動作するかを確認するために、それを実行したい場合に:

/* Bean definition 

@Component tells Spring that this is a bean. There are a few similar annotations. 
It will be discovered during the component scan, as it has @Component annotation */ 

package main.java.org.example; 
import org.springframework.stereotype.Component; 

@Component 
public class Greeting { 
    private String greeting = "Hello"; 

    public String getGreeting() { 
     return this.greeting; 
    } 

    public void setGreeting(String greeting) { 
     this.greeting = greeting; 
    } 
} 


/* Another bean definition. 
It has another bean as a dependency, which we inject with a setter. */ 

package main.java.org.example; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component; 

@Component 
public class GreetingCollector { 
    private Greeting greeting; 

    /* This is how you do setter injection */ 
    @Autowired 
    public void setGreeting(Greeting greeting) { 
     this.greeting = greeting; 
    } 

    public String getGreeting() { 
     return greeting.getGreeting(); 
    } 
} 


/* This is a minimal config class. 
@ComponentScan instructs to look for classes that are 
annotated with @Component annotation (in other words, beans) */ 

package main.java.org.example;  
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 

@ComponentScan 
@Configuration 
public class Config {} 

あなたは明示的にそれをしたい場合は

import main.java.org.example.Config; 
import main.java.org.example.GreetingCollector; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.annotation.AnnotationConfigApplicationContext; 

public class AppContext { 
    public static void main(String args[]) { 
     System.out.println("Configuring application context..."); 
     ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); 
     GreetingCollector collector = (GreetingCollector) context.getBean("greetingCollector"); 
     System.out.println(collector.getGreeting()); 
    } 
} 

もちろん、SpringのWebアプリケーションは少し異なりますが、基本的な注入のアイデアは同じです。まず、豆を宣言する必要があります(@Bean@Componentまたはその他の注釈を使用してください:違いについてはhereおよびhereを参照してください)。 setterまたはコンストラクタ(またはフィールド)に@Autowiredと注釈を付け、パラメータ(必ずしも具体的なクラスである必要はありません - インタフェース、抽象クラスも問題ありません)を指定し、それらを適切なフィールドに割り当てます。 Beanのインスタンス化を行うconfigクラスを作成します。コンポーネントを探す場所を常に指定できるので、コンポーネントをconfigクラスと同じフォルダに配置する必要はありません。最後に、よりきめの細かいコントロールが必要な場合は、Beanをコンフィグレーションクラスで明示的に宣言することができます(JavaConfigと呼ばれますが、@ComponentScanベースのconfigはautoconfigと呼ばれることもあります)。これは、あなたを始めてより高度なものを探すためのボキャブラリーを与えるのに十分なはずです。

もちろん、Spring Bootでは、すべてがより抽象化され、より速くなります。

1

しかし、私は、XMLは廃止され、すべての新しいアプリケーションは

XMLは非推奨ですが、注釈は人生を容易にしているがありますされていないJava構成で行われるべきであると言われました。それでは、普通の古いXMLを使う理由を教えてください。 「コンフィグレーションに関する条約」を覚えておいてください。

ここで私はBeanを宣言する必要がありますか?

@Component、@Configuration、@Beanのようなアノテーションをgoogleで検索してください。あなたが理解できるものを読む

これは私が見た例の1つですが、xmlで実装されています。

あなたはXMLが広く春の早い時代に使用されたとして、多くの例では、XMLを使用して実装見つけるだろう。 今度はSpring 4とSpring Bootの登場です。ほとんどの開発者はXMLを大規模に使用していません。

関連する問題