如何在没有xml的Spring中运行方面?
发布时间:2020-12-16 23:09:53 所属栏目:百科 来源:网络整理
导读:如何在 Java中运行方面. 如何在没有xml文件的情况下使用注释在Spring中运行方面? 许多其他教程使用xml文件进行配置ascpect. 解决方法 定义自定义注释; @Target({ElementType.TYPE,ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documented@Inhe
如何在
Java中运行方面.
如何在没有xml文件的情况下使用注释在Spring中运行方面? 许多其他教程使用xml文件进行配置ascpect. 解决方法
定义自定义注释;
@Target({ElementType.TYPE,ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface Loggable { } 注释要拦截的方法; @Service public class MyAwesomeService { @Loggable public void myAwesomemethod(String someParam) throws Exception { // do some awesome things. } } 将方面依赖项添加到您的pom. <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency> 并定义你的方面类; @Aspect @Component public class LoggingHandler { @Before("@annotation(com.example.annotation.Loggable)") public void beforeLogging(JoinPoint joinPoint){ System.out.println("Before running loggingAdvice on method="); } @After("@annotation(com.example.annotation.Loggable)") public void afterLogging(JoinPoint joinPoint){ System.out.println("After running loggingAdvice on method="); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |