代码之家  ›  专栏  ›  技术社区  ›  xinus01

spring mvc+spring aop+aspectj

  •  1
  • xinus01  · 技术社区  · 10 年前

    我很难在SpringMVC项目中使用方面。

    切入点方法运行良好,但没有建议。

    这是一个类,它开始了整个春季启动,这是春季上下文的根:

    @Lazy
    @SpringBootApplication
    @EnableAspectJAutoProxy(proxyTargetClass=true)
    @Configuration
    public class MainSpringBootClass{
        public static void main(String[] args)
        {
            SpringApplication.run(MainSpringBootClass.class, args);
        }
    }
    

    这是带有方法的类,即切入点。

    @Component
    @Log
    @Aspect
    @EnableAspectJAutoProxy(proxyTargetClass=true)
    public class MyExampleClass
    {
        public void example()
        {
            System.out.println("example");
        }
    }
    

    这是我的观点:

    @Aspect
    @Component
    @EnableAspectJAutoProxy(proxyTargetClass=true)
    public class MyAspect implements MethodBeforeAdvice
    {
        @Pointcut("execution(* com.example.MyExampleClass.example())")
        public void asd()
        {
            // pointcut
        }
    
        @Before("asd()")
        public void login()
        {
            System.out.println("im am logging in");
        }
    
        @Before("execution(* com.example.MyExampleClass.example())")
        public void login2()
        {
            System.out.println("im am logging in2");
        }
    
        @Override
        public void before(Method method, Object[] args, Object target) throws Throwable
        {
            System.out.println("aop before");
        }
    }
    

    这是我的控制器:

    @RestController
    @EnableAspectJAutoProxy(proxyTargetClass=true)
    public class MyExampleController
    {
        private final MyExampleClass myExampleClass;
    
        @Inject
        public AdController(MyExampleClass myExampleClass)
        {
            this.myExampleClass = myExampleClass;
        }
    
        @RequestMapping("/")
        public String index()
        {
            myExampleClass.example();
    
            return "x";
        }
    }
    

    正如您所看到的,我一直在尝试用注释强制更正结果。 我也在一些网站上看到,我需要特定的依赖关系,所以这里是我的(只粘贴那些与方面相关的依赖关系):

    compile 'org.springframework:spring-aop:+'
    compile 'org.aspectj:aspectjrt:+'
    compile 'org.aspectj:aspectjweaver:+'
    compile 'cglib:cglib:+'
    compile 'cglib:cglib-nodep:+'
    

    所有依赖项都已成功下载,项目编译和运行良好。 当我点击localhost:8080时,我会看到返回值“x”,在日志中我会看到“example”。 然而,我没有看到spring aop或aspectj的任何建议——我做错了什么?

    我只是将这个项目作为沙盒来学习方面,所以我很想学习如何使用Spring AOP和AspectJ中的每一个来实现这一点。 对我来说,最重要的是在没有XML的情况下完成这一切。

    编辑: 我已经用println向MyAspect添加了简单的构造函数,以检查它是否被创建(毕竟它是带有@Component的普通spring bean),并且它确实被创建了——它是由spring正确创建的。

    编辑2: IntelliJ IDEA告诉我关于方法login和login2:“这个建议建议没有方法”,但同时,我能够从字符串(即注释中的值)跳到正确的实现。

    1 回复  |  直到 10 年前
        1
  •  0
  •   pastafarian    10 年前

    你只需要这样的东西:

    @Aspect
    @Component
    public class MyAspect {
    
        @Before("execution(* com.example.MyExampleClass.example(..))")
        public void logBefore(JoinPoint pjp) throws Throwable {
            System.out.println("before...");
        }
    }

    您可能需要将所有aspectJ依赖项替换为 spring-boot-starter-aop .

    下面是一个有效的示例项目(请参见 RestControllerAspect.java ):

    https://github.com/khoubyari/spring-boot-rest-example