Solution for aop configuration not working in springMVC

    Recently, after learning the aop principle of spring, when practicing in springMVC, I wrote the corresponding aop class according to the online configuration and made corresponding settings in xml. But the settings of aop have been not working. Found a workaround after looking at related blogs. 
     The configuration of Spring's ApplicationContext.xml in the web environment is roughly as follows: when the web container starts, scan all other packages such as service and dao except for the controller.

<context:property-placeholder location="classpath:mybatis/db.properties"/>
<context:component-scan base-package="com.zkw">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

The configuration of springMVC.xml in springMVC is: only scan the controller package.

<!-- 扫描controller包 -->
<context:component-scan base-package="com.zkw.controller" />

Then I want to add aop to a method in a controller class. Specifically, when a function is executed before the method is executed, another function is executed after the method is executed. So first I added the following configuration to the ApplicationContext.xml file:

<!-- aop相关 -->
<bean id="timetest" class="com.zkw.aop.monitor.TimeTest" />
    <aop:config>
        <aop:aspect id="timee" ref="timetest">
            <aop:pointcut id="analysis_test" expression="execution(* com.zkw.controller.framework.analysis.analysisController.weiboAnalysis(..))"/>
            <aop:before method="beforeAdvice" pointcut-ref="analysis_test" />
            <aop:after method="afterAdvice" pointcut-ref="analysis_test" />
        </aop:aspect>
    </aop:config>

The code of the enhanced class com.zkw.aop.monitor.TimeTest is as follows:

public class TimeTest {
    public void beforeAdvice() {
        System.out.println("增强开始前");
    }

    public void afterAdvice() {
        System.out.println("增强结束后");
    }

}

It is to print a sentence before the method is executed, and then print a sentence after the method is completed. The pointcut function is weiboAnalysis(), which just prints a sentence. 
But when I run the code and access the weiboAnalysis() function from the foreground, the weaving enhancements are not printed. Later, after referring to the http://blog.csdn.net/tianjun2012/article/details/47809739  blog, put the configuration of aop into springMVC.xml, and the enhanced weaving can be completed normally. 
Later, I changed the aop configuration of ApplicationContext.xml, and changed the 
<aop:pointcut id="analysis_test" expression="execution(* com.zkw.service.framework.analysis.TestService.*(..))"/>pointcut to a method in the service package scanned by the ApplicationContext.xml file. After running, I found that the enhancement can also be woven in. 
     Summary: When configuring aop for a class or function that needs to be woven into the enhancement, the corresponding aop configuration needs to be written in the configuration file that scans the class. For this example, the configuration of services, dao, etc. other than the controller is completed in ApplicationContext.xml, so writing the aop configuration for the controller in the ApplicationContext.xml file will not work. On the contrary, springMVC.xml only The configuration of the controller is completed, so writing the configuration for the service in springMVC.xml will not work.  As for the reason, it is not clear yet.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325478759&siteId=291194637