问题描述
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fileUploadService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'fileUploadDao' is definedat org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:300)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1064)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:872)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)at JUnit.test.ArticleInforServiceTest.setUpBeforeClass(ArticleInforServiceTest.java:42)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)at java.lang.reflect.Method.invoke(Method.java:597)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)at org.junit.runners.ParentRunner.run(ParentRunner.java:300)at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'fileUploadDao' is definedat org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:510)at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1056)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:274)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:435)at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:409)at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:541)at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:147)at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:297)... 28 more部分源码: /** */@Repositorypublic class FileUploadDAO extends BaseDAO<FileUpload> {} model类@Entity@Table(name="uploadfile")public class FileUpload implements Serializable{/** * 文件上传 */private static final long serialVersionUID = 7194554589429406803L;@Id @GeneratedValueprivate Integer id;@Column(nullable=false,length=80)private String filepath;@Temporal(TemporalType.TIMESTAMP)private Date uploadtime;public FileUpload(){}public FileUpload(String filepath) {this.filepath = filepath;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getFilepath() {return filepath;}public void setFilepath(String filepath) {this.filepath = filepath;}public Date getUploadtime() {return uploadtime;}public void setUploadtime(Date uploadtime) {this.uploadtime = uploadtime;} /**接口类*/public interface IFileUploadService extends ServiceInterface<FileUpload> { public List<FileUpload> getFilePath(Integer[] ids); public List<FileUpload> findAllUploadFile(); @SuppressWarnings("rawtypes")public Page findAllUploadFile(Page page,List<PropertyFilter> pfList); @Component("fileUploadService")public class FileUploadService extends BaseService<FileUpload>implements IFileUploadService { @Resource(name="fileUploadDao")//@Resource//@Qualifier("uploadFileDao")private DAOInterface<FileUpload> fileUploadDao;public List<FileUpload> getFilePath(Integer[] ids) {// TODO Auto-generated method stubif(ids!=null && ids.length>0){StringBuffer jpql = new StringBuffer();String hql;for(int i=0; i<ids.length;i++){jpql.append('?').append((i+1)).append(',');}jpql.deleteCharAt(jpql.length()-1);hql=" select o.filepath from UploadFile o where o.id in("+ jpql.toString()+ ")";return null;}return null;}@SuppressWarnings({ "unchecked", "rawtypes" })public Page findAllUploadFile(Page page, List<PropertyFilter> pfList) {//初始化hqlStringBuffer hql=new StringBuffer("from UploadFile");//设置查询条件String condition= QueryUtil.toSqlString(pfList, true);hql.append(condition);//查找所有用户的总条数int totalCount=this.fileUploadDao.findCountBySql(hql.toString());page.setTotalCount(totalCount);//是否存在排序if(page.isOrderBySetted()){hql.append(page.getOrder());}List<FileUpload> list=this.fileUploadDao.findList(hql.toString(), page.getPageNo(), page.getPageSize());//查出结果集page.setResult(list);return page;}public List<FileUpload> findAllUploadFile() {String hql="from UploadFile";return this.fileUploadDao.findList(hql);}public DAOInterface<FileUpload> getFileUploadDao() {return fileUploadDao;}public void setFileUploadDao(DAOInterface<FileUpload> fileUploadDao) {this.fileUploadDao = fileUploadDao;}@Overrideprotected DAOInterface<FileUpload> getDAO() {// TODO Auto-generated method stubreturn fileUploadDao;} 问题补充:<div class="quote_title">qiankun 写道</div><div class="quote_div">@Repository("fileUploadDao")<br />public class FileUploadDAO extends BaseDAO<FileUpload> { <br /> <br />}试试</div><br />谢谢!既然可以哦,后来我用这个模式@Resource(type="xxxx.class")junit的时候可以!但是部署的时候不行。。。。!<br />谢谢<img src="/images/smiles/icon_biggrin.gif"/>
解决方案
@Repository("fileUploadDao")public class FileUploadDAO extends BaseDAO<FileUpload> { }试试