无法使用spring引导自动连接spring中的字段

无法使用spring引导自动连接spring中的字段,spring,hibernate,Spring,Hibernate,在过去的4-5天里,我一直在努力想办法解决spring&hibernate的问题,我觉得自己几乎成了他们的……我真的很困惑,为什么会出现这些自动连线错误 我试图创建一个restapi,所以我选择了mvc模式 有人知道我为什么会犯这些错误吗?我在下面提供了我的设置和堆栈跟踪 控制器包:spring.Controller 编辑1 我尝试删除@Repository(“userDAO”)中的标签,并使其成为@Repository。正如其中一个答案所建议的那样 这没有改变任何事情 编辑2 这是我的主要课程

在过去的4-5天里,我一直在努力想办法解决
spring&hibernate
的问题,我觉得自己几乎成了他们的……我真的很困惑,为什么会出现这些
自动连线
错误

我试图创建一个
restapi
,所以我选择了mvc模式

有人知道我为什么会犯这些错误吗?我在下面提供了我的设置和堆栈跟踪

控制器包:spring.Controller 编辑1 我尝试删除
@Repository(“userDAO”)
中的标签,并使其成为
@Repository
。正如其中一个答案所建议的那样

这没有改变任何事情

编辑2 这是我的主要课程:

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class Main {
    private static final SessionFactory ourSessionFactory;
    private static final ServiceRegistry serviceRegistry;

    static {
        try {
            Configuration configuration = new Configuration();
            configuration.configure();

            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            ourSessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static Session getSession() throws HibernateException {
        return ourSessionFactory.openSession();
    }

    public static void main(final String[] args) throws Exception {
        SpringApplication.run(Main.class, args);
    }
}
编辑3: 在尝试了m.deinum的解决方案之后,我得到了以下堆栈跟踪…删除daoImpl和abstractImpl是一个好主意。但我仍然不断地在创建名为X的bean时遇到这些错误:自动连接依赖项的注入失败了异常

调整代码以适应以下答案后的堆栈跟踪:

[WARNING] 
java.lang.reflect.InvocationTargetException
        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:483)
        at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:478)
        at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: spring.service.userService spring.controller.userController.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: hibernate.dao.userDAO spring.service.userServiceImpl.dao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [hibernate.dao.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)}
        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$1.getObject(AbstractBeanFactory.java:306)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766)
        at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
        at spring.Main.main(Main.java:44)
        ... 6 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: spring.service.userService spring.controller.userController.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: hibernate.dao.userDAO spring.service.userServiceImpl.dao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [hibernate.dao.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)}
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
        ... 23 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: hibernate.dao.userDAO spring.service.userServiceImpl.dao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [hibernate.dao.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)}
        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$1.getObject(AbstractBeanFactory.java:306)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
        ... 25 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: hibernate.dao.userDAO spring.service.userServiceImpl.dao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [hibernate.dao.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)}
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
        ... 36 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [hibernate.dao.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)}
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
        ... 38 more
只需从中删除名称

@Repository("userDAO")
public class userDAOImpl extends AbstractDao<Integer, user> implements userDAO {
@Repository(“userDAO”)
公共类userDAOImpl扩展了AbstractDao,实现了userDAO{
成功

@Repository
public class userDAOImpl extends AbstractDao<Integer, user> implements userDAO {
@存储库
公共类userDAOImpl扩展了AbstractDao,实现了userDAO{
只需从

@Repository("userDAO")
public class userDAOImpl extends AbstractDao<Integer, user> implements userDAO {
@Repository(“userDAO”)
公共类userDAOImpl扩展了AbstractDao,实现了userDAO{
成功

@Repository
public class userDAOImpl extends AbstractDao<Integer, user> implements userDAO {
@存储库
公共类userDAOImpl扩展了AbstractDao,实现了userDAO{

这可能是由于您定义了多个
造成的。最新的
正在生效。从异常情况来看,Spring确实在实例化服务bean

只用

<context:component-scan base-package="spring.controller, hibernate.dao, hibernate.model,spring.service" /> 

这可能是由于您定义了多个
造成的。最新的
正在生效。从异常情况来看,Spring确实在实例化服务bean

只用

<context:component-scan base-package="spring.controller, hibernate.dao, hibernate.model,spring.service" /> 

对于初学者,当前您的
应用程序上下文.xml
非常无用。如果删除它,重新启动,您仍然会得到相同的异常。这是由于Spring Boot(默认情况下)的工作方式

您的
Main
类在
spring
包中定义。spring引导自动扫描此包及其所有子包。但是,您的dao等位于
hibernate
中,spring引导组件扫描未涵盖

要么添加
@ImportResource(“您的/applicationContext.xml的位置”)
,要么简单地将
@ComponentScan({“spring”,“hibernate”})
添加到主类中,要么将DAO移动到
spring
的子包中

您的代码也有缺陷,您使用的是Spring,然后使用Spring,不要自己配置hibernate。为此使用
LocalSessionFactoryBean
,让Spring为您处理所有这些

@SpringBootApplication
public class Main {   

    public static void main(final String[] args) throws Exception {
        SpringApplication.run(Main.class, args);
    }

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
     return new LocalSessionFactoryBean();
    }
}
但是,与使用普通的Hibernate和滚动另一个抽象dao不同,我建议您删除所有这些。将JPA与Spring数据JPA一起使用,可以节省大量代码。(删除user
userDaoImpl
AbstractDao
及其附带的所有接口)

您的
userDao
看起来像

public interface userDao extends JpaRepository<User, Integer> {}
一般的建议是使用框架(首先阅读一些文档),而不是尝试新框架的旧方法


Pro提示:使用公认的命名约定
userserviceinpl
等。在Java语言中,这不是真正的定制,从大写字母开始
userserviceinpl

对于初学者来说,当前您的
applicationContext.xml
几乎毫无用处。如果删除它,重新启动,您仍然可以获得sam这是由于Spring引导(默认)的工作方式

您的
Main
类在
spring
包中定义。spring引导自动扫描此包及其所有子包。但是,您的dao等位于
hibernate
中,spring引导组件扫描未涵盖

要么添加
@ImportResource(“您的/applicationContext.xml的位置”)
,要么简单地将
@ComponentScan({“spring”,“hibernate”})
添加到主类中,要么将DAO移动到
spring
的子包中

您的代码也有缺陷,您使用的是Spring,然后使用Spring,不要自己配置hibernate。为此使用
LocalSessionFactoryBean
,让Spring为您处理所有这些

@SpringBootApplication
public class Main {   

    public static void main(final String[] args) throws Exception {
        SpringApplication.run(Main.class, args);
    }

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
     return new LocalSessionFactoryBean();
    }
}
但是,与使用普通的Hibernate和滚动另一个抽象dao不同,我建议您删除所有这些。将JPA与Spring数据JPA一起使用,可以节省大量代码。(删除user
userDaoImpl
AbstractDao
及其附带的所有接口)

您的
userDao
看起来像

public interface userDao extends JpaRepository<User, Integer> {}
一般的建议是使用框架(首先阅读一些文档),而不是尝试新框架的旧方法


Pro提示:使用公认的命名约定
userserviceinpl
等。在Java land中不是真正的定制,请以大写字母
userserviceinpl
开始请在以下位置使用@Qualifier注释:

public class userController {
    @Autowired
    @Qualifier("userService")
    userService userService;


并检查。

请在以下位置使用@Qualifier注释:

public class userController {
    @Autowired
    @Qualifier("userService")
    userService userService;


然后检查。

您在哪个包中有
userDAOImpl
类?
hibernate.dao
-我在上面添加了包位置^^^^您在哪个包中有
userDAOImpl
类?
hibernate.dao
-我在上面添加了包位置^^^^不幸的是,这并没有修复它。在AbstractDao的构造函数中打印一些东西,如果没有显示任何内容,则说明DAO没有被实例化。没有打印任何内容…尽管我不确定为什么DAO没有被实例化。不确定,您的主应用程序类上可能缺少@ImportResource(“classpath:applicationContext.xml”)?您的服务类在同一pac中吗
@Service("userService")
@Transactional
public class userServiceImpl implements userService {
    @Autowired
    userDAO dao;

    public List<user> findAll() {
        return dao.findAll();
    }
    ...
}
public class userController {
    @Autowired
    @Qualifier("userService")
    userService userService;
@Service("userService")
@Transactional
public class userServiceImpl implements userService {
    @Autowired
    @Qualifier("userDAO")
    userDAO dao;