哪些bean应该在applicationContext和dispatcherServlet中声明
最初,我在dispatcher-servlet 中声明了所有bean,并且我的应用程序工作了。我真的需要有一个 applicationContext.xml
文件吗?
Initially I declared all my beans in dispatcher-servlet
and my application worked. Do I really need to have an applicationContext.xml
file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不一定需要它,但它是分离应用程序层的首选方法:
dispatcher-servlet.xml
中仅放置与 Web 相关的内容 - 控制器、视图解析器、转换器等请注意,您必须声明一个侦听器,以便 spring 可以加载
applicationContext.xml
:然后来自 < code>applicationContext.xml 将是父上下文,
dispatcher-servlet.xml
中的上下文将是子上下文。子级可以看到父级中的 beans,但父级看不到子级中的 Bean。另请注意,
applicationContext.xml
是默认名称。您可以通过contextConfigLocation
更改名称或查找路径You don't necessarily need it, but it is a preferred way to separate application layers:
dispatcher-servlet.xml
place only web-related stuff - controllers, view resolvers, converters, etc.applicationContext.xml
put all services and daos, and other general configurationsNote that you'll have to declare a listener so that spring can load
applicationContext.xml
:Then the context from
applicationContext.xml
will be the parent context, and the one indispatcher-servlet.xml
will be the child context. The child sees the beans in the parent, but the parent does not see those in the child.Also note that
applicationContext.xml
is a default name. You can change the name or the path where it is seeked via thecontextConfigLocation
<context-param>
您不需要单独的
applicationContext.xml
文件。但您确实需要一个 xml 文件,即使它只是告诉 Spring 自动加载应用程序中的所有 bean。You don't need an
applicationContext.xml
file per-say. But you do need a xml file, even if it is only to tell Spring to auto-load all the beans in your application.