Results 1 to 5 of 5

Thread: AOP is not work in Spring MVC framework

  1. #1
    Join Date
    Jan 2013
    Posts
    3

    Question AOP is not work in Spring MVC framework

    Hi,
    I try to use the AOP in my Spring MVC framework. Is there anyone can tell me why? The Codes as below:

    web.xml
    ...
    <servlet>
    <servlet-name>springshare</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/springshare-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>springshare</servlet-name>
    <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    ...
    <listener>
    <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
    </listener>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    /WEB-INF/applicationContext.xml
    </param-value>
    </context-param>
    ...

    springshare-servlet.xml

    <context:component-scan base-package="com.chalet.springshare" ></context:component-scan>
    <mvc:annotation-driven />

    applicationContext.xml

    <aop:aspectj-autoproxy/>


    @Component
    @Aspect
    public class PreGreetingMethods {
    Logger logger = Logger.getLogger(PreGreetingMethods.class);

    @Before("execution(* com.chalet.springshare.service.AOPService+.*(..))" )
    public void beforeInterfaceAndCTest(){
    logger.info(">>>>>>>>>PreGreetingMethods.beforeInt erfaceAndCTest executed!");
    }
    }

    When I call the method in the class AOPServiceImpl which implements the interface AOPService, the log mentioned above is not printed.

  2. #2
    Join Date
    Jan 2013
    Posts
    3

    Default

    I also try the way as below, but not work yet.

    <aop:config proxy-target-class="true">
    <aop:aspect ref="preGreetingMethods">
    <aop:before pointcut="execution(* com.chalet.springshare.service.AOPService+.*(..))" method="beforeInterfaceAndCTest"/>
    </aop:aspect>
    </aop:config>

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Please use [ code][/code ] tags when posting code/xml/stacktraces that way it remains readable..

    Next please use the forum search as this question has been answered before also this is answered by reading the reference guide.

    In short the bean definitions (actually the Bean(Factory)PostProcessors) operate only on the application context they are defined in. So your aop:aspectj-autoproxy is useless for beans defined in the springshare-servlet.xml.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4
    Join Date
    Jan 2013
    Posts
    3

    Default

    Quote Originally Posted by Marten Deinum View Post
    Please use [ code][/code ] tags when posting code/xml/stacktraces that way it remains readable..

    Next please use the forum search as this question has been answered before also this is answered by reading the reference guide.

    In short the bean definitions (actually the Bean(Factory)PostProcessors) operate only on the application context they are defined in. So your aop:aspectj-autoproxy is useless for beans defined in the springshare-servlet.xml.
    Hi Marten,

    Sorry for the inconvenient. But I defined the tag "component-scan" in applicationContext.xml also as below:
    Code:
    <aop:aspectj-autoproxy/>
    	<context:component-scan base-package="com.chalet.springshare">    
            <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect" />
        </context:component-scan>
    In my opinion, it means all the class who has the annotation "@Aspect" in the related packages will be defined and useful, right? Just like the MVC, if I defined the tags as below, all the class who has the annotation "@Controller,@Service" in the related packages will be defined and useful.

    Code:
       <context:component-scan base-package="com.chalet.springshare" ></context:component-scan>
       <mvc:annotation-driven />
    I am not sure, but am I right?

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Read the post...

    The aop stuff is defined in the root context and doesn't influence your beans in the servlet context. Basically your configuration duplicates the bean instances, once they are scanned and proxied in the root context and next you scan again in the dispatcher servlet not proxied. The latter basically renders youra op stufff useless.

    The fact that you add an include filter doesn't mean the defaults are ignored it just adds them to the defaults, next to that it still doesn't work if you would do that because, as mentioned, the aop stuff in the root context doesn't influence beans in the servlet context.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •