Spring - 使用 applicationContext.xml 和 XXXXX-servlet.xml

发布于 2024-09-14 12:53:54 字数 1821 浏览 2 评论 0原文

我正在将 Spring MVC 集成到我一直在从事的现有项目中。通过集成,我的意思是我正在使用 Spring 重写该项目,并使用我的大部分旧代码。我已经搭建好环境并开始着手工作。我将该项目称为 ProjectX

我已经设置并配置了保存视图解析器 bean 和控制器 bean 等的 ProjectX-servlet.xml。我想设置一个 applicationContext.xml我可以将所有 DAO bean 放入其中的文件,例如 ...

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

我希望这些值位于 applicationContext.xml 中,以便在我的控制器中我可以执行以下操作。

public SomeController extends SimpleFormController{

   private MemberDAO memberDao;
   private ProductDAO productDao;

   ...getter/setter methods for memberDao;

   ...getter/setter methods for productDao;

并且这些值将可用(将它们注入控制器)

我已经在 ProjectX-servlet.xml 中配置了控制器,如下定义。

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

我相信我需要在 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>

我的问题是,创建 applicationContext.xml 文件后我必须做什么,才能执行上面显示的操作并注入诸如 ProductDAOMemberDAO< 之类的 bean /b> 进入在 ProjectX-servlet.xml 中配置的控制器

我已经使用 Spring MVC 签订合同几个月了,并且很熟悉如何使用它,但是我是自己配置它以供自己使用的新手,因此如果有任何建议或答案对我来说更容易解释,我将不胜感激。

谢谢

I am integrating Spring MVC into an existing project I have been working on. By integrating, I mean I am rewriting the project using Spring, and using much of my old code. I have already setup the environment and have began working on it. I will refer to this project as 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 ...

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

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.

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

  <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

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.

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

没有心的人 2024-09-21 12:53:54

按照约定,您为 DispatcherServlet 实例指定的名称将与 {name}-servlet.xml 关联。正如您所描述的,此上下文将是 applicationContext.xml 的子级,这意味着它将有权访问 applicationContext.xml 中的 bean。

在您的 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>

By convention, the name you give to your instance of DispatcherServlet will be associated with {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.

Try the following in your 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>
柳若烟 2024-09-21 12:53:54

你不必做任何特别的事情。您可以继续将 applicationcontext.xml 中定义的 bean 注入到 xx-servlet.xml 中定义的 bean 中,就好像所有这些 bean 都在同一个文件中声明一样。请记住使用属性ref而不是ref-local,如下所示。

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

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. Do remember to use the attribute ref instead of ref-local as below.

<bean id="mycontroller" class="x.y.z.CustomerController>
   <property name="service" ref="myservice"/><!--myservice defined in applicationcontext-->
</bean>
爱给你人给你 2024-09-21 12:53:54

除非我误解了,否则您正在寻找的解决方案是在 applicationContext.xml 中使用 import 语句。这有效地将两个 XML 文件合并到一个上下文中,允许您在任一文件中引用 bean。

例如:

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

您可能想也可能不想使用“类路径”。请参阅 Spring 文档中的第 3.2.2.1 节更多细节。

Unless I'm misunderstanding, the solution you're looking for is to use an import statement in your applicationContext.xml. This effectively combines the two XML files into a single context, allowing you to reference beans in either.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文