小编ixi*_*ixi的帖子

@Configurable不适用于在@PostConstruct方法中初始化的对象

我通过编译时编织将Spring与AspectJ结合使用,以将@Configurable spring注释用于不受容器管理的对象。

这是一个@Configurable注释的对象示例:

@Configurable(autowire = Autowire.BY_TYPE)
public class TestConfigurable {

    private TestComponent component;

    public TestComponent getComponent() {
        return component;
    }

    @Autowired
    public void setComponent(TestComponent component) {
        this.component = component;
    }
}
Run Code Online (Sandbox Code Playgroud)

我要注入此对象的组件:

@Component
public class TestComponent {}
Run Code Online (Sandbox Code Playgroud)

当我在创建上下文之后创建TestConfigurable时,可以很好地注入TestComponent,但是当我从@ PostConstruct-annotated方法执行此操作时,不会自动装配。

具有@PostConstruct的组件:

@Component
public class TestPostConstruct {

    @PostConstruct
    public void initialize() {
        TestConfigurable configurable = new TestConfigurable();
        System.out.println("In post construct: " + configurable.getComponent());
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在执行的应用程序:

public class TestApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext("/application-context.xml");
        applicationContext.registerShutdownHook(); …
Run Code Online (Sandbox Code Playgroud)

spring aspectj configurable compile-time-weaving postconstruct

3
推荐指数
1
解决办法
1203
查看次数