Spring ApplicationContext,包含来自应用程序中不同项目的多个上下文xml文件

时间:2015-02-28 18:32:42

标签: java spring spring-mvc

我的应用程序中有两个不同的项目:data,web
我想在Web应用程序层中自动连接我的数据中定义的dao 所以我在web中创建了一个主applicationContext.xml文件并导入了整个Spring 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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <import resource="data/src/main/resources/spring/applicationContext-data.xml"/>
        <import resource="applicationContext.xml"/>

</beans>
@Configuration("file:**/Master-Module.xml")
public class MyService{
    @Autowired
    public  IMyDAO myDao;
    Practice practice1 = myDao.findById("3900");

接口IMyDAO在applicationContext-data.xml

中定义

当我将应用程序部署到Websphere并运行它时,我得到一个NULL指针异常。

ID存在于数据库中 它作为独立项目运行时在Maven中成功构建。

以下是我得到的错误:

2/28/15 12:45:13:902 EST] 0000009d ServletWrappe I com.ibm.ws.webcontainer.servlet.ServletWrapper init SRVE0242I: [webapp-7_0-SNAPSHOT_war] [/web] [/WEB-INF/views/common/footer.jsp]: Initialization successful.
[2/28/15 12:45:13:762 EST] 0000009d SystemErr     R java.lang.NullPointerException
[2/28/15 12:45:13:762 EST] 0000009d SystemErr     R     at cic.web.service.MyPracticeService.retrievPracticeDetails(MyPracticeService.java:51)
[2/28/15 12:45:13:762 EST] 0000009d SystemErr     R     at cic.web.controllers.MyPracticeController.practicesList(MyPracticeController.java:42)
[2/28/15 12:45:13:762 EST] 0000009d SystemErr     R     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[2/28/15 12:45:13:762 EST] 0000009d SystemErr     R     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
[2/28/15 12:45:13:762 EST] 0000009d SystemErr     R     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
[2/28/15 12:45:13:762 EST] 0000009d SystemErr     R     at java.lang.reflect.Method.invoke(Method.java:611)​

1 个答案:

答案 0 :(得分:0)

您可以加载多个弹簧上下文,然后在web.xml

中声明
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>

<listener>
   <listener-class>
        org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>