AOP Advices not running in Spring Boot Application

46 Views Asked by At

I am trying to use simple logging aspect in my application, but the advices are not running and am not getting any logs in the console.

Dependencies added

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
</dependency>

Entity class

package com.ayushsingh.aopproject.entity;

import org.springframework.stereotype.Component;

public class Student {
    private Long id;
    private String name;
    private String address;
    private Integer age;
    private Long phone;

    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public String getAddress() { return address; }
    public void setAddress(String address) { this.address = address; }
    public Integer getAge() { return age; }
    public void setAge(Integer age) { this.age = age; }
    public Long getPhone() { return phone; }
    public void setPhone(Long phone) { this.phone = phone; }
}

Aspect class

package com.ayushsingh.aopproject.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;


@Aspect
@Component
public class LoggingAspect {

     LoggingAspect() {
          System.out.println("Logging Aspect instansiated");
     }

     // -Run before a getter
     @Before("execution(public String com.ayushsingh.aopproject.entity.Student.getName())")
     public void LoggingAdvice1() {
          System.out.println("Advice 1");
     }

     // - Run before a method of a particular class
     @Before("execution(public String com.ayushsingh.aopproject.entity.Student.getName())")
     public void LoggingAdvice2() {
          System.out.println("Adivce 2");
     }

     // - Run before all getters
     @Before("execution(public String com.ayushsingh.aopproject.*.get*())")
     public void LoggingAdvice3() {
          System.out.println("Advice 3");
     }

     // - Run before all setters (methods with argument)
     @Before("execution(public void com.ayushsingh.aopproject.*.set*(*))") // -one or more arguments
     public void LoggingAdvice4() {
          System.out.println("Advice 4");
     }

     // -for getters of all entity classes in the entity package
     @Before("execution(public * com.ayushsingh.aopproject.entity.*.get*())")
     public void LoggingAdvice5() {
          System.out.println("Advice 5");
     }

}

Main class

package com.ayushsingh.aopproject;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

import com.ayushsingh.aopproject.entity.Student;

@SpringBootApplication
@EnableAspectJAutoProxy
public class AopProjectApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(AopProjectApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        // -Create Student object
        Student student = new Student();
        student.setId(1L);
        student.setAddress("Address 1");
        student.setAge(25);
        student.setName("Ayush");
        student.setPhone(8145000047L);
        String name=student.getName();
        System.out.println("Name: " + student.getName());
    }

}

Now on running the application I must get the logs from the advices also, but as I also checked using debugger and breakpoints, the aspects are not running at all.

The output I get is

2024-02-07T15:20:41.325+05:30  INFO 21704 --- [  restartedMain] c.a.aopproject.AopProjectApplication     : No active profile set, falling back to 1 default profile: "default"
2024-02-07T15:20:41.408+05:30  INFO 21704 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
Logging Aspect instansiated
2024-02-07T15:20:42.527+05:30  INFO 21704 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2024-02-07T15:20:42.607+05:30  INFO 21704 --- [  restartedMain] c.a.aopproject.AopProjectApplication     : Started AopProjectApplication in 1.732 seconds (process running for 2.185)
Name: Ayush

Please help me resolve this issue.

1

There are 1 best solutions below

3
kriegaex On

Do not call component constructors directly, let the Spring container do that for you by calling getBean(..) on the application context returned by SpringApplication.run(), currently discarded by you.

Please also be advised to read at least minimal documentation when using a new technology, or maybe start from a functioning sample application and work your way up to more features, always just changing small bits which you can revert, if something is not working.