2017-09-14 69 views
0

在試驗Wild-羣體的過程中,我遇到了有關豆注射的奇怪情況。野蠅和野蠅羣注入CDI豆從戰爭部署VS定製模塊

我有一個非常簡單的bean,或多或少是這樣的:

@ApplicationScoped 
public class FooServiceImpl implements FooService { 
    Foo delegate; 
    @PostConstruct public void init() { 
     delegate = .....; 
    } 

    public Foo getFoo() { 
     return delegate; 
    } 
} 

如果我在一個罐子裏,直接在戰爭部署捆綁這一點,一切工作正常,並符合市場預期。但是,我需要將此實現的內部部分與應用程序完全隔離,爲什麼我將服務API及其實現打包到單獨的jboss模塊中。

這些模塊被添加到swarm uberJar中,我的應用依靠它們通過MANIFEST Dependencies條目。現在,一切似乎都很順利,FooService bean被注入到我的應用程序servlet/rest資源中,但init()方法未被調用。

我無法弄清楚這裏發生了什麼。這就像bean解析過程無法識別@ApplicationScope註釋。可以有兩個不同的類加載器嗎?

UPDATE

我啓用的跟蹤,對我,它看起來像焊縫處理FooImpl類作爲ApplicationScoped,即增加一個LifecycleMixin.lifecycle_mixin_$$_postConstruct()到代理創建:

2017-09-14 23:11:34,315 TRACE [org.jboss.weld.Bean] (ServerService Thread Pool -- 12) WELD-001538: Created context instance for bean Managed Bean [class com.xxx.FooImpl] with qualifiers [@Any @Default] identified as WELD%ManagedBean%test.war|test.war.external.file:/tmp/nestedjarloader2449602760983533131.tmp/META-INF/beans.xml|com.xxx.FooImpl|null|false 
2017-09-14 23:11:34,315 TRACE [org.jboss.weld.Bean] (ServerService Thread Pool -- 12) WELD-001542: Retrieving/generating proxy class com.xxx.FooImpl$Proxy$_$$_WeldClientProxy 
2017-09-14 23:11:34,315 TRACE [org.jboss.weld.Bean] (ServerService Thread Pool -- 12) WELD-001541: Adding method to proxy: public void com.xxx.FooImpl.registerMessageHandler(com.xxx.MessageHandler) 
2017-09-14 23:11:34,315 TRACE [org.jboss.weld.Bean] (ServerService Thread Pool -- 12) WELD-001541: Adding method to proxy: public void com.xxx.FooImpl.registerListeners(java.util.EventListener[]) 
2017-09-14 23:11:34,316 TRACE [org.jboss.weld.Bean] (ServerService Thread Pool -- 12) WELD-001541: Adding method to proxy: public void com.xxx.FooImpl.send(com.xxx.MessageHandler,com.xxx.Message) throws java.io.IOException 
2017-09-14 23:11:34,316 TRACE [org.jboss.weld.Bean] (ServerService Thread Pool -- 12) WELD-001541: Adding method to proxy: public void com.xxx.FooImpl.init() 
2017-09-14 23:11:34,316 TRACE [org.jboss.weld.Bean] (ServerService Thread Pool -- 12) WELD-001541: Adding method to proxy: public java.lang.String java.lang.Object.toString() 
2017-09-14 23:11:34,316 TRACE [org.jboss.weld.Bean] (ServerService Thread Pool -- 12) WELD-001541: Adding method to proxy: public abstract void org.jboss.weld.interceptor.proxy.LifecycleMixin.lifecycle_mixin_$$_postConstruct() 
2017-09-14 23:11:34,316 TRACE [org.jboss.weld.Bean] (ServerService Thread Pool -- 12) WELD-001541: Adding method to proxy: public abstract void org.jboss.weld.interceptor.proxy.LifecycleMixin.lifecycle_mixin_$$_preDestroy() 
2017-09-14 23:11:34,317 TRACE [org.jboss.weld.Bean] (ServerService Thread Pool -- 12) WELD-001543: Created Proxy class of type class com.xxx.FooImpl$Proxy$_$$_WeldClientProxy supporting interfaces [interface com.xxx.FooService, interface java.io.Serializable, interface org.jboss.weld.interceptor.proxy.LifecycleMixin, interface org.jboss.weld.interceptor.util.proxy.TargetInstanceProxy, interface org.jboss.weld.bean.proxy.ProxyObject] 
2017-09-14 23:11:34,317 TRACE [org.jboss.weld.Bean] (ServerService Thread Pool -- 12) WELD-001506: Created new client proxy of type class com.xxx.FooImpl$Proxy$_$$_WeldClientProxy for bean Managed Bean [class com.xxx.FooImpl] with qualifiers [@Any @Default] with ID WELD%ManagedBean%test.war|test.war.external.file:/tmp/nestedjarloader2449602760983533131.tmp/META-INF/beans.xml|com.xxx.FooImpl|null|false 
2017-09-14 23:11:34,318 TRACE [org.jboss.weld.Bean] (ServerService Thread Pool -- 12) WELD-001507: Located client proxy of type class com.xxx.FooImpl$Proxy$_$$_WeldClientProxy for bean Managed Bean [class com.xxx.FooImpl] with qualifiers [@Any @Default] 

的postconstruct攔截ISN沒有被調用 - 爲什麼?神祕加深!

UPDATE 2

上香草測試此wildfly,並且行爲是相同的,@PostConstruct方法,如果豆位於模塊不被調用。

+0

如果你已經在單獨的JBoss模塊中獲得了API和Impl,那麼它們確實在單獨的類加載器中。 MANIFEST中的impl的module.xml是否確定它應該「導入」WAR類加載器? – Ken

+0

@Ken不,不是,但應該嗎?我的意思是,我想在自己的類加載器中使用impl,ApplicationScoped應該只來自一個類加載器。 –

回答

0

我想這個問題,the one in JBoss forums有聯繫,但如果他們不...

可能的原因是:你的獨立模塊不聲明對<module name="javax.annotation.api"/>這也正是@PostConstruct來自依賴性。加上它,應該解決麻煩。 這似乎是JVM的行爲(as explained in this SO question) - 如果您在運行時錯過了依賴關係,程序仍然會執行但忽略註釋。作爲一種解決方法(如果上述不起作用或者你不能這樣做) - 你可以使用初始化方法。這是CDI創建對象後立即執行的方法。

+0

他們確實聯繫在一起:) –