Tôi thấy một số hành vi kỳ lạ, tôi hy vọng một người nào đó ở đây có thể tỏa sáng một chút về vấn đề này.Vấn đề JavaConfig và CustomScopeConfigurer của Spring
Hãy để tôi bắt đầu bằng cách mô tả thiết lập của tôi. Đầu tiên, một dữ liệu đơn giản đối tượng
public class Apple {
private String name;
public Apple withName(String name) {
this.name = name;
return this;
}
public String getName() {
return name;
}
}
Và một lớp thử nghiệm ..
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={TestConfig.class})
public class AppleTest {
@Autowired private Apple apples;
@Test
public void simpleTest() {
System.out.println("OBJ: "+apples);
}
}
Các cấu hình như sau
@Configuration
public interface ConfigInterface {
public Apple getApple();
}
Với một lớp học thực hiện
@Configuration
@Import(AbstractTestConfig.class)
public class TestConfig implements ConfigInterface {
public Apple getApple() {
return new Apple().withName("Granny apples");
}
}
Với phụ thuộc cấu hình ...
@Configuration
public class AbstractTestConfig {
@Autowired ConfigInterface conf;
@Bean Apple myTestApple() {
return conf.getApple();
}
}
Tất cả các công trình này đều tuyệt vời. Tôi chạy thử nghiệm, tôi thấy đầu ra tôi mong đợi. Nhưng sau đó tôi ném một cờ lê vào bánh xe và sửa đổi AbstractTestConfig để trông như sau.
@Configuration
public class AbstractTestConfig {
@Autowired ConfigInterface conf;
@Bean Apple myTestApple() {
return conf.getApple();
}
// NEW CODE
@Bean CustomScopeConfigurer scopeConfigurer() {
return new CustomScopeConfigurer();
}
}
Và tất cả của một đột ngột các @Autowired
đối tượng conf
là null khi nó là cần thiết để xây dựng các đậu Apple
.
Kỳ lạ hơn nữa, nếu tôi di chuyển hạt CustomScopeConfigurer
sang lớp TestConfig
, thì nó hoạt động.
Có điều gì tôi không biết về phạm vi hoặc đối tượng CustomScopeConfigurer
nói riêng không?