spring配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="DutyPeriod" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="aircrew-ear/DutyPeriodBean/remote"></property>
<property name="jndiEnvironment">
<props>
<prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop>
<prop key="java.naming.factory.url.pkgs">org.jboss.naming:org.jnp.interfaces</prop>
<prop key="java.naming.provider.url">10.95.18.47:1099</prop>
</props>
</property>
</bean>
</beans>
使用spring注入远程接口bean
注入
public class Test{
@Autowired
private DutyPeriod dutyPeriod;
}
依赖包pom导入
<dependency>
<groupId>org.jboss.client</groupId>
<artifactId>jbossall-client</artifactId>
<version>4.2.3.GA</version>
</dependency>
上面是基于spring配置的使用方式
下面是基于代码的使用方式
// 设置jndi配置
Hashtable<String, String> map = new Hashtable<String, String>();
map.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
map.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
map.put("java.naming.provider.url", "jnp://10.95.18.38:1099"));
InitialContext ctx = new javax.naming.InitialContext(map);
// jndi查找接口
Object svc = ctx.lookup(jndiName);
if (svc != null) {
logger.info("远程接口获取成功");
} else {
logger.warn("远程接口获取失败");
}