Announcement Announcement Module
Collapse
No announcement yet.
AOP is not work in Spring MVC framework Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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>

    Comment


    • #3
      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.

      Comment


      • #4
        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?

        Comment


        • #5
          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.

          Comment

          Working...
          X