是否可以仅使用dispatcher-servlet.xml
运行spring-orm程序。不使用ContextLoaderListener
和applicationcontext.xml
?如果是,那么如何?
其次,为什么有必要在dispatcher-servlet.xml
注册applicationcontext.xml
或web.xml
?因为当这些文件没有在web.xml
中注册时,所有的setter注入(在dispatcher-servlet.xml
或applicationcontext.xml
中指定)都会在服务器启动时发生,这意味着这些xml文件正在加载而不在{ {1}}。
答案 0 :(得分:1)
是否可以仅使用dispatcher-servlet.xml运行spring-orm程序。不使用ContextLoaderListener和applicationcontext.xml?
是的,没关系。
如果是,那么如何?
你就是这样做的。将您需要的所有内容放入dispatcher-servlet.xml
,它将自行运行。虽然在applicationContext.xml
中将DAO相关内容放在dispatcher-servlet.xml
和MVC内容中是常见的做法,但这绝不是必需的。如果您选择,它们都可以放在一个文件中。
其次,为什么有必要在web.xml中注册dispatcher-servlet.xml或applicationcontext.xml?因为当这些文件未在web.xml中注册时,所有setter注入(在dispatcher-servlet.xml或applicationcontext.xml中指定)都在服务器启动时发生,这意味着这些xml文件正在加载而不在web.xml中注册。 / p>
因为如果你没有在web.xml
中指定它们,Spring会使用它的默认值并且无论如何都要查找它们。 DispatcherServlet
会查找<servlet-name>-servlet.xml
,ContextLoaderListener
默认会查找/WEB-INF/applicationContext.xml
。如果您愿意,可以在web.xml
中覆盖此内容。