将spring配置打包写好
书写beanRefContext.xml
demo
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="no" default-lazy-init="false"
default-dependency-check="none">
<bean id="businessBeanFactory"
class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg>
<list>
<value>applicationContext-datasource.xml</value>
<value>applicationContext-hibernate.xml</value>
<value>applicationContext-service.xml</value>
<value>applicationContext-xstream.xml</value>
</list>
</constructor-arg>
</bean>
<bean id="ear.context"
class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg>
<list>
<value>applicationContext-quartz.xml</value>
<value>applicationContext-ibmmq-client.xml</value>
<value>applicationContext-jms-client.xml</value>
<value>applicationContext-xstream.xml</value>
<value>applicationContext-mail.xml</value>
</list>
</constructor-arg>
</bean>
</beans>
此jar包命名为 service.jar包,service.jar包位于ear包中并确保ejb的ejb.manifest.classpath中存在
businessBeanFactory 提供给ejb
在 spring对ejb支持的类中
public void setMessageDrivenContext(
MessageDrivenContext messageDrivenContext) {
super.setMessageDrivenContext(messageDrivenContext);
String locatorFactorySelector = null;
String beanFactoryLocatorKey = null;
try {
locatorFactorySelector = (String) new JndiTemplate()
.lookup("java:comp/env/locatorFactorySelector");
} catch (NamingException e) {
e.printStackTrace();
}
setBeanFactoryLocator(locatorFactorySelector != null ? ContextSingletonBeanFactoryLocator
.getInstance(locatorFactorySelector)
: ContextSingletonBeanFactoryLocator.getInstance());// 指定默认的beanRefContext.xml
try {
beanFactoryLocatorKey = (String) new JndiTemplate()
.lookup("java:comp/env/beanFactoryLocatorKey");
} catch (NamingException e) {
e.printStackTrace();
}
setBeanFactoryLocatorKey(beanFactoryLocatorKey != null ? beanFactoryLocatorKey
: "businessBeanFactory");
}
ejb配置
<description>ejb</description>
<display-name>ejb</display-name>
<enterprise-beans>
<message-driven id="MessageDriven_1">
<description>yourEJB</description>
<display-name>Name for yourEJB</display-name>
<ejb-name>yourEJB</ejb-name>
<ejb-class>com.corp.YourEJB</ejb-class>
<transaction-type>Bean</transaction-type>
<message-driven-destination>
<destination-type>javax.jms.Queue</destination-type>
</message-driven-destination>
<env-entry>
<env-entry-name>locatorFactorySelector</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>classpath*:beanRefContext.xml</env-entry-value>
</env-entry>
<env-entry>
<env-entry-name>beanFactoryLocatorKey</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>businessBeanFactory</env-entry-value>
</env-entry>
</message-driven>
</enterprise-beans>
<assembly-descriptor id="AssemblyDescriptor_1">
</assembly-descriptor>
</ejb-jar>
//传入参数指定加载的bean工厂的名字
ear.context 提供给web
service.jar必须存在web的lib下或者在manifest.classpath
ejb中这样加载
<context-param>
<param-name>locatorFactorySelector</param-name>
<param-value>classpath*:beanRefContext.xml</param-value>
</context-param>
<context-param>
<param-name>parentContextKey</param-name>
<param-value>ear.context</param-value>
</context-param>
<!-- 但我不知道再加载另外的配置能否跟其融和在一起?
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-blank.xml</param-value>
</context-param>
-->
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>