小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

彈簧-@Configurable不適用于在@PostConstruct方法中初始化的對象

 印度阿三17 2019-10-28

我通過編譯時編織將Spring與AspectJ結(jié)合使用,以便對不受容器管理的對象使用@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;
    }
}

我要注入此對象的組件:

@Component
public class TestComponent {}

當(dāng)我在創(chuàng)建上下文之后創(chuàng)建TestConfigurable時,可以很好地注入TestComponent,但是當(dāng)我從@ PostConstruct-annotated方法執(zhí)行此操作時,不會自動裝配.

具有@PostConstruct的組件:

@Component
public class TestPostConstruct {

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

我正在執(zhí)行的應(yīng)用程序:

public class TestApplication {

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

        TestConfigurable configurable = new TestConfigurable();
        System.out.println("After context is loaded: "   configurable.getComponent());
    }
}

這是此應(yīng)用程序產(chǎn)生的輸出:

In post construct: null
After context is loaded: paulenka.aleh.roguelike.sandbox.TestComponent@fe18270

那么,有什么解決方法可以將依賴項注入到@PostConstruct方法內(nèi)部創(chuàng)建的@Configurable對象中?所有帶有@Component注釋的Bean都已存在于上下文中,并且在調(diào)用@PostConstruct時已為它們自動完成了裝配.為什么Spring在這里不自動布線?

可以發(fā)布其他配置文件(上下文和pom.xml),如果它們有助于解決問題.

更新1:
看起來我找到了問題的原因.用@Configurable注釋的對象由實現(xiàn)BeanFactoryAware的AnnotationBeanConfigurerAspect初始化.該方面使用BeanFactory初始化bean.看起來在BeanFactory設(shè)置為AnnotationBeanConfigurerAspect之前,已執(zhí)行TestPostConstruct對象的@PostConstruct方法.如果設(shè)置為調(diào)試的記錄器將以下消息打印到控制臺:“尚未在…上設(shè)置BeanFactory:確保此配置程序在Spring容器中運行.無法配置類型[…]的Bean.繼續(xù)進行操作,無需注入. ”

是否有任何解決方法的問題仍對我開放…

解決方法:

我找到了一種解決方法.要在執(zhí)行@PostConstruct之前初始化AnnotationBeanConfigurerAspect方面,可以使用此類@PostConstruct方法將以下注釋添加到類中:

@DependsOn("org.springframework.context.config.internalBeanConfigurerAspect")

希望這些信息對某人有用.

來源:https://www./content-4-531501.html

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多