Spring - 父容器與子容器
阿新 • • 發佈:2018-03-25
pos pat 不能 文件中 spa c中 proxy -m 控制
通過Value註入時,在Controller中不能使用註入屬性,因為Controller是子容器控制的。 如果父子容器都有實例,而通過SpringUtil 的方式獲取bean,就要看SpringUtil 所在的容器,來獲取對應的bean,也容易混亂
一、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、避免父子容器擁有共同的實例,是沒有必要的使用方式。
常見的資源初始化、預熱多次;
2、事務一般不要放在子容器中,子容器應該僅僅存在 web相關的bean;這也間接說明@Transactional 不應該修飾controller
3、如果希望對子容器的實例擁有事務能力,需要確保<aop:aspectj-autoproxy/> 以及Aspect 配置在子容器中
Spring - 父容器與子容器