提问人:shaikh_sanan_007 提问时间:12/27/2022 最后编辑:zawarudoshaikh_sanan_007 更新时间:10/14/2023 访问量:555
无法在 Spring 5 中使用@Aspect注解
Cannot use @Aspect annotation in Spring 5
问:
我正在实现面向方面的编程。但是,即使我在POM中添加了aspectjweaver依赖项.xml,我也无法添加@Aspect注释。
你可以在下面找到我的代码
这是我的POM.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.AOP</groupId>
<artifactId>com.AOP</artifactId>
<version>1.0-SNAPSHOT</version>
<name>com.AOP</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.24</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.24</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.24</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.19</version>
<scope>runtime</scope>
</dependency>
这是 aspectjweaver 的依赖关系
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.9.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
这是我名为config.xml的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
在这里你可以看到我已经启用了 aop 的方面j
<aop:aspectj-autoproxy />
<bean name="payment" class="com.AOP.services.PaymentServicesImpl" />
<bean name="aspect" class="com.AOP.Aspects.mainAspect" />
</beans>
**我的接口名为 PaymentServiceImpl **
package com.AOP.services;
public interface PaymentServicesImpl {
public void makePayment(double amount);
}
我的java类正在实现上述接口
package com.AOP.services;
public class actualPaymentService implements PaymentServicesImpl{
@Override
public void makePayment(double amount) {
System.out.println("Rs "+ amount + " Credited to your bank Account");
}
}
最后,我的方面类 这是我遇到错误的地方,我在下面附上了错误的屏幕截图
package com.AOP.Aspects;
import org.aspectj.lang.annotation.Aspect;
public class mainAspect {
@Before("execution(* com.AOP.services.actualPaymentService)")
public void Auth(){
System.out.println("payment is processing!");
}
}
答:
尝试像这样添加 aspectjrt
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.7</version>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjtools -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.9</version>
</dependency>
转到 .m2 文件夹并删除存储库文件夹,返回 IDE 并运行maven clean install
或从终端mvn clean install
还要确保你至少使用JDK8。
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
评论
前言:违反编码和命名约定
很抱歉对你这么直言不讳,但我试图帮助你,因为我说在你开始学习像 Spring 这样的复杂应用程序框架之前,你应该先学习一些 Java 基础知识。编程不仅仅是复制和粘贴。我这么说是因为你的代码违反了这么多编码约定,我几乎不知道从哪里开始列出它们。您应该更加注意的一些事情是:
- 类名应使用大写字符,并使用驼峰大小写。例如,→ , → .
mainAspect
MainAspect
actualPaymentService
ActualPaymentService
- 包名称应由小写字符和下划线组成。例如,→ .
com.AOP.Aspects
com.aop.aspects
- 方法名称应以小写字符开头,并使用驼峰大小写。例如,→ .
Auth
auth
- 你调用了一个接口,这是超级违反直觉的。相反,如果有任何类,则应调用 implementation class 。例如,→ 和 → 。
*Impl
*Impl
PaymentServicesImpl
PaymentServices
actualPaymentService
PaymentServicesImpl
为什么编码和命名约定很重要?因为通常你不会在一个人的项目上工作,而是与其他开发人员合作,你会因为违反所有惯例而混淆他们。
Maven、Spring 和 AOP 问题
关于你的实际问题,你可以解决它,就像我在之前的评论中所说的那样,通过从依赖项中删除错误。至于依赖项,你根本不需要它,因为 AspectJ 运行时类已经包含在 weaver 库中。后者是前者的超集。现在,您的 IDE(IntelliJ IDEA 或任何其他)应该能够导入 AspectJ 类。<scope>runtime</scope>
aspectjweaver
aspectjrt
下一个问题出在你的配置文件中。你不能用接口声明一个具体的(非抽象的)bean,也就是说,如果我们坚持在你的示例代码中使用扭曲的、令人讨厌的类名,你将不得不更改为,以避免在启动Spring时得到的运行时错误。<bean name="payment" class="com.AOP.services.PaymentServicesImpl"/>
<bean name="payment" class="com.AOP.services.actualPaymentService"/>
最后但并非最不重要的一点是,你的纵横比切点是错误的。 语法无效,因为切入点需要方法签名,而不仅仅是类名。例如,您可以使用以下任何一项execution(* com.AOP.services.actualPaymentService)
execution
execution(* com.AOP.services.actualPaymentService.*(..))
,(实现类中的任何方法执行)execution(* com.AOP.services.PaymentServicesImpl.*(..))
(在实现接口的任何类中执行任何方法),within(com.AOP.services.PaymentServicesImpl+)
(接口类或其任何子类中的任何联接点)。
在修复所有这些问题之后,在运行这样的示例应用程序时...
package com.AOP;
import com.AOP.services.PaymentServicesImpl;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Application {
public static void main(String[] args) {
try (ConfigurableApplicationContext appContext = new FileSystemXmlApplicationContext("src/main/resources/META-INF/config.xml")) {
PaymentServicesImpl paymentServices = (PaymentServicesImpl) appContext.getBean("payment");
paymentServices.makePayment(123.45);
}
}
}
...您应该会看到如下所示的控制台日志:
payment is processing!
Rs 123.45 Credited to your bank Account
但是,请按照我建议的方式重命名您的包、类和方法。阅读这样的代码会伤害我的眼睛。
关于你的问题(也是我试图帮助你的主要原因)的一个好的、值得称赞的事情是,你实际上试图提供一个完整的、可重现的问题版本,即 Maven POM、Spring config 和完整的类,包括包名。这里的其他社区成员比你更有经验,在这方面往往做得更糟,他们只是提供一些他们认为相关的片段,因为缺少关键部分而搬起石头砸自己的脚,因此没有人可以重现他们的问题。如果没有可重现的示例,我当然不可能在您的代码中找到 3 个主要问题。好吧,我必须添加小驱动程序应用程序才能实际运行项目,但这没关系。下次,也请自己添加。否则,请保持提问的好方法。🙂
评论