Java 无法自动装配字段,但我有定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3277846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:16:43  来源:igfitidea点击:

Could not autowire field, but I have the definition

javahibernatespringspring-mvc

提问by Blankman

My app-config.xml has a definition for my UserDao bean:

我的 app-config.xml 有我的 UserDao bean 的定义:

  <bean id="userDao" class="com.blah.core.db.hibernate.UserDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

I have my component scanning:

我有我的组件扫描:

<context:component-scan base-package="com.blah" />

My index action in my HomeController works fine (it outputs the contents of a method on my UserService to a freemarker template).

我的 HomeController 中的索引操作工作正常(它将 UserService 上的方法的内容输出到 freemarker 模板)。

@Controller
public class HomeController {

    @Autowired
    private UserService userService;




    @RequestMapping("/")
    public ModelAndView Index() {



        ModelAndView mav = new ModelAndView();

        mav.setViewName("index");
        mav.addObject("message", userService.sayHello());

        mav.addObject("username", userService.getTestUser());


        return mav;
    }

The 'getTestUser()' is a new method that makes reference to UserDao, it looks like:

'getTestUser()' 是一个引用 UserDao 的新方法,它看起来像:

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    UserDao userDao;

    public String sayHello() {

        return "hello from user service impl part 2";

    }

    public String getTestUser() {


        return userDao.getById(1L).getUsername();

    }


}

I am getting the error:

我收到错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.blah.core.db.hibernate.UserDao com.blah.core.services.UserServiceImpl.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.blah.core.db.hibernate.UserDao] 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)}
  1. What could the issue be?
  2. If I didn't do autowire, what would I be doing instead of adding the @AutoWire on the UserDao definition.
  1. 可能是什么问题?
  2. 如果我不做自动装配,我会做什么而不是在 UserDao 定义上添加 @AutoWire。

采纳答案by yclian

Have you tried exporting all registered beans of Spring (or read Spring's bootstrap logor the memory via debugging) to find out if the userDaobean is in the list. Please make sure that UserDaoImplis really implementing UserDaotoo - I am pointing at this because am not seeing the snippet of UserDaoImplhere.

您是否尝试导出 Spring 的所有注册 bean(或通过调试读取Spring 的引导日志或内存)以找出该bean 是否在列表中。请确保这也确实在实施- 我指出这一点是因为我没有看到这里的片段。userDaoUserDaoImplUserDaoUserDaoImpl

If you do not use @Autowired, the alternative will be explicitly obtaining a reference of the bean through ApplicationContext's getBean()(which is considered as a dirty way, fix your @Autowiredinstead), via its bean name, class name, etc.

如果您不使用@Autowired,则替代方法将通过 ApplicationContext 的getBean()显式获取 bean 的引用(这被认为是一种肮脏的方式,请修复您的@Autowired替代方法),通过其 bean 名称、类名称等。