2016-08-15 125 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等)封裝