简体   繁体   English

初始化@EJB字段

[英]Initialize @EJB fields

I am newbie in EJB. 我是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: 我在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. 但是在我的控制器中调用它的方法后,我在catalogService字段上得到了NullPointerException So how should be my facade correctly instantiated? 那么如何正确地实例化我的外观?

UPDATE 1 : I have tried to plug bean with another approach asked here . 更新1 :我试图用这里要求的另一种方法插入bean。 Maybe it will help while answering this question. 回答这个问题可能会有所帮助。

UPDATE 2: I cant change sources of mine EJBs but can do it for mine controllers. 更新2:我无法更改我的EJB的来源,但可以为我的控制器执行此操作。

If you are using JBoss 5 you can't put the EJB in the war. 如果您使用的是JBoss 5,则无法将EJB投入战争。 You need to create a ear file with the EJB jar and the war file (+the lib jars). 您需要使用EJB jarwar文件(+ lib jar)创建一个ear文件。 More information about ear can be find in the JavaEE tutorial ( http://docs.oracle.com/javaee/5/tutorial/doc/bnaby.html ). 可以在JavaEE教程( http://docs.oracle.com/javaee/5/tutorial/doc/bnaby.html )中找到有关ear更多信息。

When the EJBs are correctly packaged in the ear they will be started by Jboss when deployed. 当EJB正确地包装在耳朵中时,它们将在部署时由Jboss启动。

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 Spring可以通过JDNI访问它们,如下所述: 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 . 另一个选择是切换到Jboss 7,因为EJB可能会陷入war But this will required some effort as they change loth of thing in Jboss 7. 但这需要一些努力,因为它们改变了Jboss 7中的许多东西。

If you want Spring to inject properties in your EJB, you need to use an Interceptor on your class: 如果要让Spring在EJB中注入属性,则需要在类上使用Interceptor:

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

        @Autowired
        private CatalogService catalogService;

        ...
}

You are mixing EJB and Spring beans. 您正在混合EJB和Spring Bean。 The annotation @EJB is to get enterprise java beans not a Spring bean. 注释@EJB是为了获取企业Java Bean,而不是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 您应该更改为@Autowired注释或将CatalogService更改为EJB(在类中具有正确的EJB注释),然后

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

in the xml file. 在xml文件中。

Also note that you'll need a container that support EJB like Glassfish, JBoss (Tomcat is not). 还要注意,您将需要一个支持EJB的容器,例如Glassfish,JBoss(不是Tomcat)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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