Java Spring5AOP:不执行建议

Java Spring5AOP:不执行建议,java,spring,spring-aop,Java,Spring,Spring Aop,有人能帮我理解我在这里错过了什么吗 如果我从StudentModuleConfig中删除@enableSpectJautoproxy,代码工作正常 代码也可以很好地工作,我将advice定义为@Before(“execution(*fi*(..))”),带有@enableSpectProxy,advice可以工作 @Before(“execution(**(..)”)出现问题,不确定原因 Student.java StudentModuleConfig.java App2.java 例外情况:

有人能帮我理解我在这里错过了什么吗

  • 如果我从StudentModuleConfig中删除@enableSpectJautoproxy,代码工作正常
  • 代码也可以很好地工作,我将advice定义为@Before(“execution(*fi*(..))”),带有@enableSpectProxy,advice可以工作
  • @Before(“execution(**(..)”)出现问题,不确定原因

    Student.java

    StudentModuleConfig.java

    App2.java

    例外情况: 线程“main”org.springframework.beans.factory.BeanCreationException中出现异常:创建名为“getStudentRepositoryBean”(在com.springpeople.training.assignment.student.StudentModuleConfig中定义)的bean时出错:通过工厂方法实例化bean失败;嵌套异常为org.springframework.beans.beans实例化异常:未能实例化[com.springpeople.training.assignment.student.repositoy.StudentRepository]:工厂方法“getStudentRepositoryBean”引发异常;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“getLoggingAspectBean”的bean时出错,该bean在com.springpeople.training.assignment.student.StudentModuleConfig中定义:通过工厂方法实例化bean失败;嵌套异常为org.springframework.beans.beanstantiationException:未能实例化[com.springpeople.training.assignment.student.aspect.LoggingAspect]:工厂方法“getLoggingAspectBean”引发异常;嵌套异常为org.springframework.beans.factory.beanCurrentlyIncrementationException:创建名为“getLoggingAspectBean”的bean时出错:请求的bean当前正在创建中:是否存在无法解析的循环引用? 位于org.springframework.beans.factory.support.ConstructorResolver.InstanceUsingFactoryMethod(ConstructorResolver.java:590)

    原因:org.springframework.beans.beans实例化异常:未能实例化[com.springpeople.training.assignment.student.repositoy.StudentRepository]:工厂方法“getStudentRepositoryBean”引发异常;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“getLoggingAspectBean”的bean时出错,该bean在com.springpeople.training.assignment.student.StudentModuleConfig中定义:通过工厂方法实例化bean失败;嵌套异常为org.springframework.beans.beanstantiationException:未能实例化[com.springpeople.training.assignment.student.aspect.LoggingAspect]:工厂方法“getLoggingAspectBean”引发异常;嵌套异常为org.springframework.beans.factory.beanCurrentlyIncrementationException:创建名为“getLoggingAspectBean”的bean时出错:请求的bean当前正在创建中:是否存在无法解析的循环引用? 位于org.springframework.beans.factory.support.SimpleInstallationStrategy.instantiate(SimpleInstallationStrategy.java:185)

    原因:org.springframework.beans.factory.BeanCreationException:创建名为“getLoggingAspectBean”的bean时出错,该名称在com.springpeople.training.assignment.student.StudentModuleConfig中定义:通过工厂方法实例化bean失败;嵌套异常为org.springframework.beans.beanstantiationException:未能实例化[com.springpeople.training.assignment.student.aspect.LoggingAspect]:工厂方法“getLoggingAspectBean”引发异常;嵌套异常为org.springframework.beans.factory.beanCurrentlyIncrementationException:创建名为“getLoggingAspectBean”的bean时出错:请求的bean当前正在创建中:是否存在无法解析的循环引用? 位于org.springframework.beans.factory.support.ConstructorResolver.InstanceUsingFactoryMethod(ConstructorResolver.java:590)

    原因:org.springframework.beans.beans实例化异常:未能实例化[com.springpeople.training.assignment.student.aspect.LoggingAspect]:工厂方法“getLoggingAspectBean”引发异常;嵌套异常为org.springframework.beans.factory.beanCurrentlyIncrementationException:创建名为“getLoggingAspectBean”的bean时出错:请求的bean当前正在创建中:是否存在无法解析的循环引用? 位于org.springframework.beans.factory.support.SimpleInstallationStrategy.instantiate(SimpleInstallationStrategy.java:185) 位于org.springframework.beans.factory.support.ConstructorResolver.InstanceUsingFactoryMethod(ConstructorResolver.java:582) ... 38多 原因:org.springframework.beans.factory.beanCurrentlyIncremarationException:创建名为“getLoggingAspectBean”的bean时出错:请求的bean当前正在创建中:是否存在无法解决的循环引用? 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:339)上 位于org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:215)

    编辑 如果我这样做,它就会起作用

    已从StudentModuleConfig中删除以下内容

    @Bean
        public LoggingAspect getLoggingAspectBean() {
            return new LoggingAspect();
        }
    
    并在StudentModuleConfig中添加@ComponentScan,如下所示:

    @ComponentScan(basePackages= {"com.springpeople.training.assignment.student.aspect"})
    

    但是我仍然有一个问题,如果我使用@bean注释将LoggingAspect声明为bean,为什么它不起作用,这不是我们创建容器管理bean的方法吗。因此,删除
    StudentModuleConfig.getLoggingAspectBean()
    并将
    @ComponentScan
    添加到类中是正确的做法,正如您已经发现的那样

    不是Spring用户(我只有一个Spring AOP游乐场项目来回答像你这样的问题),我只能推测那里可能发生的事情。但我认为Spring认为您希望创建一个常规的Springbean,而不知道它是一个方面实例,并尝试将方面应用于它,因为您的全局捕获切入点。因此,错误日志中提到的循环引用。注意,一个方面是n
    package com.springpeople.training.assignment.student.repositoy;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.List;
    import java.util.Objects;
    
    import com.springpeople.training.assignment.course.domain.Course;
    import com.springpeople.training.assignment.student.domain.Student;
    
    
    public class StudentRepositoryImpl implements StudentRepository {
    
        public StudentRepositoryImpl() {
            System.out.println("StudentRepositoryImpl constructor invoked");
        }
    
        public void init() {
            System.out.println("StudentRepositoryImpl.init() invoked");
        }
    
        public void destroy() {
            System.out.println("StudentRepositoryImpl.destroy() invoked");
        }
    
        @Override
        public Collection<Student> findAllStudentByCourse(Course course) {
            List<Student> students = new ArrayList<>();
            if (Objects.nonNull(course) && "Spring".equals(course.getName())) {
                students.add(new Student("A", "a", "aa"));
                students.add(new Student("B", "b", "bb"));
            }
            return students;
        }
    
    }
    
    package com.springpeople.training.assignment.student.service;
    
    import java.util.Collection;
    
    import com.springpeople.training.assignment.course.domain.Course;
    import com.springpeople.training.assignment.student.domain.Student;
    
    public interface StudentService {
        Collection<Student> findAllStudentByCourse(Course course);
    }
    
    package com.springpeople.training.assignment.student.service;
    
    import java.util.Collection;
    
    import org.springframework.beans.factory.annotation.Autowired;
    
    import com.springpeople.training.assignment.course.domain.Course;
    import com.springpeople.training.assignment.student.domain.Student;
    import com.springpeople.training.assignment.student.repositoy.StudentRepository;
    
    public class StudentServiceImpl implements StudentService {
    
        private StudentRepository repository;
    
        public StudentServiceImpl() {
            System.out.println("StudentServiceImpl constructor invoked");
        }
    
        public StudentServiceImpl(StudentRepository repository) {
            this.repository = repository;
        }
    
        public void init() {
            System.out.println("StudentServiceImpl.init() invoked");
        }
    
        public void destroy() {
            System.out.println("StudentServiceImpl.destroy() invoked");
        }
    
        @Override
        public Collection<Student> findAllStudentByCourse(Course course) {
            return repository.findAllStudentByCourse(course);
        }
    
        public void setStudentRepository(StudentRepository studentRepositoryBean) {
            repository = studentRepositoryBean;
        }
    
    }
    
    package com.springpeople.training.assignment.student.aspect;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.stereotype.Component;
    
    @Component
    @Aspect
    public class LoggingAspect {
    
        @Before("execution(* *(..))")
        public void logBefore(JoinPoint joinPoint) {
            System.out.println("LoggingAspect.logBefore(): "+joinPoint.getSignature().getName());
        }
    
    }
    
    package com.springpeople.training.assignment.student;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.EnableAspectJAutoProxy;
    
    import com.springpeople.training.assignment.student.aspect.LoggingAspect;
    import com.springpeople.training.assignment.student.repositoy.StudentRepository;
    import com.springpeople.training.assignment.student.repositoy.StudentRepositoryImpl;
    import com.springpeople.training.assignment.student.service.StudentService;
    import com.springpeople.training.assignment.student.service.StudentServiceImpl;
    
    @Configuration
    @EnableAspectJAutoProxy
    public class StudentModuleConfig {
    
        @Bean
        public StudentRepository getStudentRepositoryBean() {
            return new StudentRepositoryImpl();
        }
    
        @Bean
        public StudentService getStudentServiceBean() {
            StudentServiceImpl studentServiceImpl = new StudentServiceImpl();
            studentServiceImpl.setStudentRepository(getStudentRepositoryBean());
            return studentServiceImpl;
        }
    
        @Bean
        public LoggingAspect getLoggingAspectBean() {
            return new LoggingAspect();
        }
    }
    
    package com.springpeople.training.assignment;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    import com.springpeople.training.assignment.course.domain.Course;
    import com.springpeople.training.assignment.student.StudentModuleConfig;
    import com.springpeople.training.assignment.student.service.StudentService;
    
    public class App2 
    {
        public static void main( String[] args ) throws InterruptedException
        {
            ApplicationContext container = new AnnotationConfigApplicationContext(StudentModuleConfig.class);
            StudentService service3 = container.getBean(StudentService.class);
            System.out.println(service3.findAllStudentByCourse(new Course("Spring", 12, "advance")));
        }
    }
    
    @Bean
        public LoggingAspect getLoggingAspectBean() {
            return new LoggingAspect();
        }
    
    @ComponentScan(basePackages= {"com.springpeople.training.assignment.student.aspect"})