我正在实施面向方面的编程。但即使我在 POM.xml 中添加了spectjweaver依赖项,我也无法添加@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的aspectj
<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!");
}
}
很抱歉对你这么直白,但当我说在你开始学习像 Spring 这样的复杂应用程序框架之前,你应该首先学习一些 Java 基础知识时,我是想帮助你。编程不仅仅是复制和粘贴。我这么说是因为你的代码违反了很多编码约定,我几乎不知道从哪里开始列出它们。您应该更加注意的一些事情是:
mainAspect
→ MainAspect
,actualPaymentService
→ ActualPaymentService
。com.AOP.Aspects
→ com.aop.aspects
。Auth
→ auth
。*Impl
,这是超级违反直觉的。相反,如果有任何类,您应该调用实现类*Impl
。例如,PaymentServicesImpl
→ PaymentServices
和 actualPaymentService
→ PaymentServicesImpl
。为什么编码和命名约定很重要?因为通常你不会在一个人的项目上工作,而是与其他开发人员合作,你会因为违反所有约定而使他们感到困惑。
关于您的实际问题,您可以解决它,就像我之前在评论中所说的那样,通过从
<scope>runtime</scope>
依赖项中删除错误的 aspectjweaver
。至于 aspectjrt
依赖项,您根本不需要它,因为 AspectJ 运行时类已经包含在 weaver 库中。后者是前者的超集。现在,您的 IDE(IntelliJ IDEA 或任何其他)应该能够导入 AspectJ 类。
下一个问题出在您的配置文件中。你不能用接口声明一个具体的(非抽象的)bean,也就是说,如果我们在示例代码中坚持使用扭曲的、令人恼火的类名,你就必须将
<bean name="payment" class="com.AOP.services.PaymentServicesImpl"/>
更改为 <bean name="payment" class="com.AOP.services.actualPaymentService"/>
以避免运行时错误在开始 Spring 时你会得到其他结果。
最后但并非最不重要的一点是,您的方面切入点是错误的。
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 配置和完整的类,包括。包名称。这里的其他社区成员比你有更多的经验,在这方面经常做得更糟,只是提供一些他们认为相关的片段,搬起石头砸自己的脚,因为缺少关键部分,因此没有人可以重现他们的问题。如果没有可重现的示例,我当然无法找到代码中的 3 个主要问题。好吧,我必须添加小驱动程序应用程序才能实际运行该项目,但这没关系。下次也请您自行添加。否则,请保持提问的好方法。 🙂
<!-- 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>