我如何链接两个xml文件

时间:2013-04-08 06:01:08

标签: spring spring-mvc

我有application-context.xml文件和名为UserController2-servlet.xml的子xml文件。文件如下

UserController2-servlet.xml中

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

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>


<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="index.htm">indexController</prop>
        </props>
    </property>
</bean>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="annotatedClasses">
        <list>
            <value>Spring.project2.User</value>
          </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>
</bean>

<bean id="myUserDAO" class="project2.UserDAOImpl">
    <property name="sessionFactory" ref="mySessionFactory"/>
</bean>

<bean name="/user/*.htm" class="project2.UserController2" >
    <property name="userDAO" ref="myUserDAO" />
</bean>

<bean name="indexController"  
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="userForm" />

</beans>

应用context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org  
/dtd/spring-beans.dtd">
<beans>
<bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/fde" />
    <property name="username" value="dgdg" />
    <property name="password" value="dgd" />

</bean>
    </beans>

我如何从application-context.xml引用UserController2.xml,即我需要在这两个xml之间创建一个链接(我不想将它们组合起来)。可以请一些人告诉我它是如何实现的?

2 个答案:

答案 0 :(得分:0)

您可以使用import标记将application-context.xml导入UserController2-servlet.xml,如下所示。将以下条目添加到UserController2-servlet.xml

 <import resource="application-context.xml"/>

答案 1 :(得分:0)

ArunM给出的答案是正确的,虽然我认为你的意思是如何在Web应用程序中注册应用程序上下文和servlet上下文?

如果这是你的意思,你需要将它们添加到web.xml文件中。

首先将application-context.xml添加到web.xml中,如下所示

<context-param>
    <param-name>contextConfigLocation</param-name>
    <!-- add you application context location -->
    <param-value>classpath*:META-INF/spring/application-Context.xml</param-value>   
</context-param>

然后在web.xml中注册您的servlet配置

<!-- Handles Spring requests -->
<servlet>
    <servlet-name>someName</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring/UserController2-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>someName</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>