Home > Net >  Cannot use @Aspect annotation in Spring 5
Cannot use @Aspect annotation in Spring 5

Time:12-27

I was implementing aspect-oriented programming. But I cannot add @Aspect annotation even though I have added the aspectjweaver dependency in my POM.xml.

You can find my code below

This is my POM.xml file

<?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>

Here is the dependency of 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>

This is my configuration file named 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">

Here you can see I have enabled the aop's aspectj


<aop:aspectj-autoproxy />

<bean name="payment"  />
<bean name="aspect"  />
</beans>

**My interface named PaymentServiceImpl **

package com.AOP.services;

public interface PaymentServicesImpl {

    public void makePayment(double amount);
}

My java class which is implementing the above interface

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");
    }
}

And finally my aspect class This is where I am getting an error I have attached the screenshot of the error below

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!");
    }
}

The screenshot of the Error The screenshot of the Error

CodePudding user response:

Try to add aspectjrt like this

<!-- 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>

Go to your .m2 folder and delete the repository folder, go back to your IDE and run maven clean install

Or from terminal mvn clean install

And also make sure you are using at least JDK8.

 <maven.compiler.source>1.8</maven.compiler.source>
 <maven.compiler.target>1.8</maven.compiler.target>

CodePudding user response:

Preface: coding and naming convention violations

Sorry for being so blunt with you, but I am trying to help you when I am saying that before you start learning about a complex application framework like Spring, you should first learn some Java basics. Programming is more than just copy & paste. I am saying this, because your code violates so many coding conventions, I almost do not know where to begin listing them. Some things you should give more attention to are:

  • Class names should being with upper case characters and use camel case. E.g., mainAspectMainAspect, actualPaymentServiceActualPaymentService.
  • Package names should consist of lower-case characters plus maybe underscores. E.g., com.AOP.Aspectscom.aop.aspects.
  • Method names should start with lower-case characters and use camel case. E.g., Authauth.
  • You called an interface *Impl, which is super counter-intuitive. Instead, if any class, you should call the implementation class *Impl. E.g., PaymentServicesImplPaymentServices and actualPaymentServicePaymentServicesImpl.

Why are coding and naming conventions important? Because usually your do not work on one-person projects but collaborate with other developers whom you would confuse by defying all conventions.

Maven, Spring and AOP problems

Concerning your actual problem, you can solve it, like I said in my comment before, by removing the faulty <scope>runtime</scope> from the aspectjweaver dependency. As for the aspectjrt dependency, you do not need it at all because the AspectJ runtime classes are already contained in the weaver library. The latter is a superset of the former. Now, your IDE (IntelliJ IDEA or any other) should be able to import the AspectJ classes.

The next problem is in your config file. You cannot declare a concrete (non-abstract) bean with an interface, i.e. if we stick to the twisted, irritating class names in your sample code, you would have to change <bean name="payment" /> to <bean name="payment" /> in order to avoid the runtime error you would get otherwise while starting Spring.

Last but not least, your aspect pointcut is wrong. execution(* com.AOP.services.actualPaymentService) is not valid syntax, because execution pointcuts need a method signature, not just a class name. For example, you could use any of

  • execution(* com.AOP.services.actualPaymentService.*(..)), (any method execution in the implementation class)
  • execution(* com.AOP.services.PaymentServicesImpl.*(..)) (any method execution in any class implementing the interface),
  • within(com.AOP.services.PaymentServicesImpl ) (any joinpoint within the interface class or any of its subclasses).

After you will have fixed all these problems, when running a sample application like this...

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);
    }
  }
}

... you should see a console log like this:

payment is processing!
Rs 123.45 Credited to your bank Account

But please, please rename your packages, classes and methods the way I suggested. It hurts my eyes to read code like that.

The one good, commendable thing about your question (and the main reason why I am trying to help you) is that you actually tried to provide a complete, reproducible version of your problem, i.e. Maven POM, Spring config and full classes incl. package names. Other community members here with way more experience than you often do much worse in this respect by just providing some fragments they think are relevant, shooting themselves in the foot because crucial parts are missing and hence nobody can reproduce their problems. I certainly could not have found the 3 main problems in your code without a reproducible example. OK, I had to add the little driver application in order to be able to actually run the project, but that was OK. Next time, please also add that by yourself. Otherwise, keep up the good way to ask questions.

  • Related