奇妙な動作が見られます。ここの誰かがこの問題に光を当ててくれることを願っていました。
私の設定を説明することから始めましょう。まず、単純なデータ オブジェクト
public class Apple {
private String name;
public Apple withName(String name) {
this.name = name;
return this;
}
public String getName() {
return name;
}
}
そしてテストクラス..
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={TestConfig.class})
public class AppleTest {
@Autowired private Apple apples;
@Test
public void simpleTest() {
System.out.println("OBJ: "+apples);
}
}
構成は次のとおりです
@Configuration
public interface ConfigInterface {
public Apple getApple();
}
実装クラスで
@Configuration
@Import(AbstractTestConfig.class)
public class TestConfig implements ConfigInterface {
public Apple getApple() {
return new Apple().withName("Granny apples");
}
}
構成の依存関係で...
@Configuration
public class AbstractTestConfig {
@Autowired ConfigInterface conf;
@Bean Apple myTestApple() {
return conf.getApple();
}
}
これはすべてうまくいきます。テストを実行すると、期待どおりの出力が表示されます。しかし、スパナをホイールに投げ込み、AbstractTestConfig を次のように変更します。
@Configuration
public class AbstractTestConfig {
@Autowired ConfigInterface conf;
@Bean Apple myTestApple() {
return conf.getApple();
}
// NEW CODE
@Bean CustomScopeConfigurer scopeConfigurer() {
return new CustomScopeConfigurer();
}
}
そして、 Beanを構築する必要があるときに、@Autowired
オブジェクトが突然 null になります。conf
Apple
さらに奇妙なことに、CustomScopeConfigurer
Bean をTestConfig
クラスに移動すると、機能します。
CustomScopeConfigurer
特にスコープやオブジェクトについてわからないことはありますか?