基本的なこと(注入など)がどのように機能するかを知るには、最初にプレーンな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では、すべてがより抽象化され、より速くなります。