如何在调度程序servlet.xml中导入上下文文件

时间:2015-02-22 10:07:19

标签: java spring spring-mvc java-ee

我创建了一个maven多模块项目,它们是MVCLayer,ServiceLayer和DAOLayer。

在DAOLayer中,我在src / main / resources下面有applicationContext.xml,如下所示

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context.xsd
              http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.sharique" />
    <tx:annotation-driven />
    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>/DB.properties</value>
        </property>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="${JDBC.DRIVERCLASSNAME}" />
        <property name="url" value="${JDBC.URL}" />
        <property name="username" value="${USERNAME}" />
        <property name="password" value="${PASSWORD}" />
        <!-- <property name="initialSize" value="2" /> <property name="maxActive" 
            value="5" /> -->
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.sharique.domainObjects" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${HIBERNATE.DAILECT}</prop>
                <prop key="hibernate.hbm2ddl.auto">${HBM2DDL.AUTO.UPDATE}</prop>
                <prop key="hibernate.show_sql">${HIBERNATE.SHOW_SQL}</prop>
            </props>
        </property>
    </bean>

      <bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager" 
    p:sessionFactory-ref="sessionFactory"></bean>
</beans>

在ServiceLayer中,serviceContext.xml位于src / main / resources下,如下所示

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context.xsd
              http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.sharique" />
    <tx:annotation-driven />
    <import resource="classpath*:/applicationContext.xml"/>
    <!--   <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>DB.properties</value>
        </property>
    </bean>  --> 

    <!--  <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="${JDBC.DRIVERCLASSNAME}" />
        <property name="url" value="${JDBC.URL}" />
        <property name="username" value="${USERNAME}" />
        <property name="password" value="${PASSWORD}" />
        <property name="initialSize" value="2" /> <property name="maxActive" 
            value="5" />
    </bean>  -->

    <!--  <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.sharique.domainObjects" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${HIBERNATE.DAILECT}</prop>
                <prop key="hibernate.hbm2ddl.auto">${HBM2DDL.AUTO.UPDATE}</prop>
                <prop key="hibernate.show_sql">${HIBERNATE.SHOW_SQL}</prop>
            </props>
        </property>
    </bean>  -->

     <bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager" 
    p:sessionFactory-ref="sessionFactory"></bean>
</beans>

在MVCLayer中,WEB-INF下的MVC-Dispatcher-servlet.xml如下所示

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 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
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.sharique.controller" />
    <mvc:annotation-driven />
    <import resource="classpath*:/serviceContext.xml"/>



    <bean id="ViewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass">
            <value>org.springframework.web.servlet.view.JstlView</value>
        </property>
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

在所有这些配置之后,当我从eclipse运行tomcat中的项目MVCLayer时,我得到了这个错误。

  

SEVERE:为servlet MVC-Dispatcher分配异常   java.io.FileNotFoundException:无法打开ServletContext资源   [/DB.properties] at   org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141)     在   org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:143)     在   org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98)     在   org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:175)     在   org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:156)     在   org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:80)     在   org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:265)     在   org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:162)     在   org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606)     在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462)     在   org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:663)     在   org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:629)     在   org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:677)     在   org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:548)     在   org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:489)     在   org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)     在javax.servlet.GenericServlet.init(GenericServlet.java:160)at   org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1189)     在   org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1103)     在   org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:813)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)     在   org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)     在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)     在   org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)     在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)     在   org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)     在   org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:188)     在   org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:166)     在   org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:302)     at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)     at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)     在java.lang.Thread.run(未知来源)

当我通过创建主类来从ServiceLayer执行crud操作时它工作正常但是每当运行MVCLayer时,我都会收到此错误 所以请帮我解决一下如何配置它

MVCLayer pom.xml如下所示

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.test</groupId>
        <artifactId>Roomies</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>MVCLayer</artifactId>

    <packaging>war</packaging>
    <name>MVCLayer Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>javax.servlet.jsp.jstl-api</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>javax.servlet.jsp.jstl</artifactId>
            <version>1.2.1</version>
            <exclusions>
                <!-- jstl-api was adding selvlet-api 2.5 and jsp-api -->
                <exclusion>
                    <artifactId>jstl-api</artifactId>
                    <groupId>javax.servlet.jsp.jstl</groupId>
                </exclusion>
            </exclusions>
        </dependency>

         <dependency>
            <groupId>com.test</groupId>
            <artifactId>ServiceLayer</artifactId>
            <version>0.0.1-SNAPSHOT</version>           
        </dependency>

    </dependencies>
    <build>
        <finalName>MVCLayer</finalName>
    </build>
</project>

2 个答案:

答案 0 :(得分:0)

似乎你在classpath添加到项目时遇到问题 您可以将导入放在WEB-INF或web.xml中 这样做

 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value> 
            /WEB-INF/dispatcher-servlet.xml,           
            /WEB-INF/spring-mail.xml,           
        </param-value>
    </context-param>

答案 1 :(得分:0)

看起来你有路径问题,Spring无法找到你的DB.properties

  

SEVERE:为servlet MVC-Dispatcher分配异常   java.io.FileNotFoundException:无法打开ServletContext资源   [/DB.properties]

     

...

由于您在servlet容器中运行它,因此以下目录将位于类路径中:

WEB-INF/classes
WEB-INF/lib

部署应用程序时,DB.properties需要位于这两个位置之一(最好是在它们的根目录,因为您的映射是/Db.properties)。如果您使用标准Maven项目结构src/main/resources,请确保您的DB.properties位于该资源目录中。