Mingjian
Mingjian

Reputation: 31

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.

Upvotes: 1

Views: 3737

Answers (1)

kriegaex
kriegaex

Reputation: 67297

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.

Upvotes: 3

Related Questions