aop使用的注意点

1. 使用类名引用,启动过程中,报错:

Description:

The bean 'newProductModuleService' could not be injected as a 'xxx.xxx.NewProductModuleService' because it is a JDK dynamic proxy that implements:
    xxx.xxx.HandleService


Action:

Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on @EnableAsync and/or @EnableCaching.

这个是因为,有地方使用类名引用这个类对象,注入bean时,jdk动态代理的bean只适用于接口名引用。

解决方法:在类上加@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)。

2. aop不生效,需要public方法,用bean引用调用。

如果是类自己调自己的方法,需要在类里面声明一个对自己的引用对象,再autoWired自己。否则会进不去切面。这个在使用transactionManager的注解时也有一样的问题。

@Service
XXService{
    
    @AutoWired
    privte XXService xxService;

    @Cachable
    public void aopMethod(){

    }
    
    public void callAopMethod(){
        xxService.aopMethod();
    }
}

3. 一个继承的例子

父类为了能引用其他bean,不做成抽象类,父类保留一些公共方法。

做接口是为了找出差异点方便扩展,做父类是未了寻找共同点,使用公共代码。

public class NewProductModuleService extends EntBaseModuleService implements HandleService 
public class ModuleEntranceService {

    @Autowired
    private List<HandleService> handleServiceList;
}

继承时,不应该在父类对所有实现现类做聚合autowired,会重复初始化。新增一个单独的聚合类。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值