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 例外情况:
@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"})