这个是没有导入jar包,maven代码如下
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.4</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.4</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.1</version>
</dependency>
不行,换成你给你依赖还是不行。我把log贴一下吧!Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'noticeDao' defined in file [D:\Java\workspace\sim\sim_common\target\classes\edu\cs\tnt\sim\common\dao\NoticeDao.class]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class edu.cs.tnt.sim.common.dao.NoticeDao]: 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.RuntimeException-->edu.cs.tnt.sim.common.dao.NoticeDao$$EnhancerBySpringCGLIB$$caa1927b没有指定继承的po类型
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:296)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at edu.cs.tnt.sim.common.dao.test.main(test.java:12)
Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class edu.cs.tnt.sim.common.dao.NoticeDao]: 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.RuntimeException-->edu.cs.tnt.sim.common.dao.NoticeDao$$EnhancerBySpringCGLIB$$caa1927b没有指定继承的po类型
at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:212)
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:1518)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
... 11 more
Caused by: org.springframework.cglib.core.CodeGenerationException: java.lang.RuntimeException-->edu.cs.tnt.sim.common.dao.NoticeDao$$EnhancerBySpringCGLIB$$caa1927b没有指定继承的po类型
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:206)
... 18 more
Caused by: java.lang.RuntimeException: edu.cs.tnt.sim.common.dao.NoticeDao$$EnhancerBySpringCGLIB$$caa1927b没有指定继承的po类型
at edu.cs.tnt.sim.common.dao.BaseDao.<init>(BaseDao.java:23)
at edu.cs.tnt.sim.common.dao.NoticeDao.<init>(NoticeDao.java:17)
at edu.cs.tnt.sim.common.dao.NoticeDao$$EnhancerBySpringCGLIB$$caa1927b.<init>(<generated>)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.springframework.cglib.core.ReflectUtils.newInstance(ReflectUtils.java:228)
... 26 more
SessionFactory sessionFactory; 子类不要这样注入,采用方法实行注入,你这样的话,子类的sessionFactory跟父类的是不一样的,
public void setSessionFactory(SessionFactory sessionfatory){
super.sessionFactory = sessionFactory;
}
这样子来
那父类中sessionfactory应该如何写?能不能给一个完整的例子?谢谢!我现在按你说的修改了一下子类还是cglib错误。。
那父类中sessionfactory应该如何写?能不能给一个完整的例子?谢谢!我现在按你说的修改了一下子类还是cglib错误。。子类就不用sessionFactory字段了,直接用父类的sessionFactory,父类的sessionFactory用protected修饰
Type type = this.getClass().getGenericSuperclass();
问题就出在这句代码。
这句代码的意思是获取这个类的直接父类的类型,也就是获取BaseDao的直接父类的类型,而你这里的父类是Object,他并不是参数化的类型,所以会执行你抛出异常的那句代码。
改进方式有两种:
1、将
Type type = this.getClass().getGenericSuperclass();
改为
Type type = this.getClass();2、另外写一个参数化接口,比如 IBaseDao<T>,使用BaseDao<T>继承他,然后其他代码就跟你上面的一样。
谢谢!按你说的第二个方法,不会报CGlib错误了,但是又开始
com.sun.proxy.$Proxy13 cannot be cast to
错误。
获取Bean的时候声明写成接口也还是这样
谢谢!按你说的第二个方法,不会报CGlib错误了,但是又开始
com.sun.proxy.$Proxy13 cannot be cast to
错误。你把异常贴出来啊,看的清楚点。
谢谢!按你说的第二个方法,不会报CGlib错误了,但是又开始
com.sun.proxy.$Proxy13 cannot be cast to
错误。
你还要为NoticeDao写一个接口INoticeDao,INoticeDao继承IBaseDao,因为你用getBean获取的对象并不是NoticeDao的实例,而是一个代理对象。