Spring AOP 不适用于 @Component class
Spring AOP doesn't work with @Component class
我正在使用 Spring AOP 进行异常处理,但有一点我猜想我的组件 class 不在 Spring 代理中,所以 Spring AOP 注释我创建的那个在 class.
中不起作用
@Configuration
@AllArgsConstructor
public class MGRuleConfig {
private final GRepository repository;
private final GInitializer initializer;
private final GMapper mapper;
@Bean
@Qualifier("mRules")
public List<GRules> mRules(){
SSRule rule1 = new SSRule();
CSRule rule2 = new CSRule();
MPRule rule3 = new MPRule();
EGRule rule4 = new EGRule();
return List.of(rule1, rule2, rule3, rule4);
}
@Bean
public GService gService() {
return new MGServiceImpl(repository, initializer, mapper);
}
}
那我就有这个服务了;
@Service
@RequiredArgsConstructor
public class MGServiceImpl implements GService {
............
@Override
public GaDTO executeRules(String gId, Integer pN) {
Ga ga = repository.findById(gId);
GaDTO gaDTO = mapper.toDTO(ga);
List<GaRules> mRules = (List<GaRules>) applicationContext.getBean("mRules");
mRules.forEach(rule -> rule.apply(gaDTO, pN));
repository.save(mapper.toEntity(gaDTO));
return gaDTO;
}
我需要将我的异常处理注释放入 apply 方法中,但方面在该方法中不起作用。
@Component
public class SSRule implements GaRules {
@Override
@IPException
public void apply(GaDTO gaDTO, Integer pN) {
PDTO p1 = gaDTO.getP1();
PDTO p2 = gaDTO.getP2();
if (PTEnum.P_1.equals(gaDTO.getPT())) {
sS(gaDTO, pN, p1, p2);
} else {
sS(gaDTO, pN, p2, p1);
}
}
注释在那里不起作用。这是我的方面 class;
@Aspect
@Component
public class IPExceptionAspect {
@Around("execution(public * c.m.s.r.i.SSRule.apply(..)) && " +
"@annotation(c.m.s.i.a.IPException)")
public Object checkIP(ProceedingJoinPoint pjp) throws Throwable {
pjp.proceed();
return pjp;
}
}
那么,我应该怎么做才能让 IPException 注释和我的 Spring AOP 工作,为什么不工作工作?
问题出在您的代码上,您正在 在 一个 bean 方法中自己创建这些规则的实例,并将它们公开为 List
。这意味着该 bean 的类型为 List
而不是您自己的 SSRule
,因此它不会工作。
改为制作@Bean
方法,或使用检测到的实例注入到列表中。当您的 SSRule
被注释时,您将已经有一个实例,只需将其注入您的 @Bean
方法即可。
Bean
@Qualifier("mRules")
public List<GRules> mRules(SSRule rule1){
CSRule rule2 = new CSRule();
MPRule rule3 = new MPRule();
EGRule rule4 = new EGRule();
return List.of(rule1, rule2, rule3, rule4);
}
现在您将获得应用 AOP 的 Spring 托管实例。
虽然我很难称这个 AOP,因为它对一个人来说太具体了 class(在这方面不是真正的横切)。
我正在使用 Spring AOP 进行异常处理,但有一点我猜想我的组件 class 不在 Spring 代理中,所以 Spring AOP 注释我创建的那个在 class.
中不起作用@Configuration
@AllArgsConstructor
public class MGRuleConfig {
private final GRepository repository;
private final GInitializer initializer;
private final GMapper mapper;
@Bean
@Qualifier("mRules")
public List<GRules> mRules(){
SSRule rule1 = new SSRule();
CSRule rule2 = new CSRule();
MPRule rule3 = new MPRule();
EGRule rule4 = new EGRule();
return List.of(rule1, rule2, rule3, rule4);
}
@Bean
public GService gService() {
return new MGServiceImpl(repository, initializer, mapper);
}
}
那我就有这个服务了;
@Service
@RequiredArgsConstructor
public class MGServiceImpl implements GService {
............
@Override
public GaDTO executeRules(String gId, Integer pN) {
Ga ga = repository.findById(gId);
GaDTO gaDTO = mapper.toDTO(ga);
List<GaRules> mRules = (List<GaRules>) applicationContext.getBean("mRules");
mRules.forEach(rule -> rule.apply(gaDTO, pN));
repository.save(mapper.toEntity(gaDTO));
return gaDTO;
}
我需要将我的异常处理注释放入 apply 方法中,但方面在该方法中不起作用。
@Component
public class SSRule implements GaRules {
@Override
@IPException
public void apply(GaDTO gaDTO, Integer pN) {
PDTO p1 = gaDTO.getP1();
PDTO p2 = gaDTO.getP2();
if (PTEnum.P_1.equals(gaDTO.getPT())) {
sS(gaDTO, pN, p1, p2);
} else {
sS(gaDTO, pN, p2, p1);
}
}
注释在那里不起作用。这是我的方面 class;
@Aspect
@Component
public class IPExceptionAspect {
@Around("execution(public * c.m.s.r.i.SSRule.apply(..)) && " +
"@annotation(c.m.s.i.a.IPException)")
public Object checkIP(ProceedingJoinPoint pjp) throws Throwable {
pjp.proceed();
return pjp;
}
}
那么,我应该怎么做才能让 IPException 注释和我的 Spring AOP 工作,为什么不工作工作?
问题出在您的代码上,您正在 在 一个 bean 方法中自己创建这些规则的实例,并将它们公开为 List
。这意味着该 bean 的类型为 List
而不是您自己的 SSRule
,因此它不会工作。
改为制作@Bean
方法,或使用检测到的实例注入到列表中。当您的 SSRule
被注释时,您将已经有一个实例,只需将其注入您的 @Bean
方法即可。
Bean
@Qualifier("mRules")
public List<GRules> mRules(SSRule rule1){
CSRule rule2 = new CSRule();
MPRule rule3 = new MPRule();
EGRule rule4 = new EGRule();
return List.of(rule1, rule2, rule3, rule4);
}
现在您将获得应用 AOP 的 Spring 托管实例。
虽然我很难称这个 AOP,因为它对一个人来说太具体了 class(在这方面不是真正的横切)。