java 创建 bean 时出错。自动装配依赖项的注入失败。无法自动装配字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32579040/
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-11-02 20:25:57  来源:igfitidea点击:

Error creating bean.Injection of autowired dependencies failed.Could not autowire field

javaspringhibernatespring-mvc

提问by O.Kuz

I have a problem with my Spring configuration in Spring + Spring MVC + Hibernate + MySQL web application.Spring can't create beans that I was announce in Service class.

我在 Spring + Spring MVC + Hibernate + MySQL web 应用程序中的 Spring 配置有问题。Spring 无法创建我在 Service 类中声明的 bean。

Here is Controller class

这是控制器类

@Controller
@RequestMapping("/main")
public class MainController {

private static Logger LOGGER = Logger.getLogger("Controller");

@Autowired
private PersonServiceImpl personServiceImpl;

@Autowired
private ActionServiceImpl actionServiceImpl;



@RequestMapping(value = "/users" , method = RequestMethod.GET)
public String getUsers(Model model) {

    LOGGER.debug("Receive request for show all users");
    List<User> users = personServiceImpl.getAll();
    model.addAttribute("users", users);
    return "userspage";

}

@RequestMapping(value = "/users/add", method = RequestMethod.GET)
public String getAdd() {

    LOGGER.debug("Receive request to show add page");
    return "addpage";

}

@RequestMapping(value = "/users/add", method = RequestMethod.POST)
public String add(@ModelAttribute("userAttribute") User user,Actions 
actions) {

    LOGGER.debug("Recieve request to add a new user");
    personServiceImpl.add(user);
    actionServiceImpl.addAction(user.getId(), actions);
    return "addedpage";

}

@RequestMapping(value = "/users/delete", method = RequestMethod.GET)
public String delete(@RequestParam(value = "id", required = true)Integer 
id, Model model) {

    LOGGER.debug("Recieve request for deleting user");
    personServiceImpl.delete(id);
    model.addAttribute("id", id);
    return "deletedpage";

}

@RequestMapping(value = "/users/edit", method = RequestMethod.GET)
public String getEdit(@RequestParam(value = "id", required = true)Integer 
id, Model model) {

    LOGGER.debug("Recieve request to show editpage");
    model.addAttribute("userAttribute", personServiceImpl.get(id));
    return "editpage";

}

@RequestMapping(value = "/users/edit", method = RequestMethod.POST)
public String saveEdit(@ModelAttribute("userAttribute") User user,
                       @RequestParam(value = "id", required = 
true)Integer id, Model model,
                       Actions actions) {

    LOGGER.debug("Received request to update person");
    user.setId(id);
    personServiceImpl.edit(user);
    actionServiceImpl.editAction( id, actions);
    model.addAttribute("id", id);
    return "editedpage";

}

@RequestMapping(value = "/users/actions", method = RequestMethod.GET)
public String getActionsOfUser(@RequestParam(value = "id", required = 
true)Integer id, Model model,

    LOGGER.debug("Recieve request to show user Action");
    model.addAttribute("userId", personServiceImpl.get(id));
    model.addAttribute("userAction", 
actionServiceImpl.getListOfActions(user));
    return "userActionsPage";

}
}

Here is Service intarfaces

这是服务接口

public interface ActionService {

List<Actions> getListOfActions(User user);

void addAction(Integer id, Actions actions);

void editAction(Integer id, Actions actions);

}



public interface PersonService {

List<User> getAll();

User get(Integer id);

void add(User user);

void delete(Integer id);

void edit(User user);

}

And they implementations

他们实施

@Service
@Transactional
public class ActionServiceImpl implements ActionService {

private static Logger LOGGER = Logger.getLogger("actionService");

@Autowired
private SessionFactory sessionFactory;

@Bean
ActionService getActionService() {
    return new ActionServiceImpl();
}

public List<Actions> getListOfActions(User user){

    LOGGER.debug("Retriving all user actions");
    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery("from Actions where  user = " +    
user.getId());
    return query.list();
}

public void addAction(Integer id, Actions actions){

    LOGGER.debug("Adding addAction to user info");
    Session session = sessionFactory.getCurrentSession();
    actions.setActionType(ActionType.ADDING_NEW_USER);
    actions.setDate(new Date());
    User existingUser = (User) session.get(User.class , id);
    actions.setUser(existingUser);
    existingUser.getActions().add(actions);
    session.save(existingUser);
    session.save(actions);

}

public void editAction(Integer id, Actions actions){

    LOGGER.debug("Adding editAction to user info");
    Session session = sessionFactory.getCurrentSession();
    actions.setActionType(ActionType.EDITING_EXISTING_USER);
    actions.setDate(new Date());
    User existingUser = (User) session.get(User.class , id);
    actions.setUser(existingUser);
    existingUser.getActions().add(actions);
    session.save(existingUser);
    session.save(actions);

}
}


@Service
@Transactional
public class PersonServiceImpl implements  PersonService{

private static Logger LOGGER = Logger.getLogger("service");

@Autowired
private SessionFactory sessionFactory;

@Bean
PersonService getPersonService() {
    return new PersonServiceImpl();
}

public List<User> getAll() {

    LOGGER.debug("Retrieving all users");
    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery("from User ");
    return query.list();

}

public User get(Integer id) {

    Session session = sessionFactory.getCurrentSession();
    User user = (User) session.get(User.class, id);
    return user;

}

public void add(User user) {

    LOGGER.debug("Adding new user");
    Session session = sessionFactory.getCurrentSession();
    session.save(user);
}

public void delete(Integer id) {

    LOGGER.debug("Deleting existing user");
    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery("from User where id = " + id);
    User user = (User) query.uniqueResult();
    List<Actions> actions = user.getActions();
    session.delete(user);
    for(Actions actionses: actions ){
        session.delete(actionses);
    }

}

public void edit(User user) {

    LOGGER.debug("Editing existing user");
    Session session = sessionFactory.getCurrentSession();
    User existingUser = (User) session.get(User.class, user.getId());
    existingUser.setLogin(user.getLogin());
    existingUser.setPassword(user.getPassword());
    existingUser.setReal_name(user.getReal_name());
    session.save(existingUser);
}
}

Application-context

应用上下文

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-  
3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />

<!-- Scans the classpath for annotated components that will be  
auto-registered as Spring beans.
 For example @Controller and @Service.-->

<context:component-scan base-package="com.oleg.project.*" />

<!-- Configures the annotation-driven Spring MVC Controller programming 
model.-->
<mvc:annotation-driven />

<!-- Load Hibernate related configuration -->
<import resource="hibernate-context.xml" />

</beans>

And finally my StackTrace

最后是我的 StackTrace

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.oleg.project.Services.PersonServiceImpl com.oleg.project.Controller.MainController.personServiceImpl; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.oleg.project.Services.PersonServiceImpl] 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.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:305)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5016)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5528)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1809)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:618)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:565)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1471)
at javax.management.remote.rmi.RMIConnectionImpl.access0(RMIConnectionImpl.java:76)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1312)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1404)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:832)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:323)
at sun.rmi.transport.Transport.run(Transport.java:200)
at sun.rmi.transport.Transport.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run(TCPTransport.java:683)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.oleg.project.Services.PersonServiceImpl com.oleg.project.Controller.MainController.personServiceImpl; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.oleg.project.Services.PersonServiceImpl] 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:571) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ... 58 more Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.oleg.project.Services.PersonServiceImpl] 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:1326) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1072) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:967) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:543) ... 60 more

引起:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.oleg.project.Services.PersonServiceImpl com.oleg.project.Controller.MainController.personServiceImpl; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:未找到类型为 [com.oleg.project.Services.PersonServiceImpl] 的合格 bean 依赖项:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)} 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:571) 在 org.springframework. beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 在 org。springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ... 58 导致:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型 [com.oleg.project.Services 的合格 bean PersonServiceImpl] 已找到依赖项:预期至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)} 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1326) 在 org.springframework.beans。 factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1072) 在 org.springframework.beans.factory.support。

回答by M. Deinum

Your service is defined as follows.

您的服务定义如下。

@Service
@Transactional
public class ActionServiceImpl implements ActionService { ... }

Now what happens is due to the <context:component-scan />in your configuration Spring will create an instance of the ActionServiceImpl. You also have a <tx:annotation-driven />which detects the @Transactionaland creates a dynamic proxy (a nice $proxy12class or something like that). This proxy only implements the interfaces and as such is an ActionServicebut not an ActionServiceImpl.

现在发生的事情是由于<context:component-scan />在您的配置中 Spring 将创建一个ActionServiceImpl. 你也有一个<tx:annotation-driven />检测@Transactional并创建一个动态代理(一个不错的$proxy12类或类似的东西)。此代理仅实现接口,因此是一个ActionService但不是ActionServiceImpl.

To apply AOP spring uses proxies and when interfaces are involved it by default uses JDK Dynamic proxies which are interfaces only.

应用 AOP spring 使用代理,当涉及接口时,它默认使用仅作为接口的 JDK 动态代理。

To solve your problem you can do 1 of 2 things

要解决您的问题,您可以做 2 件事中的 1 件事

  1. Change the private PersonServiceImpl personServiceImpl;to private PersonService personService;.
  2. Force class based proxies for <tx:annotation-driven />, by specifying proxy-target-class="true"on the element.
  1. 将 更改private PersonServiceImpl personServiceImpl;private PersonService personService;
  2. <tx:annotation-driven />通过proxy-target-class="true"在元素上指定,强制使用基于类的代理。

Pro Tips

专业提示

  1. <context:annotation-config />is already implied when using <context:component-scan />you can remove that line.
  2. base-packageis what it states a base package it doesn't take an expression, change com.oleg.project.*to om.oleg.project.
  3. The @Beanmethod in your service doesn't add anything but clutter, remove it to cleanup your code.
  1. <context:annotation-config />使用时已经暗示<context:component-scan />您可以删除该行。
  2. base-package是它声明的基本包,它不需要表达式,更改com.oleg.project.*om.oleg.project.
  3. @Bean您的服务中的方法除了混乱之外不会添加任何内容,请将其删除以清理您的代码。