項目是springmvc+spring,發現只能攔截dao層里方法
Service層不攔截將導致無法進行事務管理
Service層不攔截原因:springmvc的配置文件及spring的配置文件同時掃描了service,就無效了
Service層不攔截解決方案:將springmvc配置文件里將service層排除即可,如
<!-- 配置掃描的包 --> <context:component-scan base-package="com.xxx.xxx" > <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan>
Controller層不攔截原因:切面配置必須與掃描controller層的配置放在同一個配置文件中
Controller層不攔截解決方案:將配置放一起,如在springmvc.xml中如下配置:
<!-- 配置掃描的包 --> <context:component-scan base-package="com.xxx.xxx" > <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan> <!-- 切面配置--> <bean id="restfulAop" class="com.xxx.xxx.fitter.RestfulAop"/> <aop:config expose-proxy="true"> <aop:aspect ref="restfulAop"> <aop:pointcut id="restfulPointcut" expression="execution(* com.xxx.xxx.restful.*RestController.*(..))"/> <aop:before pointcut-ref="restfulPointcut" method="before"/> <aop:after pointcut-ref="restfulPointcut" method="after"/> </aop:aspect> </aop:config>
springmvc.xml頭部beans的屬性里加上
xmlns:aop="http://www.springframework.org/schema/aop"
springmvc.xml頭部beans的屬性的xsi:schemaLocation里加上
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd