简体   繁体   中英

Spring AOP: How to intercept inner class?

My Class:

@Aspect
public class ServiceAspect {
    @Before("execution(public * com.test.server.support.service.*.Client.*(..))")
    public void before(JoinPoint joinPoint) {
        System.out.println("....");
    }
}

spring-config.xml Content Only:

<context:annotation-config/>

spring-servlet.xml:

<mvc:annotation-driven/>
<mvc:default-servlet-handler/>

<context:component-scan base-package="com.test.client.support">
    <context:include-filter type="aspectj" expression="com.test.client.support.aspect.ServiceAspect"/>
</context:component-scan>
<context:component-scan base-package="com.test.manager"/>

<aop:aspectj-autoproxy proxy-target-class="true"/>

I hope in this way can then xxx.Client each class the following method call, the connection is automatically created.

When the current operation of the project, AOP no response.

However, if theBefore

("execution (public * com.test.server.support.service.*.Client.*(..))") 

IntoPointcut

("@within (org.springframework.stereotype.Controller)")

, you can enter before method

Note: Client class is automatically generated Thrift IDL.

Spring AOP only works on public methods of classes declared as Spring Beans. I guess your inner classes probably are not Spring Beans. So either you should

  • refactor your code to get rid of inner classes or
  • make your inner classes Spring Beans or
  • switch from Spring AOP to full AspectJ ( <context:load-time-weaver/> instead of <aop:aspectj-autoproxy/> ), see Spring documentation, chapter 9.8 .

Update: The within() pointcut in AspectJ also encompasses inner classes, this is documented. If and how that also applies to Spring AOP, I do not know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM