javascript
Spring - 父容器与子容器
一、Spring容器(父容器)
?
1、Mapper代理對象
2、Service對象
?
二、Springmvc(前端控制器)(子容器)
? ? ?? Controller對象
? ? ? ? 1.標準的配置是這樣的:Controller只在SpringMVC的配置文件掃描。Service等只在Spring容器的配置文件配置掃描。
? ? ? ? 2.Spring中也可以掃描Controller,但是使用時還是使用的SpringMVC自己的(自己沒有配置,那么不能用),造成對象冗余,而且使用時事務控制可能有問題。
? ? ? ? 3.SpringMVC中也可以配置掃描Service等,也造成冗余,而且使用時事務控制可能有問題。
? ? ? ?通過Value注入時,在Controller中不能使用注入屬性,因為Controller是子容器控制的。
? ? ? ? ? 除非在spring-mvc.xml中也加入配置:
? ? ? ? ? <!-- 加載文件上傳路徑配置文件 解決@controller 中無法注入配置文件中的屬性 -->
? ? ? ? ? ?<context:property-placeholder location="classpath*:file.properties"/>
三、總結
?
1、避免父子容器擁有共同的實例,是沒有必要的使用方式。
常見的資源初始化、預熱多次;如果父子容器都有實例,而通過SpringUtil 的方式獲取bean,就要看SpringUtil 所在的容器,來獲取對應的bean,也容易混亂
2、事務一般不要放在子容器中,子容器應該僅僅存在 web相關的bean;這也間接說明@Transactional 不應該修飾controller
3、如果希望對子容器的實例擁有事務能力,需要確保<aop:aspectj-autoproxy/> 以及Aspect 配置在子容器中
?
轉載于:https://www.cnblogs.com/caoshouling/p/8644514.html
總結
以上是生活随笔為你收集整理的Spring - 父容器与子容器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenJDK install
- 下一篇: Xamarin.Android 使用 E