2012-12-07 49 views
0

我試圖@Autowired一類,這是一個簡單的類,沒有註釋,只是其中有一些計算類。然而,當我嘗試@Autowired這個類,在我的控制,我得到了以下錯誤:無法實例化bean類:找不到默認構造函數;嵌套的例外是java.lang.NoSuchMethodException

控制器:

public class MyClass { 

public Double calculation(){ 
     //implementation 
    } 

} 

錯誤:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private net.pontoall.hemisphere.core.calculo.evaportranspiracao.MyClass net.pontoall.hemisphere.controller.LeituraController.evapo; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [net.pontoall.hemisphere.core.calculo.evaportranspiracao.MyClass] 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:513) 
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92) 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284) 
... 21 more 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [net.pontoall.hemisphere.core.calculo.evaportranspiracao.MyClass] 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:948) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:817) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:731) 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:485) 
... 23 more 

@Controller 
public class LeituraController extends HemisphereController { 

private static final String VIEW = "calculation/index"; 

@Autowired 
private MyClass evapo; 

@RequestMapping(value = "/request", method = RequestMethod.GET) 
@SuppressWarnings("unchecked") 
public ModelAndView leituras() { 
    ModelAndView view = new ModelAndView(VIEW); 
    Double valorMax = evapo.calculation(); 
    return view; 
}} 

類的實例

有人可以幫助我嗎?謝謝。

+0

你的構造函數是什麼樣的? –

+0

需要關於該類的更多信息,以及從錯誤的堆棧跟蹤中獲取更多信息。 – Vikdor

+0

好的,我編輯了這個問題。謝謝。 –

回答

1

注入的MyClass實例必須是Spring bean。用@Component或其他bean註釋標註它,或者在spring XML配置文件中聲明它。

+0

我的XML:'<背景:組件掃描基礎包= 「net.pontoall.hemisphere.core.calculo.MyClass」/> ', 這是正確的嗎? –

+0

如其名稱所示,base-package期望包名稱爲值。不是班級名稱。 –

+0

我應該說:'net.pontoall.hemisphere.core.calculo' ?? –

相關問題