开发中有一个新项目需要调用现存的EJB服务,新项目有Spring容器,参考文档后使用JNDI方式请求EJB服务。解决思路如下。
首先是配置JNDI服务的属性文件,application.local.properties。
######################################################### # JNDI ######################################################### naming.initial=org.jnp.interfaces.NamingContextFactory naming.pkgs=org.jboss.naming:org.jnp.interfaces naming.root=sg-service-ear-1.0.0-M3-SNAPSHOT naming.url=jnp://172.16.0.55:1099
naming.root是ejb发布后的服务名。naming.url是EJB服务地址,1099是默认端口。
在Spring配置文件applicationContext.xml中引入该属性文件
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property name="ignoreResourceNotFound" value="true" /> <property name="locations"> <list> <value>classpath:jdbc.properties</value> <!-- 本地开发 --> <value>classpath:application.local.properties</value> <!-- 服务器环境 --> <value>file:/var/mms/application.server.properties</value> </list> </property> </bean>
加入一个配置文件管理商业接口 modules-remote.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"> <util:map id="jndiEnv"> <entry key="java.naming.factory.initial" value="${naming.initial}" /> <entry key="java.naming.factory.url.pkgs" value="${naming.pkgs}" /> <entry key="java.naming.provider.url" value="${naming.url}" /> </util:map> <jee:remote-slsb id="orderInfoService" jndi-name="${naming.root}/OrderInfoServiceBean/remote" business-interface="com.test.trade.service.OrderInfoService" resource-ref="true" cache-home="true" lookup-home-on-startup="false" environment-ref="jndiEnv"> </jee:remote-slsb> </beans>
OrderInfoService是一个商业接口,OrderInfoServiceBean是一个具体的实现。
开发中注入这个远程接口即可
private OrderInfoService _orderInfoService;
@Autowired
public void setOrderInfoService(OrderInfoService orderInfoService) {
_orderInfoService = orderInfoService;
}
为了应用以上服务需要导入一些Jboss的jar包,因为没有使用maven,手动引入以下jar包
jbossall-client.jar
jboss-aspect-library-jdk50.jar
jboss-ejb3x.jar
以上jar在{JBOSS_ROOT}/client 下都可以找到。
have fun fellows.