基于注解的Spring AOP拦截含有泛型的DAO
- 出错场景
1、抽象类BaseDao
public abstract class BaseDao<T> {
public BaseDao() {
entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
private Class<T> entityClass;
@Resource
private SessionFactory sessionFactory;
}
2、applicationContext.xml文件中配置:需要引入aop的命名空间和增加如下配置
<aop:aspectj-autoproxy proxy-target-class="true"/>
3、编写自己的aop类
@Component
@Aspect
public class GlobalValueAspect {
@Pointcut(
"execution(* com.test.dao.SceneryDao.save*(..)) " +
"|| execution(* com.test.dao.SceneryDao.update*(..)) " +
"|| execution(* com.test.dao.SceneryDao.delete*(..))")
public void aspect(){}
@Before("aspect()")
public void before(JoinPoint joinPoint){
System.out.println("before");
}
@After("aspect()")
public void after(JoinPoint joinPoint){
System.out.println("after");
}
@Around("aspect()")
public void around(JoinPoint joinPoint){
System.out.println("around");
}
@AfterThrowing("aspect()")
public void afterThrowing(){
System.out.println("afterthrow");
}
@AfterReturning("aspect()")
public void afterReturn(JoinPoint joinPoint){
}
@Resource
private GlobalValueAspectDao globalValueAspectDao;
}
4、运行时会报一个错误
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sceneryDao' defined in file : Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.lztravel.dao.SceneryDao]: Common causes of this problem include using a final class or a non-visible class; nested exception is org.springframework.cglib.core.CodeGenerationException: java.lang.ClassCastException-->java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3461)
at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:426)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1361)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1653)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1662)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1662)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1642)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class : Common causes of this problem include using a final class or a non-visible class; nested exception is org.springframework.cglib.core.CodeGenerationException: java.lang.ClassCastException-->java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:211)
at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:111)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:477)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:362)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:322)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:409)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1488)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
... 21 more
Caused by: org.springframework.cglib.core.CodeGenerationException: java.lang.ClassCastException-->java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
at org.springframework.cglib.core.ReflectUtils.newInstance(ReflectUtils.java:235)
at org.springframework.cglib.core.ReflectUtils.newInstance(ReflectUtils.java:220)
at org.springframework.cglib.core.ReflectUtils.newInstance(ReflectUtils.java:216)
at org.springframework.cglib.proxy.Enhancer.createUsingReflection(Enhancer.java:643)
at org.springframework.cglib.proxy.Enhancer.firstInstance(Enhancer.java:538)
at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:225)
at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
at org.springframework.cglib.proxy.Enhancer.create(Enhancer.java:285)
at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:205)
... 28 more
Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
at com.test.dao.BaseDao.<init>(BaseDao.java:23)
at com.test.dao.SceneryDao.<init>(SceneryDao.java:13)
- 原因分析
Spring AOP是通过代理实现的,代理类继承了这里的SceneryDao,而SceneryDao继承了抽象类BaseDao,所以对于aop代理类来说,执行BaseDao中下面这句代码时出错了。
entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
因为代理类的父类是SceneryDao,而SceneryDao不是一个泛型类型(ParameterizedType),所以出现强制类型转换错误。
- 解决方法:
在BaseDao的构造方法中增加一个判断语句:if (getClass().getGenericSuperclass() instanceof ParameterizedType)。具体修改构造方法如下:
public BaseDao() {
if (getClass().getGenericSuperclass() instanceof ParameterizedType) {
entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass())
.getActualTypeArguments()[0];
} else {
entityClass = (Class<T>) ((ParameterizedType) getClass().getSuperclass().getGenericSuperclass())
.getActualTypeArguments()[0];
}
}
基于注解的Spring AOP拦截含有泛型的DAO的更多相关文章
- 基于注解的Spring AOP的配置和使用
摘要: 基于注解的Spring AOP的配置和使用 AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不 ...
- 基于注解的Spring AOP示例
基于注解的Spring AOP示例 目录 在XML配置文件中开启 @AspectJ 支持 声明切面及切入点 声明通知 测试 结语 在XML配置文件中开启 @AspectJ 支持 要使用Spring的A ...
- 基于注解的Spring AOP的配置和使用--转载
AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...
- 基于注解的Spring AOP入门、增强Advice实例
这篇文章简单通过一个例子,介绍几种增强的基本配置,以方便spring框架初学者对aop的代码结构有个清楚的了解认识.首先,spring支持aop编程,支持aspectJ的语法格式来表示切入点,切面,增 ...
- aop 拦截含有特定注解的类
1.功能点:使用aop拦截含有自定义注解的类 1.自定义注解 package com.zhuanche.common.dingdingsync; import java.lang.annotation ...
- Spring Aop(二)——基于Aspectj注解的Spring Aop简单实现
转发地址:https://www.iteye.com/blog/elim-2394762 2 基于Aspectj注解的Spring Aop简单实现 Spring Aop是基于Aop框架Aspectj实 ...
- 基于注解的Spring多数据源配置和使用(非事务)
原文:基于注解的Spring多数据源配置和使用 1.创建DynamicDataSource类,继承AbstractRoutingDataSource package com.rps.dataSourc ...
- Spring:基于注解的Spring MVC
什么是Spring MVC Spring MVC框架是一个MVC框架,通过实现Model-View-Controller模式来很好地将数据.业务与展现进行分离.从这样一个角度来说,Spring MVC ...
- Spring7:基于注解的Spring MVC(下篇)
Model 上一篇文章<Spring6:基于注解的Spring MVC(上篇)>,讲了Spring MVC环境搭建.@RequestMapping以及参数绑定,这是Spring MVC中最 ...
随机推荐
- 学会Git玩转Github笔记(三)—— Github Pages 搭建个人网站
https://help.github.com/categories/github-pages-basics/ 一.个人站点 访问 https://用户名.github.io 搭建步骤 1) 创建个人 ...
- python 特征缺失值填充
python数据预处理之缺失值简单处理:https://blog.csdn.net/Amy_mm/article/details/79799629 该博客总结比较详细,感谢博主. 我们在进行模型训练时 ...
- property 与 attribute 的区别?
一个是属性,用于存取类的字段,一个是特性,用来标识类,方法等的附加性质. 属性: class TimePeriod { private double seconds; public double Ho ...
- apache与和mysql重启命令
修改linux服务器的http配置之后,必须重启Apache服务. 命令为: /etc/rc.d/init.d/httpd restart chown -R mysql:mysql 目录名 改变文件属 ...
- [sh]清理memcached缓存
#!/bin/bash ###author xxx ###date xxx ###清理内存缓存 used=`free -m | awk 'NR==2' | awk '{print $3}'` free ...
- Entity Framework "There is already an open DataReader associated with this 的解决办法
解决办法: 1,修改连接串,加上MultipleActiveResultSets=true 2, 一次性先把数据读出来 var contacts = from c in db.Contact sele ...
- C++游戏系列5:不止有一件武器
很多其它见:C++游戏系列文件夹 知识点:对象数组作为数据成员 改进:每一个角色所持有的武器不仅仅一件,故持有的武器,用了对象数组来表示,当然,也能够是空手. 由此而带来的,还得记录一共同拥有几件武器 ...
- HDU 1978 How many ways DP问题
How many ways Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- superobject 序列数据集
unit uDBJson; interface {$HINTS OFF} uses SysUtils, Classes, Variants, DB, DBClient, SuperObject; ty ...
- [转]C++ error C2011: “XXX”:“class”类型重定义
http://blog.csdn.net/m_leonwang/article/details/27678219 尝试修复这个程序的错误: 点击下载源代码文件夹