0

我正在尝试@Autowired一个简单的类,没有注释,只是一个有一些计算的类。但是,当我尝试@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

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

4

1 回答 1

1

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

于 2012-12-07T16:50:07.730 回答