kiran reddy
kiran reddy

Reputation: 1

How to access application scoped cdi bean packed as jboss shared library

I have to use an application scoped cdi bean present in jboss shared libraries in my ear application.

Example:

jboss\modules\com\test\test.jar

In test.jar I have one application scoped bean

@ApplicationScoped

public class Test {

    @Inject
    SomeClass someClass;

    @PostConstructor
    public void init() {
        someClass.doSomething();
    }

    public void testMethod() {
    }
}

myapp.ear → ejb.jar

In ejb.jar I have one class which initializes Test application scoped bean.

public class Another {

    @Inject
    Test test;

    public void myMethod() {
        test.testMethod();
    }
}

When I test this example, I'll get null pointer exception as I'm trying to inject application scoped bean which is out side an ear application.

Note: I cannot have test.jar in my ear lib directory, as per my requirement it will be delivered as jboss shared library.

Any ideas how to access application scoped bean?

Upvotes: 0

Views: 1019

Answers (1)

A.Panzer
A.Panzer

Reputation: 391

Add beans.xml to META-INF directory of your test.jar. In this case container will scan test.jar file for CDI beans. This is also strange that you get NPE. You would get "unsatisfied dependencies" if injected bean was not found. Probably you instantiate Test bean incorrectly. For example, via the New operator.

Upvotes: 1

Related Questions