注入Java类并重新绑定到InitialContext

时间:2015-12-28 19:50:46

标签: java-ee dependency-injection jboss module wildfly

我尝试将一个名为SomeProcess的java类重新绑定到JNDI。这种绑定发生在名为ResourceBinder的单例bean中。

InitialContext ctx = new InitialContext();
ctx.rebind("SomeProcess", new SomeProcess());
log.info(">>>>> SomeProcess bound");

稍后,此类将注入其他名为SomeServiceBean的ejb中。

@Inject
private SomeProcess process;

但是,我收到此错误:

  

org.jboss.naming.remote.protocol.NamingIOException:无法重新绑定   [根异常是java.io.IOException:   抛出java.lang.ClassNotFoundException:   来自[Module。的de.brockhaus.userMgmt.control.process.SomeProcess   " org.jboss.remote命名:主"来自本地模块加载器@ ed17bee   (发现者:本地模块查找器@ 2a33fae0(根:C:\ Program   文件\ jboss的\ wildfly \模块,C:\ PROGRAM   Files \ jboss \ wildfly \ modules \ system \ layers \ base))]] at   org.jboss.naming.remote.client.ClientUtil.namingException(ClientUtil.java:49)   2015-12-28 20:33:02,302调试[主要]   de.brockhaus.userMgmt.util.jndi.BeanLocator:寻找:   /BRO130_3_7_ResourceBinder-solution/SomeServiceBean!de.brockhaus.userMgmt.boundary.SomeService     在   org.jboss.naming.remote.protocol.v1.Protocol $ 3.execute(Protocol.java:306)     在   org.jboss.naming.remote.protocol.v1.Protocol $ 3.execute(Protocol.java:265)     在   org.jboss.naming.remote.protocol.v1.RemoteNamingStoreV1.rebind(RemoteNamingStoreV1.java:116)     在   org.jboss.naming.remote.client.HaRemoteNamingStore $ 3.operation(HaRemoteNamingStore.java:302)     在   org.jboss.naming.remote.client.HaRemoteNamingStore $ 3.operation(HaRemoteNamingStore.java:299)     在   org.jboss.naming.remote.client.HaRemoteNamingStore.namingOperation(HaRemoteNamingStore.java:137)     在   org.jboss.naming.remote.client.HaRemoteNamingStore.rebind(HaRemoteNamingStore.java:298)     在   org.jboss.naming.remote.client.RemoteContext.rebind(RemoteContext.java:163)     在   org.jboss.naming.remote.client.RemoteContext.rebind(RemoteContext.java:170)     在javax.naming.InitialContext.rebind(InitialContext.java:433)at   de.brockhaus.userMgmt.boundary.ResourceBinder.bindResources(ResourceBinder.java:32)     在   de.brockhaus.userMgmt.boundary.SomeServiceTest.bindResources(SomeServiceTest.java:21)     在   de.brockhaus.userMgmt.boundary.SomeServiceTest.main(SomeServiceTest.java:13)   引起:java.io.IOException:java.lang.ClassNotFoundException:   来自[Module。的de.brockhaus.userMgmt.control.process.SomeProcess   " org.jboss.remote命名:主"来自本地模块加载器@ ed17bee   (发现者:本地模块查找器@ 2a33fae0(根:C:\ Program   文件\ jboss的\ wildfly \模块,C:\ PROGRAM   Files \ jboss \ wildfly \ modules \ system \ layers \ base))]   org.jboss.naming.remote.protocol.v1.Protocol $ 3.handleServerMessage(Protocol.java:329)     在   org.jboss.naming.remote.protocol.v1.RemoteNamingServerV1 $ MessageReciever $ 1.run(RemoteNamingServerV1.java:73)     在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)     在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)     在java.lang.Thread.run(Thread.java:745)引起:   抛出java.lang.ClassNotFoundException:   来自[Module。的de.brockhaus.userMgmt.control.process.SomeProcess   " org.jboss.remote命名:主"来自本地模块加载器@ ed17bee   (发现者:本地模块查找器@ 2a33fae0(根:C:\ Program   文件\ jboss的\ wildfly \模块,C:\ PROGRAM   Files \ jboss \ wildfly \ modules \ system \ layers \ base))]   org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:205)     在   org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:455)     在   org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:404)     在   org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:385)     在   org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:130)     at java.lang.Class.forName0(Native Method)at   java.lang.Class.forName(Class.java:348)at   org.jboss.marshalling.AbstractClassResolver.loadClass(AbstractClassResolver.java:131)     在   org.jboss.marshalling.AbstractClassResolver.resolveClass(AbstractClassResolver.java:112)     在   org.jboss.marshalling.river.RiverUnmarshaller.doReadClassDescriptor(RiverUnmarshaller.java:948)     在   org.jboss.marshalling.river.RiverUnmarshaller.doReadNewObject(RiverUnmarshaller.java:1255)     在   org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:276)     在   org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:209)     在   org.jboss.marshalling.AbstractObjectInput.readObject(AbstractObjectInput.java:41)     在   org.jboss.naming.remote.protocol.v1.Protocol $ 3.handleServerMessage(Protocol.java:327)     ......还有4个

为了解决这个问题,我创建了一个wilfly模块de.brockhaus.userMgmt,其中我已经放置了maven构建jar。这是module.xml的内容

<properties>
    <property name="jboss.api" value="private"/>
</properties>

<resources>
    <resource-root path="BRO130_3_7_ResourceBinder-solution.jar"/>
</resources>

<dependencies>
    <module name="org.apache.log4j"/>
</dependencies>

我在WildFly 10版本中修改了模块module.xml的{​​{1}}。

org.jboss.remote-naming

但现在问题不同了:13:36:01,016 ERROR

  

[org.jboss.as.naming](pool-1-thread-1)WFLYNAM0008:意外   内部错误:java.lang.UnsupportedOperationException:WFLYNAM0043:   命名上下文是只读的   org.jboss.as.naming.WritableServiceBasedNamingStore.requireOwner(WritableServiceBasedNamingStore.java:161)     在   org.jboss.as.naming.WritableServiceBasedNamingStore.rebind(WritableServiceBasedNamingStore.java:109)     在org.jboss.as.naming.NamingContext.rebind(NamingContext.java:301)     在   org.jboss.naming.remote.protocol.v1.Protocol $ 3.handleServerMessage(Protocol.java:335)     在   org.jboss.naming.remote.protocol.v1.RemoteNamingServerV1 $ MessageReciever $ 1.run(RemoteNamingServerV1.java:73)     在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)     在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)     在java.lang.Thread.run(Thread.java:745)

     

13:36:01,459 ERROR [org.jboss.as.naming](pool-1-thread-2)   WFLYNAM0008:意外的内部错误:   java.lang.UnsupportedOperationException:WFLYNAM0043:命名上下文   是只读的   org.jboss.as.naming.WritableServiceBasedNamingStore.requireOwner(WritableServiceBasedNamingStore.java:161)     在   org.jboss.as.naming.WritableServiceBasedNamingStore.rebind(WritableServiceBasedNamingStore.java:109)     在org.jboss.as.naming.NamingContext.rebind(NamingContext.java:301)     在   org.jboss.naming.remote.protocol.v1.Protocol $ 3.handleServerMessage(Protocol.java:335)     在   org.jboss.naming.remote.protocol.v1.RemoteNamingServerV1 $ MessageReciever $ 1.run(RemoteNamingServerV1.java:73)     在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)     在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:617)     在java.lang.Thread.run(Thread.java:745)

     

13:36:01,590 INFO [org.jboss.ejb.client](pool-1-thread-3)JBoss EJB   客户端版本2.1.3.Final 13:36:01,918 ERROR [stderr](EJB默认 -   1)log4j:WARN没有找到记录器的appender   (de.brockhaus.userMgmt.control.activity.StepOne)。

     

13:36:01,920 ERROR [stderr](EJB默认值 - 1)log4j:WARN Please   正确初始化log4j系统。

     

13:36:01,921 ERROR [stderr](EJB默认值 - 1)log4j:WARN请参阅   http://logging.apache.org/log4j/1.2/faq.html#noconfig了解更多信息。

1 个答案:

答案 0 :(得分:1)

当您按照所描述的配方时,HOW TO EXPOSE AND INJECT A POJO ...INTO AN EJB 3中描述的模式在未修改的WildFly服务器中工作正常。特别是,你应该使用:

@Resource(mappedName="SomeProcess")
private SomeProcess process;

而不是@Inject。

@Resource和@Inject不可互换。

也就是说,这篇文章在现代Java EE年代已经相当陈旧,几乎早于正式引入CDI的Java EE 6。