2011-09-30 61 views
14

注入豆我有兩個罐A和B,其中A取決於B.從不同的罐中焊接

罐B具有單一的類:

@ApplicationScoped 
public class MyManagedBean { 

private String user; 

public MyManagedBean(){ 
    //Constructor necesary for CDI 
} 

@Inject 
public MyManagedBean(@Named("user") String user){ 
    this.user = user; 
} 

... 
} 

罐A(更精確地說,一個EJB JAR)有一個bean:

@ApplicationScoped 
public class AnotherManagedBean { 

public AnotherManagedBean(){ 
    //Constructor necesary for CDI 
} 

@Inject 
public AnotherManagedBean(MyManagedBean bean){ 
    ... 
} 
} 

而且配置Bean與@Produces方法:

@ApplicationScoped 
public class ConfigurationBean { 

public ConfigurationBean(){ 
    //Constructor necesary for CDI 
} 

@Produces 
@Named("user") 
public String getUser(){ 
    return "myUser"; 
} 
} 

然而,當我部署與兩個罐子我得到這個例外的EAR:

SEVERE: Exception while loading the app : WELD-001408 Unsatisfied dependencies for type [String] with qualifiers [@Named] at injection point [[parameter 1] of [constructor] @Inject public com.example.MyManagedBean(String)] 
org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [String] with qualifiers [@Named] at injection point [[parameter 1] of [constructor] @Inject public com.example.MyManagedBean(String)] 
    at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:270) 
    at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:106) 
    at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:129) 
    at org.jboss.weld.bootstrap.Validator.validateBeans(Validator.java:351) 
    at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:336) 
    at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:396) 
    at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:190) 
    at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:128) 
    at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:306) 
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:462) 
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:240) 
    at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:382) 
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:355) 
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:370) 
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1064) 
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1200(CommandRunnerImpl.java:96) 
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1244) 
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1232) 
    at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:459) 
    at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:209) 
    at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:168) 
    at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:117) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:238) 
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828) 
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725) 
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019) 
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225) 
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) 
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) 
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) 
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) 
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) 
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) 
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71) 
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) 
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) 
    at java.lang.Thread.run(Thread.java:662) 

任何想法?

感謝

+0

我相信這是正確的答案,您的問題http://stackoverflow.com/a/5704973/569051 –

+0

我有相同的問題,讓你找到一個解決辦法我知道。 –

回答

25

確保兩個罐子「豆檔案」 - 即它們META-INF/beans.xml

+2

是的,兩個罐子都有META-INF/beans.xml – narduk

+2

這絕對解決了我的問題,謝謝! –

0

一件事,你必須創建Qulifier註解爲指定究竟哪些應該被注入。

@Qualifier 
@Retention(RUNTIME) 
@Target({METHOD, FIELD, PARAMETER, TYPE}) 
public @interface UserConfiguration { } 

然後..

@Produces 
@UserConfiguration 
@Named("user") 
public String getUser(){ 
    return "myUser"; 
} 

注射..

@Inject 
public MyManagedBean(@UserConfiguration String user){ 
    this.user = user; 
} 

http://docs.jboss.org/weld/reference/1.1.0.Final/en-US/html_single/#d0e1355

+2

如果沒有限定符註釋,它應該有@Default限定符...不是嗎? – narduk

2

我在這個確切同樣的問題,我能得到它看想通了,但我用耳朵來結合罐子。

耳佈局

project.ear 
|-- META-INF 
|  |-- MANIFEST.MF 
|  |-- application.xml* 
|-- one.jar (bean archive) 
|  |-- META-INF 
|  |  |-- beans.xml 
|  |-- <code> 
|-- two.jar (ejb) 
  • 的application.xml

<application> 
    <display-name>test-application</display-name> 
    <module> 
    <ejb>two.jar</ejb> 
    </module> 
    <module> 
    <java>one.jar</java> 
    </module> 
</application> 

這樣做,它由one.jar可在容器中two.jar 。

-kurt