簡體   English   中英

Spring ApplicationContext,包含來自應用程序中不同項目的多個上下文xml文件

[英]Spring ApplicationContext with multiple context xml files from different projects in an application

我的應用程序中有兩個不同的項目:數據,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)​

您可以在web.xml加載聲明的多個spring上下文

<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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM