简体   繁体   中英

Initialize @EJB fields

I am newbie in EJB.

I have a class with following fields

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class MyServiceFacadeBean implements MyServiceFacadeLocal, MyServiceFacadeRemote {

    @EJB(name = "ejb/CatalogService")
    private CatalogService catalogService = null;
    ....
}

I have instantiated the object while injecting it implementation in Sring IOC:

<bean id="contestServiceFacade" class="my.company.service.facade.contest.ejb.MyServiceFacadeBean">
 </bean>

but after invoke it methods in my controllers I get NullPointerException on catalogService field. So how should be my facade correctly instantiated?

UPDATE 1 : I have tried to plug bean with another approach asked here . Maybe it will help while answering this question.

UPDATE 2: I cant change sources of mine EJBs but can do it for mine controllers.

If you are using JBoss 5 you can't put the EJB in the war. You need to create a ear file with the EJB jar and the war file (+the lib jars). More information about ear can be find in the JavaEE tutorial ( http://docs.oracle.com/javaee/5/tutorial/doc/bnaby.html ).

When the EJBs are correctly packaged in the ear they will be started by Jboss when deployed.

The Spring will be able to access them via JDNI as describe here: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/ejb.html

The other option is to switch to Jboss 7 as there EJB can be in war . But this will required some effort as they change loth of thing in Jboss 7.

If you want Spring to inject properties in your EJB, you need to use an Interceptor on your class:

@Interceptors(SpringBeanAutowiringInterceptor.class)
public class MyServiceFacadeBean ... {

        @Autowired
        private CatalogService catalogService;

        ...
}

You are mixing EJB and Spring beans. The annotation @EJB is to get enterprise java beans not a Spring bean.
You should change to an @Autowired annotation or change the CatalogService to be an EJB (with the correct EJB annotations in the class) and

<context:component-scan base-package="com.foo" />

in the xml file.

Also note that you'll need a container that support EJB like Glassfish, JBoss (Tomcat is not).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM