简体   繁体   中英

how to import contexts file in dispatcher servlet.xml

I have created a maven multi-module project, they are MVCLayer, ServiceLayer and DAOLayer.

In DAOLayer I have applicationContext.xml under src/main/resources which looks like below

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

In ServiceLayer, serviceContext.xml is under src/main/resources which looks like below

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

And in MVCLayer, MVC-Dispatcher-servlet.xml under WEB-INF is like below

<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 after all these configuration when I am running the project MVCLayer in tomcat from eclipse I am getting this error.

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.GenericSer vlet.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(H ttp11Processor.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)

When I am doing a crud operation from ServiceLayer by creating main class its working fine but whenever am running MVCLayer, am getting this error So please help me out on how to configure it

And MVCLayer pom.xml is like below

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

it seems you have problem in classpath add to project alterantivily you can put import in WEB-INF or in web.xml Do such

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

Looks like you're having a path issue, Spring can't find your DB.properties

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

...

Since you are running this in a servlet container, the following directories will be in your classpath:

WEB-INF/classes
WEB-INF/lib

When your application is deployed, DB.properties needs to be in one of those two locations (preferably at their root since your mapping is /Db.properties). If you're using a standard Maven project structure, with src/main/resources , just make sure your DB.properties is in that resources directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM