为什么启用Axchaps j表达使应该在上下文中消失的班级消失 我学习如何使用“ Spring stark onere”一书使用ActectJ表达式,在运行同一程序5次(可以)之后,jeackj表达式的更改是例外...

问题描述 投票:0回答:1
仅当extactj表达式拟合Criterea

时才出现错误

ASPECTO.JAVA File structoreimport java.util.logging.Logger; import org.springframework.stereotype.Component; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; @Aspect @Component public class Aspecto { private static final Logger logger = Logger.getLogger(Aspect.class.getName()); @Around("execution (* julian.*.*.Comment.*.*(..))") public void log(ProceedingJoinPoint joinPoint) { logger.info("Comenzó un metodo atrapado por el aspecto"); try { joinPoint.proceed(); } catch (Throwable e) { System.err.println(e); } logger.info("Terminó"); } }

CommentRepository.java

import julian.gonzalezlopez.aop.POJO.Comment; import org.springframework.stereotype.Service; @Service public class CommentRepository implements CommentRepositoryInterface { public void pie(Comment comment){ System.out.println(comment.getText()); System.out.println("Escrito por: " + comment.getOwner()); } }
main.java

import julian.gonzalezlopez.aop.Comment.CommentRepository; import julian.gonzalezlopez.aop.Comment.CommentRepositoryInterface; import julian.gonzalezlopez.aop.POJO.Comment; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { System.out.println("Hello World!"); AnnotationConfigApplicationContext c = new AnnotationConfigApplicationContext(ProjectConfig.class); System.out.println(c); CommentRepositoryInterface commentRepository = c.getBean(CommentRepository.class); Comment comment = new Comment("hola","julian"); commentRepository.pie(comment); } }
    

在我们解决您的主要问题之前,一些提示:

您应该遵守命名惯例,例如不使用大写软件包名称,因为它们很容易与班级名称混淆。
java spring aspectj
1个回答
0
投票
Aspect

,但您可能要使用

Aspecto
    。此外,这也是一个糟糕的班级名称。您应该使用一个方面名称来描述它是什么样的方面,例如
  • LoggingAspect

    .

  • 在您的屏幕截图中,我可以看到一个名为

    CommentService
    的类,但是在您的示例代码中,您使用class
    CommentRepository
    带有
    @Service
    注释。这完全令人困惑。你甚至还记得自己为什么这样做吗?

  • 现在,您如何解决问题?可能,您的配置类看起来有些类似:

    @Configuration
    @EnableAspectJAutoProxy
    public class ProjectConfig {}
    
    I.E。,它缺少
    @ComponentScan
    注释。
    其他可能性是您实际上确实添加了注释,但仍然得到同样的例外。在这种情况下,您可以修复主类,搜索接口类的豆子,...

    CommentRepositoryInterface commentRepository = c.getBean(CommentRepositoryInterface.class);
...,这是我建议的。作为替代方案,您可以保持bean实例化为IS,但可以修改春季创建代理的方式:

@Configuration @EnableAspectJAutoProxy(proxyTargetClass = true) @ComponentScan public class ProjectConfig {}
我热烈建议使用第一个选项,因为从设计的角度来看,它是清洁的。您已经拥有界面及其实现,这是一个干净的设计。利用它,不要围绕它工作。
    


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.