私たちのソフトウェアでは、Spring Javaの設定を使用しています。 1つの設定が抽象設定を拡張した設定があります。 MyConfig
はAbstractConfig
を上書きして作成したのApplicationContextに名前anotherName
下のタイプAtomicInteger
の唯一のBeanがあることを、抽象構成を拡張したSpringのJava設定
import java.util.concurrent.atomic.AtomicInteger; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; public class SpringConfigTest { @Test public void test() { final AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfig.class); ctx.getBeansOfType(AtomicInteger.class).entrySet().stream().forEach( b -> System.out.println(b.getKey() + " : " + b.getValue() + " (" + b.getValue().hashCode() + ")")); } @Configuration public static class MyConfig extends AbstractConfig { @Bean(name = "anotherName") public AtomicInteger myBean() { return new AtomicInteger(5); } } public static abstract class AbstractConfig { @Bean public AtomicInteger myBean() { return new AtomicInteger(10); } } }
考え方は次のとおりです。このテストケースを見てください。
anotherName : 5 (2109798150) myBean : 5 (1074389766)
だから、2つのBean(2つのインスタンス - それぞれの名前に1つ) - があり、言うより一層surpring:
結果となったの両方を作成するために使用したのと同じ方法(MyConfig#myBean()
が)それら。
春は通常のJavaの継承方法を尊重し、MyConfig
から豆のみを作成するか、少なくとも2つの独立した豆( "10"と "5" )場合は、AbstractConfig
を独立した設定と見なします。さらに驚くべきことのために何であったか... anotherName : 5 (2109798150)
:
public static class MyConfig extends AbstractConfig { @Bean(name = ["anotherName", "myBean"]) public AtomicInteger myBean() { ...
と私たちは一つだけ豆を得た。この時間:これを調査している間
は、我々はまた、MyConfig
クラスにメソッド名を登録しようとしました米国。
これは本当に正しい動作であるか、間違っているだけですか?春のジラでチケットを買うべきですか? ありがとうございます!
春のJava設定を抽象化するためのユースケースは何ですか? – dhiller
私たちはspring-data-mongodbを使用しており、アプリケーションでは2つの異なるデータベースに接続しています。したがって、[AbstractMongoConfiguration](http://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/config/AbstractMongoConfiguration.html)を2回オーバーライドしますが、名前を変更する必要がありますいくつかの豆は他の場所で見つけることができます。しかし、今、私たちはappCtxで3回目を見ました... – tvvk
これはおそらくコンポーネントスキャンパスの問題に関連していますか?多分、mongodbパッケージのスキャンを避けることができますか?それとも、mongodbで春のデータを扱うために必要なのでしょうか? – dhiller