Spring - 使用 applicationContext.xml 和 XXXXX-servlet.xml
我正在将 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 文件后我必须做什么,才能执行上面显示的操作并注入诸如 ProductDAO 和 MemberDAO< 之类的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
按照约定,您为
DispatcherServlet
实例指定的名称将与{name}-servlet.xml
关联。正如您所描述的,此上下文将是applicationContext.xml
的子级,这意味着它将有权访问applicationContext.xml
中的 bean。在您的
web.xml
中尝试以下操作:By convention, the name you give to your instance of
DispatcherServlet
will be associated with{name}-servlet.xml
. This context will be a child toapplicationContext.xml
as you described, meaning it will have access to beans inapplicationContext.xml
.Try the following in your
web.xml
:你不必做任何特别的事情。您可以继续将 applicationcontext.xml 中定义的 bean 注入到 xx-servlet.xml 中定义的 bean 中,就好像所有这些 bean 都在同一个文件中声明一样。请记住使用属性ref而不是ref-local,如下所示。
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.
除非我误解了,否则您正在寻找的解决方案是在 applicationContext.xml 中使用 import 语句。这有效地将两个 XML 文件合并到一个上下文中,允许您在任一文件中引用 bean。
例如:
您可能想也可能不想使用“类路径”。请参阅 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:
You may or may not want to use "classpath." See section 3.2.2.1 in the Spring docs for more details.