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

how to import contexts file in dispatcher servlet.xml

我创建了一个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>

Sp 完成所有这些配置后,当我从 eclipse 运行 tomcat 中的项目 MVCLayer 时,我收到此错误。

SEVERE: Allocate exception for servlet MVC-Dispatcher java.io.FileNotFoundException: Could not open ServletContext resource [/DB.properties] at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141) at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:143) at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98) at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:175) at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:156) at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:80) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:265) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:162) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462) at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:663) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:629) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:677) at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:548) at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:489) at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) at javax.servlet.GenericServlet.init(GenericServlet.java:160) at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1189) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1103) at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:813) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166) at 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) at java.lang.Thread.run(Unknown Source)

当我通过创建 main class 从 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>

看来你在添加到项目的类路径中有问题 您可以交替地在 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>

您似乎遇到了路径问题,Spring 找不到您的 DB.properties

SEVERE: Allocate exception for servlet MVC-Dispatcher java.io.FileNotFoundException: Could not open ServletContext resource [/DB.properties]

...

由于您在 servlet 容器中 运行 this,以下目录将在您的类路径中:

WEB-INF/classes
WEB-INF/lib

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