尽管在表达式中指定了确切的名称,但切入点表达式与 Spring 数据方法不匹配
Pointcut expression not matching the Spring Data method despite specfying exact name in expression
在我的 Spring 引导项目中,我有 AddressRepository,它从数据库中获取所有地址。我有一个切入点 class 和一个在调用 findAll()
方法之后执行的切入点表达式。当我执行我的测试用例时,没有触发 Advice 并且 findAll(Sort sort)
、findAll(Pageable pageable)
等其他方法工作正常。我不确定这是 Spring Boot 的错误还是我的表达。我尝试使用 Spring Boot 2.0.5 和 2.1.0,似乎没有解决我的问题
AddressLogging.java
@Aspect
@Configuration
public class AddressLogging {
private Logger log=LoggerFactory.getLogger(AddressLogging.class);
//@Pointcut("execution(* com.springtesting.repo.AddressRepository.*(..))")
@Pointcut("execution(* com.springtesting.repo.AddressRepository.findAll())")
public void getAddresses() {}
@After("getAddresses()")
public void afterAdvice() {
log.error("Log Message: Inside afterAdvice() advice");
}
}
AopTest.java
@RunWith(SpringRunner.class)
@SpringBootTest
public class AopTest {
@Autowired
private AddressRepository addressRepository;
@Test
public void getAddresses() {
//addressRepository.findAll(PageRequest.of(0,20, Sort.by("id")));
addressRepository.findAll();
}
@Test
public void findAddressById() {
addressRepository.findById(1L);
}
}
地址库
public interface AddressRepository extends JpaRepository<Address,Long> {}
一个Spring AOP 方面也应该是一个@Component
并且被组件扫描拾取。我不知道为什么你添加 @Configuration
到方面而不是因为这里没有配置。
也许您对单独配置的测试 class 应该带有 @Configuration
注释,您还应该激活 @EnableAspectJAutoProxy(proxyTargetClass = true)
和 @ComponentScan(basePackages = { "de.scrum_master" })
.
之类的东西
这是我的一个 Spring AOP 游乐场项目的片段(我几乎不使用它,我不使用 Spring AOP 甚至 Spring 本身,通常我使用更强大的AspectJ:
package de.scrum_master.app;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ComponentScan(basePackages = { "de.scrum_master" })
public class Application2 {
public static void main(String[] args) throws Exception {
ApplicationContext appContext = new AnnotationConfigApplicationContext(Application2.class);
B b = (B) appContext.getBean("b");
System.out.println(b.getData("bbb"));
A a = (A) appContext.getBean("b");
System.out.println(a.getData("aaa"));
}
}
在我的 Spring 引导项目中,我有 AddressRepository,它从数据库中获取所有地址。我有一个切入点 class 和一个在调用 findAll()
方法之后执行的切入点表达式。当我执行我的测试用例时,没有触发 Advice 并且 findAll(Sort sort)
、findAll(Pageable pageable)
等其他方法工作正常。我不确定这是 Spring Boot 的错误还是我的表达。我尝试使用 Spring Boot 2.0.5 和 2.1.0,似乎没有解决我的问题
AddressLogging.java
@Aspect
@Configuration
public class AddressLogging {
private Logger log=LoggerFactory.getLogger(AddressLogging.class);
//@Pointcut("execution(* com.springtesting.repo.AddressRepository.*(..))")
@Pointcut("execution(* com.springtesting.repo.AddressRepository.findAll())")
public void getAddresses() {}
@After("getAddresses()")
public void afterAdvice() {
log.error("Log Message: Inside afterAdvice() advice");
}
}
AopTest.java
@RunWith(SpringRunner.class)
@SpringBootTest
public class AopTest {
@Autowired
private AddressRepository addressRepository;
@Test
public void getAddresses() {
//addressRepository.findAll(PageRequest.of(0,20, Sort.by("id")));
addressRepository.findAll();
}
@Test
public void findAddressById() {
addressRepository.findById(1L);
}
}
地址库
public interface AddressRepository extends JpaRepository<Address,Long> {}
一个Spring AOP 方面也应该是一个@Component
并且被组件扫描拾取。我不知道为什么你添加 @Configuration
到方面而不是因为这里没有配置。
也许您对单独配置的测试 class 应该带有 @Configuration
注释,您还应该激活 @EnableAspectJAutoProxy(proxyTargetClass = true)
和 @ComponentScan(basePackages = { "de.scrum_master" })
.
这是我的一个 Spring AOP 游乐场项目的片段(我几乎不使用它,我不使用 Spring AOP 甚至 Spring 本身,通常我使用更强大的AspectJ:
package de.scrum_master.app;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ComponentScan(basePackages = { "de.scrum_master" })
public class Application2 {
public static void main(String[] args) throws Exception {
ApplicationContext appContext = new AnnotationConfigApplicationContext(Application2.class);
B b = (B) appContext.getBean("b");
System.out.println(b.getData("bbb"));
A a = (A) appContext.getBean("b");
System.out.println(a.getData("aaa"));
}
}