我这里所用的ejb全部为jboss ejb .
实现同普通bean一样的通过getBean()的方式来获取Ejb,当然这个实用性可能不大。
在spring中,ProxyFactoryBean可以返回一个目标对象的实例,它实现 FactoryBean接口。FactoryBean接口提供了三个方法。
getObject()//返回一个对象实例
getObjectType()
isSingleton()
下面看看EJBProxy类,很简单
package test ;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.util.Assert;
import org.xiaohongli.springejb.util.PropUtil;
public class EJBProxy
implements FactoryBean{
public static final String GLOBAL_SUFFIX = "*";
private boolean singleton = true;
public void setSingleton(boolean singleton) {
this.singleton = singleton;
}
public Object getObject() throws BeansException {
return this.lookup() ;
}
public boolean isSingleton() {
return this.singleton;
}
private String jndi ;
private String java_naming_factory_initial = "org.jnp.interfaces.NamingContextFactory";
private String java_naming_provider_url = "localhost:1099";
private String java_naming_factory_url_pkgs = "org.jboss.naming" ;
private String jndi_properties_file ;
public String getJndi_properties_file() {
return jndi_properties_file;
}
public void setJndi_properties_file(String jndi_properties_file) {
this.jndi_properties_file = jndi_properties_file;
}
public String getJava_naming_factory_initial() {
return java_naming_factory_initial;
}
public void setJava_naming_factory_initial(String java_naming_factory_initial) {
this.java_naming_factory_initial = java_naming_factory_initial;
}
public String getJava_naming_factory_url_pkgs() {
return java_naming_factory_url_pkgs;
}
public void setJava_naming_factory_url_pkgs(String java_naming_factory_url_pkgs) {
this.java_naming_factory_url_pkgs = java_naming_factory_url_pkgs;
}
public String getJava_naming_provider_url() {
return java_naming_provider_url;
}
public void setJava_naming_provider_url(String java_naming_provider_url) {
this.java_naming_provider_url = java_naming_provider_url;
}
public String getJndi() {
return jndi;
}
public void setJndi(String jndi) {
this.jndi = jndi;
}
PropUtil propUtil ;
private Object lookupByStringProperties(){
String pkgs = this.getJava_naming_factory_url_pkgs();
String url = this.getJava_naming_provider_url() ;
String init = this.getJava_naming_factory_initial() ;
Assert.notNull(pkgs,"Warning :The java.naming.factory.url.pkgs cannot null!");
Assert.notNull(url,"Warning :The java.naming.provider.url cannot null!");
Assert.notNull(init,"Warning :The java.naming.factory.url.initial cannot null!");
Assert.notNull(this.getJndi(),"Warning :The jndi cannot null!");
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", init);
props.setProperty("java.naming.provider.url", url);
props.setProperty("java.naming.factory.url.pkgs", pkgs);
InitialContext ctx;
try {
ctx = new InitialContext(props);
Object lookupObject = ctx.lookup(this.getJndi());
return lookupObject ;
} catch (NamingException e) {
e.printStackTrace() ;
}
return null ;
}
private Object lookupByFileProperties(){
String file = this.getJndi_properties_file() ;
propUtil = new PropUtil(file) ;
String init = propUtil.getProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory") ;
String url = propUtil.getProperty("java.naming.provider.url","localhost:1099") ;
String pkgs =propUtil.getProperty("java.naming.factory.url.pkgs","org.jboss.naming") ;
//首先在配置文件中查找jndi,如果没有则属性中查找
String _jndi = propUtil.getProperty("jndi") ;
if(null == _jndi) _jndi = this.getJndi() ;
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", init);
props.setProperty("java.naming.provider.url", url);
props.setProperty("java.naming.factory.url.pkgs", pkgs);
Assert.notNull(_jndi,"Warning :The jndi cannot null!");
InitialContext ctx;
try {
ctx = new InitialContext(props);
Object lookupObject = ctx.lookup(this.getJndi());
return lookupObject ;
} catch (NamingException e) {
e.printStackTrace() ;
}
return null ;
}
private synchronized Object lookup(){
if(null != this.getJndi_properties_file())
return this.lookupByFileProperties() ;
else
return this.lookupByStringProperties() ;
}
public Class getObjectType() {
return null;
}
}
其实,就只需要实现FactoryBean 的getBean就行了
然后就可以通过在applicationContext.xml中配置
<bean id="SpringEJBBean" class="test.EJBProxy">
<property name="jndi">
<value>SpringEJBBean/remote</value>
</property>
//可指定jndi配置文件,那其他的就不用了
<!--property name="jndi_properties_file">
<value>jndi.properties</value>
</property-->
<property name="java_naming_factory_initial">
<value>org.jnp.interfaces.NamingContextFactory</value>
</property>
<property name="java_naming_provider_url">
<value>localhost:1099</value>
</property>
<property name="java_naming_factory_url_pkgs">
<value>org.jboss.naming</value>
</property>
</bean>
消息驱动bean 类似