如何拦截在Spring托管bean中定义的内部非spring类的方法执行?

时间:2017-10-28 12:09:58

标签: java spring aop aspectj

我需要实现内部类的拦截器,外部类由Spring管理

@Component
@Scope("prototype")
public class Outer {

    protected class Inner {

        protected void methodToIntercept() {
        }

    }

}

Outer.Inner.class可能有孩子(也是匿名的)。 Overriden methodToIntercept()也应该被拦截

文档很简单: https://docs.spring.io/spring/docs/3.2.0.RELEASE/spring-framework-reference/htmlsingle/#aop-aj-ltw

根据文档,我按照以下方式配置了所有内容:

项目结构

-src
--main
---java
----temp
-----Config.java
-----MyAspect.java
-----Outer.java
-----SpringBean.java
--test
---resources
----META-INF
-----aop.xml

aop.xml文件

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
    <weaver>
        <include within="temp.*"/>
    </weaver>
    <aspects>
        <aspect name="temp.MyAspect"/>
    </aspects>
</aspectj>

Config.java

@Configuration
@EnableLoadTimeWeaving
@ComponentScan("temp")
@Lazy
public class Config {
}

Outer.java

@Component
@Scope("prototype")
public class Outer {

    @Component
    public static class Inner {

        public void methodToIntercept() {
        }

    }

}

MyAspect.java

@Aspect
@Component
public class MyAspect {

    @Autowired
    private SpringBean springBean;

    @Around("execution(* temp.Outer.Inner.methodToIntercept())")
    public Object interceptor(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("success");
        return joinPoint.proceed();
    }

}

依赖

<dependencies>
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.7.3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/aspectj/aspectjweaver -->
        <dependency>
            <groupId>aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.7.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-instrument -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-instrument</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>

    </dependencies>

不幸的是,它无法正常工作

它是否适用于此类配置?我错过了什么?

由于

1 个答案:

答案 0 :(得分:0)

尝试创建自定义注释并注释方法。

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ServiceLoggable {
}

@Around 将如下所示

    @Around("execution(* *(..)) && @annotation(com.company.core.log.ServiceLoggable)")
    public Object logServiceMethods(ProceedingJoinPoint jp) throws Throwable {
        ...    
    }