简体   繁体   English

从EJB会话Bean调用Spring Bean

[英]Calling Spring Bean from the EJB Session Bean

I want to call my Spring bean from my EJB Session Bean. 我想从EJB会话Bean调用Spring Bean。 Here is an example scenario that I want to achieve. 这是我要实现的示例方案。

Spring Interface: 弹簧接口:

public interface ReqSpring {
    public String processMsg(String msg);
}

Spring Bean: 春豆:

@Component
public class ReqStringImpl implements ReqSpring{

    public String processMsg(String msg) {
        return "Msg ["+msg+"] is processed";
    }
}

EJB Interface: EJB接口:

@Remote
public interface EjbService{
    String echo(String msg);
}

EJB Session Bean: EJB会话Bean:

(please notice that I have used @Autowired over a Spring object) (请注意,我已经在Spring对象上使用了@Autowired

@Stateless(name = "EjbWS", mappedName = "EjbWS")
@WebService(name = "EjbService", portName = "EjbServicePort")
public class EjbServiceBean implements EjbService {

    =====> @Autowired
    =====> private ReqSpring reqSpring;

    public EjbServiceBean() {
    }

    @WebMethod
    public String echo(@WebParam(name="msg")String msg) {
            // This is printing null
        System.out.println("ReqSpring = "+reqSpring);
        return reqSpring.processMsg(msg);
    }
}

My Application failed to load the ReqSpring object from my EJB and always generating NullPointerException . 我的应用程序无法从EJB加载ReqSpring对象,并始终生成NullPointerException Any idea why this is happening? 知道为什么会这样吗?

You can't inject a Spring Bean into a EJB like that because the EJB is not managed by the Spring container. 您不能像那样将Spring Bean注入到EJB ,因为EJB不是由Spring容器管理的。 Anyway, by using the SpringBeanAutowiringInterceptor , you will be able to do it. 无论如何,通过使用SpringBeanAutowiringInterceptor ,您将能够做到这一点。 Try with this annotation : 尝试使用此注释:

@Stateless(name = "EjbWS", mappedName = "EjbWS")
@WebService(name = "EjbService", portName = "EjbServicePort")
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class EjbServiceBean implements EjbService {

    @Autowired
    private ReqSpring reqSpring;

    public EjbServiceBean() {
    }

    @WebMethod
    public String echo(@WebParam(name="msg")String msg) {
        System.out.println("ReqSpring = "+reqSpring);
        return reqSpring.processMsg(msg);
    }
}

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

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