2016-08-15 88 views
1

我是一位java开发人员,他正在使用公司所需的定制框架,该框架基于Spring。我设法使它的旧版本工作,但是这个新版本与我的@Repository有关,尽管事实上这个bean正在被扫描,但它不能被@Autowired。@Repository的自定义失败

以下是错误

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobInvoiceServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private foo.repository.impl.JobInvoiceRepositoryImpl foo.service.impl.JobInvoiceServiceImpl.jobInvoiceRepositoryImpl; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [foo.repository.impl.JobInvoiceRepositoryImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 
    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:772) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5003) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5517) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1574) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1564) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private foo.repository.impl.JobInvoiceRepositoryImpl foo.service.impl.JobInvoiceServiceImpl.jobInvoiceRepositoryImpl; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [foo.repository.impl.JobInvoiceRepositoryImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) 
    ... 22 more 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [foo.repository.impl.JobInvoiceRepositoryImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) 
    ... 24 more 

Aug 15, 2016 5:09:17 PM com.sun.faces.config.ConfigureListener contextInitialized 
INFO: Initializing Mojarra 2.2.5 (20140108-1427 https://svn.java.net/svn/mojarra~svn/tags/[email protected]) for context '/invoicer-application-war' 
0 [localhost-startStop-1] WARN org.springframework.web.context.support.XmlWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobInvoiceServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private foo.repository.impl.JobInvoiceRepositoryImpl foo.service.impl.JobInvoiceServiceImpl.jobInvoiceRepositoryImpl; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [foo.repository.impl.JobInvoiceRepositoryImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
19 [localhost-startStop-1] ERROR org.springframework.web.context.ContextLoader - Context initialization failed 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobInvoiceServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private foo.repository.impl.JobInvoiceRepositoryImpl foo.service.impl.JobInvoiceServiceImpl.jobInvoiceRepositoryImpl; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [foo.repository.impl.JobInvoiceRepositoryImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 
    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:772) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5003) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5517) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1574) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1564) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private foo.repository.impl.JobInvoiceRepositoryImpl foo.service.impl.JobInvoiceServiceImpl.jobInvoiceRepositoryImpl; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [foo.repository.impl.JobInvoiceRepositoryImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) 
    ... 22 more 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [foo.repository.impl.JobInvoiceRepositoryImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) 
    ... 24 more 

这里是应用程序代码:

@Controller

package foo.controller; 

import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Scope; 
import org.springframework.stereotype.Component; 
import org.springframework.stereotype.Controller; 

import foo.domain.JobInvoiceBean; 
import foo.service.impl.JobInvoiceServiceImpl; 

@SuppressWarnings("unused") 
@Component 
@Scope("view") 
public class InvoiceSearchController implements Serializable { 

    private static final long serialVersionUID = -1124694025090601008L; 

    private List<JobInvoiceBean> jobInvoiceList = new ArrayList<JobInvoiceBean>(); 
    private List<JobInvoiceBean> filteredJobInvoices = new ArrayList<JobInvoiceBean>(); 

    private JobInvoiceBean selectedJobInvoice; 

    @Autowired 
    private JobInvoiceServiceImpl jobInvoiceServiceImpl; 

    public void init() { 
     List<JobInvoiceBean> jobInvoiceList = jobInvoiceServiceImpl.getAllJobInvoice(); 
    } 


// public void getJobInvoiceDetail() { 
//  getJobInvoiceDetail(this.selectedJobInvoice); 
// } 
// 
// private void getJobInvoiceDetail(JobInvoiceBean jobInvoiceBean){ 
//  //This is essentially just a re-fetch 
//  JobInvoiceBean jobInvoice = jobInvoiceService.findJobInvoiceById(jobInvoiceBean.getInvoiceNo()); 
//  
//  jobInvoiceBean = jobInvoice; 
// } 

    /* 
    public void onPropertyEdit(CellEditEvent event) { 

     DataTable dataTable = (DataTable) event.getSource(); 
     PropertyBean modifiedProperty = (PropertyBean) dataTable.getRowData(); 

     Property property = jobInvoiceService.findPropertyById(modifiedProperty.getId()); 
     BeanUtils.copyProperties(modifiedProperty, property, "surface", "addresses"); 
     property.setSurface(BigDecimal.valueOf(modifiedProperty.getSurface())); 

     jobInvoiceService.saveOrUpdateProperty(property); 

     FacesMessage msg = new FacesMessage(
       getBundleMessage("property.edition.message"), 
       modifiedProperty.getName()); 
     FacesContext.getCurrentInstance().addMessage(null, msg); 
    } 

    */ 

    public JobInvoiceBean getSelectedJobInvoice() { 
     return selectedJobInvoice; 
    } 

    public void setSelectedJobInvoice(JobInvoiceBean selectedJobInvoice) { 
     this.selectedJobInvoice = selectedJobInvoice; 
    } 


    public List<JobInvoiceBean> getJobInvoiceList() { 
     return jobInvoiceList; 
    } 


    public void setJobInvoiceList(List<JobInvoiceBean> jobInvoiceList) { 
     this.jobInvoiceList = jobInvoiceList; 
    } 


    public List<JobInvoiceBean> getFilteredJobInvoices() { 
     return filteredJobInvoices; 
    } 


    public void setFilteredJobInvoices(List<JobInvoiceBean> filteredJobInvoices) { 
     this.filteredJobInvoices = filteredJobInvoices; 
    } 


} 

@Service

package foo.service.impl; 

import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 

import foo.domain.JobInvoiceBean; 
import foo.repository.impl.JobInvoiceRepositoryImpl; 
import foo.service.JobInvoiceService; 

@Service 
public class JobInvoiceServiceImpl implements JobInvoiceService { 

    @Autowired 
    private JobInvoiceRepositoryImpl jobInvoiceRepositoryImpl; 

    @Override 
    public List<JobInvoiceBean> getAllJobInvoice() { 
     return jobInvoiceRepositoryImpl.getAllInvoices(); 
    } 

} 

@Repository

package foo.repository.impl; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.Map; 

import javax.sql.DataSource; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.core.convert.ConversionService; 
import org.springframework.jdbc.core.BeanPropertyRowMapper; 
import org.springframework.jdbc.core.simple.SimpleJdbcCall; 
import org.springframework.stereotype.Repository; 
import org.springframework.transaction.annotation.Transactional; 

import foo.domain.JobInvoiceBean; 
import foo.repository.CustomJobInvoiceRepository; 

@Transactional(readOnly = true) 
@SuppressWarnings("unchecked") 
@Repository 
public class JobInvoiceRepositoryImpl implements CustomJobInvoiceRepository { 

    private static final Logger LOGGER = LoggerFactory.getLogger(JobInvoiceRepositoryImpl.class); 

    private BeanPropertyRowMapper<JobInvoiceBean> invoiceRowMapper; 

    private SimpleJdbcCall simpleJdbcCall; 
    private DataSource dataSource; 

    @Override 
    public List<JobInvoiceBean> getAllInvoices() { 
     List<JobInvoiceBean> listInvoice = new ArrayList<JobInvoiceBean>(); 
     try{ 
      simpleJdbcCall = new SimpleJdbcCall(dataSource); 

      simpleJdbcCall.withCatalogName("invoicer_pkg") 
         .withFunctionName("get_job_invs") 
         .returningResultSet("ret_cursor", invoiceRowMapper); 

//   SqlParameterSource in = new MapSqlParameterSource() 
//    .addValue("clientReference", clientReference) 
//    .addValue("hasChildren", hasChildren ? "Y" : "N"); 

      Map<String, Object> result = simpleJdbcCall.execute();   

      if (result != null && result.size() > 0) { 
       listInvoice = (List<JobInvoiceBean>) result.get("ret_cursor"); 
      } 

     } catch (Exception e) { 
      LOGGER.error(e.getMessage()); 
     }  
     return listInvoice; 
    } 

    @Autowired 
    public void setDataSource(DataSource dataSource) { 
     this.dataSource = dataSource; 
    } 

    @Autowired 
    public void setConversionService(final ConversionService conversionService) { 
     this.invoiceRowMapper = new BeanPropertyRowMapper<JobInvoiceBean>(JobInvoiceBean.class);   
    } 
} 

有些东西我可能丢失了,但我看不到它。

在此先感谢。

+0

异常显示,它正在寻找一个'CustomJobInvoiceRepositoryImpl',它不应用在您发布的代码中的任因此,例外情况是错误的或者您的代码示例不完整。 – dunni

+0

我最近在@Autowired中遇到了一个问题,它没有找到该类,因为它不在包含初学者类的包的子包中。这可能是这里的问题吗? –

+0

您应该在堆栈跟踪中隐藏客户/雇主的包名称:) –

回答

0

你JobInvoiceRepositoryImpl类实现CustomJobInvoiceRepository接口,并标注有@Transactional。

Spring AOP Transactional基础结构检测到它的事务性并创建实现CustomJobInvoiceRepository并委托给JobInvoiceRepositoryImpl的事务性JDK代理。

应用程序上下文中的实际Bean不再是JobInvoiceRepositoryImpl,而是代理。

你不能老是注入JobInvoiceRepositoryImpl而使用界面如下

@Service 
public class JobInvoiceServiceImpl implements JobInvoiceService { 

    @Autowired 
    private CustomJobInvoiceRepository jobInvoiceRepository; 

    @Override 
    public List<JobInvoiceBean> getAllJobInvoice() { 
     return jobInvoiceRepository.getAllInvoices(); 
    } 

} 

否则指示春季使用使用基于类的代理CGLIB如

@EnableTransactionManagement(proxyTragetClass = true) 

或等价的XML

<tx:annotation-driven proxy-target-class="true"/> 
0

需要声明这样@Repository(“beanName”)和@Service(“beanName”)将变成一个Spring bean在自动检测组件

0

首先的情况下,你必须使用@Autowired在界面中不在Impl中。然后,如果您有几个此Bean的实现,请使用@Qualifier批注。如果bean是尚未扫描的,也许你需要的@ComponentScan配置扫描你@Component (@Repository, @Service等)封装