Could not instantiate bean class: No default const

2019-09-11 15:41发布

I am trying to @Autowired a class which is a simple class, without Annotations, is just a class which has some calculation. However when I try to @Autowired that class, in my controller I get the following error:

Controller:

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

Example of class:

public class MyClass {

public Double calculation(){
       //implementation
    }

}

Error:

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

Anybody could help me? Thanks.

1条回答
叛逆
2楼-- · 2019-09-11 15:53

The injected MyClass instance must be a Spring bean. Annotate it with @Component or another bean annotation, or declare it in the spring XML config file.

查看更多
登录 后发表回答