您的位置:首页 > 编程语言 > Java开发

Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'dao' is exp

qq_35954591 2017-08-15 14:39 106 查看

异常信息:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'service': Unsatisfied dependency expressed through field 'dao'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'dao' is expected to be of type 'ifox.DAOImpl' but was actually of type 'com.sun.proxy.$Proxy14'

at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at ifox.TestDb.<init>(TestDb.java:20)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'dao' is expected to be of type 'ifox.DAOImpl' but was actually of type 'com.sun.proxy.$Proxy14'
at org.springframework.beans.factory.support.DefaultListableBeanFactory.checkBeanNotOfRequiredType(DefaultListableBeanFactory.java:1510)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1489)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
... 41 more


今天在看spring注解的时候遇到这个问题可以说弄的自己很难受,我先贴上我的部分错误源码

DAO接口
package ifox;

/**
* Created by exphuhong on 17-8-14.
*/
public interface DAO
{
public Db findDbById(int id);

public void saveBook(Db db);
}


DAO实现类

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
* Created by exphuhong on 17-8-14.
*/
@Repository("dao")
public class DAOImpl implements DAO{

@Autowired
private SessionFactory sessionFactory;

//获取和当前线程绑定的Seesion
private Session getSession()
{
return sessionFactory.getCurrentSession();
}

public Db findDbById(int id)
{
String hql = "from Db  WHERE id = ?";
return (Db) getSession().createQuery(hql).setInteger(0, id).uniqueResult();
}
public void saveBook(Db db)
{
getSession().save(db);
}
}


Service接口
package ifox;

/**
* Created by exphuhong on 17-8-14.
*/
public interface Service {
public Db findDbById(int id);
public void saveBook(Db db);

}


Service实现类

package ifox;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
* Created by exphuhong on 17-8-14.
*/
@org.springframework.stereotype.Service("service")
public class ServiceImpl implements Service {

@Autowired
@Qualifier("dao")
private DAO dao;   //错误用法  private DAOImpl dao;  原理在最下面

public Db findDbById(int id)
{
return dao.findDbById(id);
}
public void saveBook(Db db)
{
dao.saveBook(db);

}
}


测试类

package ifox;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
* Created by exphuhong on 17-8-14.
*/
public class TestDb {

private ApplicationContext context=null;
private Service service=null;

{
context= new ClassPathXmlApplicationContext("applicationContext.xml");
service = (Service) context.getBean("service");
}

@Test
public void test1() {
System.out.println(context.getBean("dao"));
}

@Test
public void test2()
{
Db db= (Db) service.findDbById(5);
System.out.println(db);
}

@Test
public void test3()
{
service.saveBook(new Db("张飞",21,"1"));
}
}

解释:

读者首先需要去了解java的反射机制以及java动态代理。spring注解是实现java注解接口的。注解的实现和java的一样,都是通过反射。通过反射实现动态代理机制,动态代理需要java.lang.reflect.InvocationHandler接口和java.lang.reflect.Prox类的支持。Prox类是专门完成代理的操作类,可以通过此类为一个或多个接口动态生成实现类。
所以这里在获取通过注解的注入的bean时(通过反射将该类(接口的实现类)对应的bean注入到IOC容器中)需要定义成接口类型了只有这样才叫动态代理。
标签: 
相关文章推荐