当前位置: 首页 > 知识库问答 >
问题:

spring mvc+spring aop+aspectj

韩鸿波
2023-03-14

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

作为切入点的方法运行良好,但没有Advise。

下面是启动整个Spring Boot的类,它是spring上下文的根:

@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:+'

编辑2:IntelliJ IDEA告诉我有关方法login和login2:“这个建议没有方法”,但同时,我能够从字符串跳转(通过控制单击),这是批注中的价值,以纠正实现。

共有1个答案

姜钊
2023-03-14

您所需要的就是这样的东西:

@Aspect
@Component
public class MyAspect {

    @Before("execution(* com.example.MyExampleClass.example(..))")
    public void logBefore(JoinPoint pjp) throws Throwable {
        System.out.println("before...");
    }
}
 类似资料:
  • 主要内容:读者,前提条件,Spring AOP 概述Spring框架的关键组件之一是面向方面编程(AOP)框架。 面向方面的编程需要将程序逻辑分解成不同的部分。 此教程将通过简单实用的方法来学习Spring框架提供的AOP/面向方面编程。 读者 本教程主要是为Spring 面向方面编程(AOP)初学者准备的,帮助他们了解与Spring的AOP框架相关的基础到高级概念。 前提条件 在开始练习本教程系列文章中给出的各种类型的示例之前,我们假设您已经了解

  • 我有一个自定义注释, 我正在将这个注释用于以下方法, 我在以下方面捕捉事件, @Around建议仅适用于“进程连接点”参数。如果将 XAudit 作为第二个参数传递,则会引发以下错误: 我需要在方面中使用xaud才能访问Xaud的操作。请分享一些关于如何在@周围方面中访问@Xaud值的指针。

  • Java+Spring+Maven应用程序: 有人能给我提供链接或者建议我一个纯AspectJ实现,不使用基于代理的Spring AOP吗? 如果我试图从同一中的访问,则不支持此操作。 我想知道:1)如何用切入点编写一个支持类内方法调用的aspectj?2)如何将其配置到我当前的Spring,maven项目中,使用aspectj加载时编织?3)如何配置aspectj maven插件,以便在Tomc

  • 我想在优雅的关闭上做一些工作。 我尝试了如下所示的方法,但它不起作用。 我找到了一个解决方法(在ContextClosedEvent的@EventListener标记方法上放置方面注释),但我想了解它失败的原因(方法没有任何异常根本没有调用)。 就我对Spring 5的研究而言,我发现@PreDestroy由CommonAnnotationBeanPostProcessor处理,而@Aspect类

  • 本文向大家介绍SpringAOP中的注解配置详解,包括了SpringAOP中的注解配置详解的使用技巧和注意事项,需要的朋友参考一下 这篇文章主要介绍了SpringAOP中的注解配置详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 使用注解实现SpringAOP的功能: 例子: xml配置:注意给例子中使用的其他的类上面也使用注解 注意:<aop

  • 有没有一种方法可以有条件地创建方面介绍?我想要的是有条件地使用Spring AOP扩展一个类: 所以testClass根据我设置该选项的属性文件扩展Test1Impl或Test2Impl,这是可能的吗?我如何排除被调用的Aspects,我尝试使用aspectj-maven-plugin,但它不排除我的Aspects: pom。xml 编辑 我删除了aspectj maven插件,只使用Spring

  • 龙果开源-后台管理系统 项目介绍 框架完全是基于Spring IO platform,绝对拥抱Spring,版本的依赖关系再不用担心。 前端基于 龙果开源-后台管理UI Roncoo AdminLTE,高端大气上档次。 实现了邮件发送功能,邮件增删改查功能 代码自动生成工具使用了龙果开源-Mybatis代码自动生成工具 项目愿景 根据实际项目的需求,实现一个适合由单应用扩展到多应用的架构。在项目的

  • 一些日志记录需要在类的静态方法执行前后完成。我试图使用Spring AOP实现这一点,但它不起作用,对于普通方法来说,它是起作用的。请帮助我理解如何实现这一点,如果可以使用注释来完成,那就太好了。