简体   繁体   English

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

[英]How to intercept method execution of inner non-spring class, which is defined in Spring managed bean?

I need to implement interceptor of inner class, outer class is managed by Spring 我需要实现内部类的拦截器,外部类由Spring管理

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

    protected class Inner {

        protected void methodToIntercept() {
        }

    }

}

Outer.Inner.class may have childs (anonymous as well). Outer.Inner.class可能有孩子(也为匿名孩子)。 Overriden methodToIntercept() should be intercepted as well 重写methodToIntercept()应被拦截

Documentation is pretty straightforward: https://docs.spring.io/spring/docs/3.2.0.RELEASE/spring-framework-reference/htmlsingle/#aop-aj-ltw 文档非常简单: https : //docs.spring.io/spring/docs/3.2.0.RELEASE/spring-framework-reference/htmlsingle/#aop-aj-ltw

According to documentation I configured everything in following way: 根据文档,我以以下方式配置了所有内容:

Project structure 项目结构

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

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 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 依存关系

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

It doesn't work as I expected unfortunately 不幸的是,它不起作用

Should it work with such configuration? 这样的配置是否可以使用? What do I miss? 我想念什么?

Thanks 谢谢

Try to create your custom annotation and annotate the method. 尝试创建自定义注释并注释该方法。

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

Your @Around will look like this 您的@Around看起来像这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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