在eclipse中使用STS来创建一个mvc项目我注意到servlet-context.xml似乎被编写为在根上下文和dispatcherservlet Context中使用.我这样说是因为我注意到上下文:组件扫描在其中,它通常被加载到根上下文中,但它被加载到dispatcherservlet上下文中.我还注意到一个样品的Spring MVC/JPA项目- http://duckranger.com/2012/04/spring-mvc-3-x-with-sts-tutorial-part-iii-add-some-jpa/ -特异将servlet-context.xml加载到两个上下文中.我认为这个想法是在上下文之间保持清晰的分离.谁可以给我解释一下这个?
以下配置是完全错误的
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:META-INF\root-context.xml
classpath:META-INF\servlet-context.xml
classpath:META-INF\datasource.xml
</param-value>
</context-param>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:META-INF\servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Run Code Online (Sandbox Code Playgroud)
<tx:annotation-driven>仅在根上下文中声明它将不会影响servlet上下文bean,它会强迫你纠缠配置更多).<jpa:repositories>servlet上下文是非常不合逻辑的,因为很可能您将使用服务层中的存储库.
servlet上下文应仅包含与用户端 API相关的内容,但不包含应用程序.您是否应该划分根Web应用程序上下文的配置并将data-side-api放入单独的配置文件中是有争议的,但问题是关于servlet/root上下文.
这里不那么抽象,这是我在配置Spring应用程序时通常会记住的一些非正式图表(在Spring上下文和bean配置文件方面)(当然它都是主观的,它不是一个超级解决方案,实际上有点过了复杂 - 我不太可能需要这么多servlet和配置文件)

| 归档时间: |
|
| 查看次数: |
1490 次 |
| 最近记录: |