栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

aop使用的注意点

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 handleServiceList;
}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/866947.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号