我有一个实现 apache cxf Web 服务和 ejb 对象的 maven 项目,我正在尝试使用 spring 注入 ejb 实例,当我运行程序时,spring 容器将 ejb bean 作为空返回。我不明白如何将 ejb 实现与春豆联系起来。
该项目有一个apache cxf实现,这样:
ServiceBindingImpl
@WebService(endpointInterface = "cl.flying.binding.ServiceBinding")
public class ServiceBindingImpl implements ServiceBinding {
private ServiceBusinessLocal serviceEjb
public void setServiceEjb(ServiceBusinessLocal serviceEjb) {
this.serviceEjb = serviceEjb
}
public String sayHello(String request) {
return serviceEjb.sayHello(request);
}
}
It also has a spring configuration in order to achieve DI, applicationContext.xml
<bean id="serviceEjb"
class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean" scope="request">
<property name="jndiName" value="ejb/ServiceBusinessImpl" />
<property name="businessInterface" value="cl.service.business.ServiceBusinessLocal" />
<property name="resourceRef" value="true" />
</bean>
<bean id="ServiceController" class="cl.flying.binding.ServiceBindingImpl">
<property name="serviceEjb" ref="serviceEjb" />
</bean>
这就是业务层,无状态 ejb 实现:
@Stateless(mappedName="ejb/ServiceBusiness")
public class ServiceBusinessImpl implements ServiceBusinessLocal
public String sayHello(String request) {
return "hello: " + request;
}
}
How can I instantiate the ejb object?
Here is the bean configuration file I used as part POC sometime back
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<jee:remote-slsb id="calculatorRemote" business-interface="com.kp.swasthik.remote.CalculatorRemote" jndi-name="java:kp-ejb/Calculator!com.kp.swasthik.remote.CalculatorRemote" lookup-home-on-startup="true" >
<jee:environment>
java.naming.factory.initial=org.jboss.naming.remote.client.InitialContextFactory
java.naming.provider.url=http-remoting://localhost:8080
jboss.naming.client.ejb.context=true
java.naming.security.principal=username
java.naming.security.credentials=password
</jee:environment>
</jee:remote-slsb>
<context:component-scan base-package="com.kp.swasthik.jaxws"></context:component-scan>
<util:properties id="remoteRef">
<prop key="java.naming.factory.initial">org.jboss.naming.remote.client.InitialContextFactory</prop>
<prop key="java.naming.provider.url">remote://localhost:4447</prop>
<prop key="java.naming.factory.url.pkgs">org.jboss.ejb.client.naming</prop>
</util:properties>
</beans>
并注入豆子,如下所示。
@Service
@WebService
public class KPService {
@Autowired
CalculatorRemote calc;
@WebMethod
public int add(int a, int b){
return calc.add(a, b);
}
@WebMethod
public Result subtract(Input num){
return calc.subtract(num);
}
}
注意:上述配置适用于 Wildfly/JBOSS EAP,您可能需要根据应用服务器进行相应更改