I'm seeing some odd behavior, I was hoping someone here can shine some light on the issue.
Let me start by describing my setup. First, a simple data object
public class Apple {
private String name;
public Apple withName(String name) {
this.name = name;
return this;
}
public String getName() {
return name;
}
}
And a test class..
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={TestConfig.class})
public class AppleTest {
@Autowired private Apple apples;
@Test
public void simpleTest() {
System.out.println("OBJ: "+apples);
}
}
The config is as follows
@Configuration
public interface ConfigInterface {
public Apple getApple();
}
With an implementing class
@Configuration
@Import(AbstractTestConfig.class)
public class TestConfig implements ConfigInterface {
public Apple getApple() {
return new Apple().withName("Granny apples");
}
}
With the config dependency...
@Configuration
public class AbstractTestConfig {
@Autowired ConfigInterface conf;
@Bean Apple myTestApple() {
return conf.getApple();
}
}
All of this works great. I run the test, I see the output I expect. But then I throw a spanner into the wheel and modify AbstractTestConfig to look as follows.
@Configuration
public class AbstractTestConfig {
@Autowired ConfigInterface conf;
@Bean Apple myTestApple() {
return conf.getApple();
}
// NEW CODE
@Bean CustomScopeConfigurer scopeConfigurer() {
return new CustomScopeConfigurer();
}
}
And all of a sudden the @Autowired
object conf
is null when it is required to construct the Apple
bean.
Even more odd, if I move the CustomScopeConfigurer
bean to the TestConfig
class, then it works.
Is there something I don't know about scopes or the CustomScopeConfigurer
object in particular?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…