简体   繁体   English

春季-使用applicationContext.xml和XXXXX-servlet.xml

[英]Spring - using applicationContext.xml and XXXXX-servlet.xml

I am integrating Spring MVC into an existing project I have been working on. 我正在将Spring MVC集成到我正在从事的现有项目中。 By integrating, I mean I am rewriting the project using Spring, and using much of my old code. 通过集成,我的意思是我将使用Spring并使用许多旧代码来重写项目。 I have already setup the environment and have began working on it. 我已经设置好环境并开始研究它。 I will refer to this project as ProjectX . 我将这个项目称为ProjectX

I have already setup and configured my ProjectX-servlet.xml that holds the view-resolver bean, and the controller beans, etc. I want to set up an applicationContext.xml file that I can place all my DAO beans in such as ... 我已经设置并配置了包含view-resolver bean和controller bean等的ProjectX-servlet.xml 。我想设置一个applicationContext.xml文件,可以将所有DAO bean放入其中。 。

<bean id="MemberDAO" class="com.xxx.xxx.MemberDAO"/>
<bean id="ProductDAO" class="com.xxx.xxx.ProductDAO"/>

I want these values to be in the applicationContext.xml so that in my controllers I can do the following. 我希望这些值位于applicationContext.xml中,以便在控制器中可以执行以下操作。

public SomeController extends SimpleFormController{

   private MemberDAO memberDao;
   private ProductDAO productDao;

   ...getter/setter methods for memberDao;

   ...getter/setter methods for productDao;

and the values will be available(injecting them into the controllers) 并且这些值将可用(将它们注入到控制器中)

I have configured the controllers in the ProjectX-servlet.xml like the following definition. 我已经在ProjectX-servlet.xml中配置了控制器,如下所示。

<bean name="/SomeController.thm" class="com.xxx.xxx.controllers.SomeController">
      <property name="memberDao" ref="MemberDAO"/>
      <property name="productDao" ref="ProductDAO"/> 
</bean>

I believe I need to configure something such as the following in my web.xml so that it knows to load the application context. 我相信我需要在web.xml中配置诸如以下内容,以便它知道加载应用程序上下文。

  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>

  <servlet>
   <servlet-name>context</servlet-name>
   <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
  </servlet>

My question is, what do I have to do following creating an applicationContext.xml file, to be able to do what I showed above and inject beans such as the ProductDAO and MemberDAO into my controlellers which are configured in the ProjectX-servlet.xml 我的问题是,创建一个applicationContext.xml文件之后,我该怎么做才能执行上面显示的内容,并将诸如ProductDAOMemberDAO之类的 bean注入到在ProjectX-servlet.xml中配置的控制器中。

I have been using Spring MVC for a contract for a couple months and am comfortable with how to use it, but I am new to configuring it on my own, for my own use, so I would appreciate if any advice or answers were explained a little easier for me. 我已经使用Spring MVC签订合同了几个月,并且对如何使用它感到很满意,但是对于自己配置,我还是一个新手,我自己可以使用,因此,如果有任何建议或答案得到解释,我将不胜感激。对我来说容易一点。

Thank you 谢谢

By convention, the name you give to your instance of DispatcherServlet will be associated with {name}-servlet.xml . 按照约定,您为DispatcherServlet实例指定的{name}-servlet.xml将与{name}-servlet.xml关联。 This context will be a child to applicationContext.xml as you described, meaning it will have access to beans in applicationContext.xml . 如您所描述的,此上下文将是applicationContext.xml的子级,这意味着它将可以访问applicationContext.xml bean。

Try the following in your web.xml : 在您的web.xml尝试以下操作:

<servlet>
        <servlet-name>ProjectX</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
        <servlet-name>ProjectX</servlet-name>
        <url-pattern>/projectx/*</url-pattern>
</servlet-mapping>

Unless I'm misunderstanding, the solution you're looking for is to use an import statement in your applicationContext.xml. 除非我有误解,否则您要寻找的解决方案是在applicationContext.xml中使用import语句。 This effectively combines the two XML files into a single context, allowing you to reference beans in either. 这样可以将两个XML文件有效地组合到一个上下文中,从而使您可以在任何一个中引用bean。

Ex: 例如:

<import resource="classpath:foo/bar/ProjectX-servlet.xml" />

You may or may not want to use "classpath." 您可能会或可能不想使用“类路径”。 See section 3.2.2.1 in the Spring docs for more details. 有关更多详细信息,请参见Spring文档中的3.2.2.1节

You don't have to do anything special. 您不必做任何特别的事情。 You can continue injecting beans defined in applicationcontext.xml into the beans defined in xx-servlet.xml as if all of them are declared in same file. 您可以继续将applicationcontext.xml中定义的bean注入到xx-servlet.xml中定义的bean中,就像所有它们都在同一文件中声明一样。 Do remember to use the attribute ref instead of ref-local as below. 请记住要使用属性ref代替ref-local ,如下所示。

<bean id="mycontroller" class="x.y.z.CustomerController>
   <property name="service" ref="myservice"/><!--myservice defined in applicationcontext-->
</bean>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的spring程序可以使用dispatcher-servlet.xml而不使用ContextLoaderListener和applicationcontext.xml来运行吗? - Could my spring program run using dispatcher-servlet.xml and without using ContextLoaderListener and applicationcontext.xml? 春季-applicationContext.xml中的路径 - Spring - Paths in applicationContext.xml Spring Framework中applicationContext.xml和spring-servlet.xml的区别 - Difference between applicationContext.xml and spring-servlet.xml in Spring Framework 使用Spring MVC中的ApplicationContext.xml创建的对象的生命周期 - Lifetime of object created using ApplicationContext.xml in Spring MVC 为什么Spring-servlet.xml上的Beans无法访问applicationContext.xml中的bean - Why Beans on spring-servlet.xml can't access beans in my applicationContext.xml 在Spring ApplicationContext.xml中推送maven属性 - Pushing maven property in Spring ApplicationContext.xml 如何编写Spring ApplicationContext.xml文件? - How to write Spring ApplicationContext.xml file? Spring-加载applicationContext.xml时出现问题 - Spring - problems loading applicationContext.xml 有没有一种方法可以为Spring生成applicationContext.xml? - Is there a way to generate applicationContext.xml for Spring? 在Spring @Configuration中引用applicationContext.xml bean - Referring applicationContext.xml bean in Spring @Configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM