SOA 服务使用 spring 获取空 ejb 实例
SOA service gets null ejb instance using spring
我有一个实现 apache cxf web 服务和 ejb 对象的 maven 项目,我正在尝试使用 spring 注入 ejb 实例,当我 运行 程序时, spring container returns 将 ejb bean 作为 null。我不明白如何将 ejb 实现与 spring bean 联系起来。
该项目有一个 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);
}
}
</pre>
为了实现DI,它还有一个spring配置,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>
也就是业务层,Stateless ejb实现:
@Stateless(mappedName="ejb/ServiceBusiness")
public class ServiceBusinessImpl implements ServiceBusinessLocal
public String sayHello(String request) {
return "hello: " + request;
}
}
</pre>
如何实例化 ejb 对象?
这是我以前用作部分 POC 的 bean 配置文件
<?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>
并如下所示注入 bean。
@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,您可能需要根据您的应用服务器进行相应更改
我有一个实现 apache cxf web 服务和 ejb 对象的 maven 项目,我正在尝试使用 spring 注入 ejb 实例,当我 运行 程序时, spring container returns 将 ejb bean 作为 null。我不明白如何将 ejb 实现与 spring bean 联系起来。
该项目有一个 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); } } </pre>
为了实现DI,它还有一个spring配置,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>
也就是业务层,Stateless ejb实现:
@Stateless(mappedName="ejb/ServiceBusiness") public class ServiceBusinessImpl implements ServiceBusinessLocal public String sayHello(String request) { return "hello: " + request; } } </pre>
如何实例化 ejb 对象?
这是我以前用作部分 POC 的 bean 配置文件
<?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>
并如下所示注入 bean。
@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,您可能需要根据您的应用服务器进行相应更改