无法实例化bean类:找不到默认构造函数;嵌套异常是java.lang.NoSuchMethodException

时间:2012-12-07 16:24:29

标签: spring spring-mvc

我正在尝试@Autowired一个类是一个简单的类,没有Annotations,只是一个有一些计算的类。但是,当我尝试@Autowired该类时,在我的控制器中出现以下错误:

控制器:

@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;
}}

班级示例:

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

任何人都可以帮助我吗?感谢。

1 个答案:

答案 0 :(得分:1)

注入的MyClass实例必须是Spring bean。使用@Component或其他bean注释对其进行注释,或在spring XML配置文件中声明它。