当前位置: 首页 > 知识库问答 >
问题:

自动连线依赖项的注入失败;嵌套的异常是org。springframework。豆。工厂BeanCreationException:无法自动关联字段

沈枫涟
2023-03-14

我在运行代码时出错。

错误消息:

类型异常报告

消息Servlet。servlet mvc调度程序的init()引发异常

说明服务器遇到内部错误,无法满足此请求。

例外情况

javax。servlet。ServletException:Servlet。servlet mvc调度程序的init()引发异常组织。阿帕奇。卡塔琳娜。验证者。AuthenticatorBase。调用(AuthenticatorBase.java:502)org。阿帕奇。卡塔琳娜。阀门。错误报告阀。调用(ErrorReportValve.java:79)组织。阿帕奇。卡塔琳娜。阀门。抽象逻辑阀。调用(AbstractAccessLogValve.java:616)org。阿帕奇。卡塔琳娜。连接器。郊狼适应者。服务(CoyoteAdapter.java:528)org。阿帕奇。郊狼。http11。抽象http11处理器。process(AbstractHttp11Processor.java:1099)org。阿帕奇。郊狼。AbstractProtocol$AbstractConnectionHandler。进程(AbstractProtocol.java:672)org。阿帕奇。公猫util。网NioEndpoint$SocketProcessor。doRun(NioEndpoint.java:1520)org。阿帕奇。公猫util。网NioEndpoint$SocketProcessor。运行(NioEndpoint.java:1476)java。util。同时发生的线程池执行器。runWorker(ThreadPoolExecutor.java:1142)java。util。同时发生的线程池执行器$Worker。运行(ThreadPoolExecutor.java:617)org。阿帕奇。公猫util。线程。TaskThread$WrappingRunnable。运行(TaskThread.java:61)java。朗。丝线。运行(Thread.java:745)

组织。springframework。豆。工厂BeanCreationException:创建名为“tipoSanguineoController”的bean时出错:自动关联依赖项的注入失败;嵌套的异常是org。springframework。豆。工厂BeanCreationException:无法自动关联字段:private br。inf.datus。伊菲西奥。服务TIPOSANGINEOSERVICE br。inf.datus。伊菲西奥。控制器。TipoSanguineoController。TiposanguinoService;嵌套的异常是org。springframework。豆。工厂BeanCreationException:创建名为“TiposAnguineServiceImpl”的bean时出错,该名称在文件[D:\projetos\ifisio\target\ifisio\WEB-INF\classes\br\INF\datus\ifisio\service\TiposAnguineServiceImpl.class]中定义:bean实例化失败;嵌套的异常是org。springframework。豆。BeanInstationException:未能实例化[br.inf.datus.ifisio.service.TipoSanguineoServiceImpl]:未找到默认构造函数;嵌套的异常是java。lang.NoSuchMethodException:br。inf.datus。伊菲西奥。服务TipoSanguineoServiceImpl。()组织。springframework。豆。工厂注释。AutowiredNotationBeanPostProcessor。PostProcessPropertyValue(AutowiredNotationBeanPostProcessor.java:334)组织。springframework。豆。工厂支持AbstractAutowireCapableBeanFactory。populateBean(AbstractAutowireCapableBeanFactory.java:1202)org。springframework。豆。工厂支持AbstractAutowireCapableBeanFactory。doCreateBean(AbstractAutowireCapableBeanFactory.java:537)org。springframework。豆。工厂支持AbstractAutowireCapableBeanFactory。createBean(AbstractAutowireCapableBeanFactory.java:476)org。springframework。豆。工厂支持AbstractBeanFactory 1美元。getObject(AbstractBeanFactory.java:303)org。springframework。豆。工厂支持DefaultSingletonBeanRegistry。getSingleton(DefaultSingletonBeanRegistry.java:230)org。springframework。豆。工厂支持抽象工厂。doGetBean(AbstractBeanFactory.java:299)org。springframework。豆。工厂支持抽象工厂。getBean(AbstractBeanFactory.java:194)org。springframework。豆。工厂支持DefaultListableBeanFactory。预实例化单例(DefaultListableBeanFactory.java:755)org。springframework。上下文支持AbstractApplicationContext。finishBeanFactoryInitialization(AbstractApplicationContext.java:757)org。springframework。上下文支持AbstractApplicationContext。刷新(AbstractApplicationContext.java:480)组织。springframework。网状物servlet。FrameworkServlet。configureAndRefreshWebApplicationContext(FrameworkServlet.java:663)org。springframework。网状物servlet。FrameworkServlet。createWebApplicationContext(FrameworkServlet.java:629)org。springframework。网状物servlet。FrameworkServlet。createWebApplicationContext(FrameworkServlet.java:677)org。springframework。网状物servlet。FrameworkServlet。initWebApplicationContext(FrameworkServlet.java:548)org。springframework。网状物servlet。FrameworkServlet。initServletBean(FrameworkServlet.java:489)org。springframework。网状物servlet。HttpServletBean。init(HttpServletBean.java:136)javax。servlet。GenericServlet。init(GenericServlet.java:158)org。阿帕奇。卡塔琳娜。验证者。AuthenticatorBase。调用(AuthenticatorBase.java:502)org。阿帕奇。卡塔琳娜。阀门。错误报告阀。调用(ErrorReportValve.java:79)组织。阿帕奇。卡塔琳娜。阀门。抽象逻辑阀。调用(AbstractAccessLogValve.java:616)org。阿帕奇。卡塔琳娜。连接器。郊狼适应者。服务(CoyoteAdapter.java:528)org。阿帕奇。郊狼。http11。抽象http11处理器。process(AbstractHttp11Processor.java:1099)org。阿帕奇。郊狼。AbstractProtocol$AbstractConnectionHandler。进程(AbstractProtocol.java:672)org。阿帕奇。公猫util。网NioEndpoint$SocketProcessor。doRun(NioEndpoint.java:1520)org。阿帕奇。公猫util。网NioEndpoint$SocketProcessor。运行(NioEndpoint.java:1476)java。util。同时发生的线程池执行器。runWorker(ThreadPoolExecutor.java:1142)java。util。同时发生的线程池执行器$Worker。运行(ThreadPoolExecutor.java:617)org。阿帕奇。公猫util。线程。TaskThread$WrappingRunnable。运行(TaskThread.java:61)java。朗。丝线。运行(Thread.java:745)

根本原因

org.springframework.beans.factory.BeanCreation异常:无法自动连接字段:私有br.inf.datus.ifisio.service.TipoSanguineoServicebr.inf.datus.ifisio.controller.TipoSanguineoController.tipoSanguineoService;嵌套异常是org.springframework.beans.factory.BeanCreation异常:创建名为'tipoSanguineoServiceImpl'的bean时出错ifisio\Target\ifisio\WEB-INF\class\br\inf\datus\ifisio\service\TipoSanguineoServiceImpl.class]: bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationExc0019:未能实例化[br.inf.datus.ifisio.service.TipoSanguineoServiceImpl]:未找到默认构造函数;嵌套异常是<-br.inf.datus.ifisio.service.TipoSanguineoServiceImpl.()org.springframework.beans.factory.annotation.autowiredAnnotationBeanPostProcator$autowiredFieldElement.inject(autowiredAnnotationBeanPostProcessor.java:561)org.springframework.beans.factory.annotation.注入etadata.inject(InjessMetadata.java:88)org.springframework.beans.factory.annotation.autowiredAnnotationBeanPostProcessor.postProcess属性值(AutowiredAnnotationBeanPostProcessor.java:331)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory. PopateBean(AbstractAutowireCapableBeanFactory. java: 1202)org. springframework. beans. factory. support。AbstractAutowireCapableBeanFactory. doCreateBean(AbstractAutowireCapableBeanFactory. java: 537)org. springframework. beans. factory. support.AbstractAutowireCapableBeanFactory. createBean(AbstractAutowireCapableBeanFactory. java: 476)org. springframework. beans. factory. support.AbstractBeanFactory1美元. getObject(AbstractBeanFactory. java: 303)org. springframework. beans. factory. support.DefaultSingletonBean登记. getSingleton(DefaultSingletonBean登记. java: 230)org. springframework. beans. factory. support.AbstractBeanFactory. doGetBean(AbstractBeanFactory. java: 299)org. springframework. beans. factory. support.AbstractBeanFactory. getBean(AbstractBeanFactory. java: 194)org. springframework. beans. factory. support.DefaultListableBeanFactory. preInstantiateSingletons(DefaultListableBeanFactory. java: 755)org. springframework. background. support.AbstractApplication ationContext. finishBeanFactory初始化(AbstractApplication ationContext. java: 757)org. springframework. Context. support.java: 480)org. springframework. web. servlet.FrameworkServlet. configureAndReFresWebApplication ationContext(FrameworkServlet. java: 663)org. springframework. web. servlet.FrameworkServlet. createWebApplication ationContext(FrameworkServlet. java: 629)org. springframework. web. servlet.FrameworkServlet. createWebApplication ationContext(FrameworkServlet. java: 677)org. springframework. web. servlet.FrameworkServlet. initWebApplication ationContext(FrameworkServlet. java: 548)org. springframework. web. servlet.initServletBean(FrameworkServlet. java: 489)org. springframework. web. servlet.init(HttpServletBean. java: 136)javax. servlet.GenericServlet. init(GenericServlet. java: 158)org. apache. catalina.验证器。其一:函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数。ErrorReportValve. invoke(ErrorReportValve. java: 79)org. apache. catalina. valves。AbstractAccessLogValve. invoke(AbstractAccessLogValve. java: 616)org. apache. catalina.连接器。CoyoteAdapter. service(CoyoteAdapter. java: 528)org. apache. coyot. http 11.进程(AbstractHttp11Processor. java: 1099)org. apache. coyote.进程(AbstractAction. java: 672)org. apache. tomcat. util. net。NioEndpoint$SocketProcessor. doRun(NioEndpoint. java: 1520)org. apache. tomcat. util. net。NioEndpoint$SocketProcessor. run(NioEndpoint. java: 1476)java. util.并发。ThreadPoolExecutor. runWorker(ThreadPoolExecutor. java: 1142)java. util. con当前。线程池执行器$Worker. run(ThreadPoolExecutor. java: 617)org. apache. tomcat. util. threads。TaskThread$WrappingRunnable. run(TaskThread. java: 61)java. lang.线程.运行(线程. java: 745)

和我的文件:

网状物xml:

<web-app version="2.4"
     xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>x</display-name>

<servlet>
     <servlet-name>mvc-dispatcher</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
     <servlet-name>mvc-dispatcher</servlet-name>
     <url-pattern>/</url-pattern>
 </servlet-mapping>
</web-app>

mvc-dispatcher-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
                           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

  <context:annotation-config/>

  <context:component-scan base-package="br.inf.datus.ifisio"/>

  <mvc:annotation-driven/>

  <!-- Database properties -->
  <context:property-placeholder location="classpath:application.properties"/>

  <!-- Resource location to load JS, CSS, Images etc -->
  <mvc:resources mapping="/resources/**" location="/resources/"/>

  <!--Prevent browsers from caching contents except the static resources content-->
  <mvc:interceptors>
    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
      <property name="paramName" value="lang"/>
    </bean>
    <mvc:interceptor>
      <mvc:mapping path="/**"/>
      <mvc:exclude-mapping path="/resources/**"/>
      <bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
        <property name="cacheSeconds" value="0"/>
        <property name="useExpiresHeader" value="true"/>
        <property name="useCacheControlHeader" value="true"/>
        <property name="useCacheControlNoStore" value="true"/>
      </bean>
    </mvc:interceptor>
  </mvc:interceptors>

  <!-- View Resolver -->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
  </bean>

  <!-- DataSource -->
  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${db.driverClassName}"/>
    <property name="jdbcUrl" value="${db.url}"/>
    <property name="user" value="${db.username}"/>
    <property name="password" value="${db.password}"/>

    <property name="acquireIncrement" value="${conn.acquireIncrement}"/>
    <property name="minPoolSize" value="${conn.minPoolSize}"/>
    <property name="maxPoolSize" value="${conn.maxPoolSize}"/>
    <property name="maxIdleTime" value="${conn.maxIdleTime}"/>
  </bean>

  <!-- Hibernate SessionFactory -->
  <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">${hibernate.dialect}</prop>
        <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
        <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
        <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
      </props>
    </property>
    <property name="packagesToScan" value="br.inf.datus.ifisio.persistence.entity"/>
  </bean>

  <!-- Transaction -->
  <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
  </bean>

  <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

通用DAOImpl:

    package br.inf.datus.ifisio.persistence.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import br.inf.datus.ifisio.persistence.dao.GenericDao;

@Repository
public abstract class GenericDaoImpl<E, K extends Serializable> implements GenericDao<E, K> {

    @Autowired
    private SessionFactory sessionFactory;

    protected Class<? extends E> daoType;

    @SuppressWarnings("unchecked")
    public GenericDaoImpl() {
        Type t = getClass().getGenericSuperclass();
        ParameterizedType pt = (ParameterizedType) t;
        daoType = (Class) pt.getActualTypeArguments()[0];
    }

    protected Session currentSession() {
        return sessionFactory.getCurrentSession();
    }

    @Override
    public void add(E entity) {
        currentSession().save(entity);
    }

    @Override
    @SuppressWarnings("unchecked")
    public E findById(K key) {
        return (E) currentSession().get(daoType, key);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<E> getAll() {
        return currentSession().createCriteria(daoType).list();
    }

    @Override
    public void update(E entity) {
        currentSession().saveOrUpdate(entity);
    }

    @Override
    public void remove(E entity) {
        currentSession().delete(entity);
    }

    @Override
    public void saveOrUpdate(E entity) {
        currentSession().saveOrUpdate(entity);
    }  
}

GenericServiceImpl:

    package br.inf.datus.ifisio.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import br.inf.datus.ifisio.persistence.dao.GenericDao;

@Service
public abstract class GenericServiceImpl<E, K> implements GenericService<E, K> {

    private GenericDao<E, K> genericDao;

    public GenericServiceImpl(GenericDao<E,K> genericDao) {
        this.genericDao=genericDao;
    }

    public GenericServiceImpl() {

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void add(E entity) {
        genericDao.add(entity);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public E findById(K id) {
        return genericDao.findById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<E> getAll() {
        return genericDao.getAll();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void remove(E entity) {
        genericDao.remove(entity);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveOrUpdate(E entity) {
        genericDao.saveOrUpdate(entity);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void update(E entity) {
        genericDao.update(entity);
    }
}

Tiposanguinodaoimpl:

package br.inf.datus.ifisio.persistence.dao;

import br.inf.datus.ifisio.persistence.entity.TipoSanguineo;
import org.springframework.stereotype.Repository;

@Repository
public class TipoSanguineoDaoImpl extends GenericDaoImpl<TipoSanguineo, Long> implements TipoSanguineoDao {

}

TipoSanguineoServiceImpl:

package br.inf.datus.ifisio.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import br.inf.datus.ifisio.persistence.dao.GenericDao;
import br.inf.datus.ifisio.persistence.dao.TipoSanguineoDao;
import br.inf.datus.ifisio.persistence.entity.TipoSanguineo;

@Service
public class TipoSanguineoServiceImpl extends GenericServiceImpl<TipoSanguineo, Long> implements TipoSanguineoService {

    @Autowired
    private final TipoSanguineoDao tipoSanguineoDao;

    public TipoSanguineoServiceImpl(@Qualifier("tipoSanguineoDaoImpl") GenericDao<TipoSanguineo, Long> genericDao) {
        super(genericDao);
        this.tipoSanguineoDao = (TipoSanguineoDao) genericDao;
    } 
}

共有2个答案

洪安顺
2023-03-14

看起来您正在尝试进行基于构造函数的依赖项注入。。下面TipoSanguineoServiceImpl中的更改应该可以纠正您的问题。

@Autowired
public TipoSanguineoServiceImpl(@Qualifier("tipoSanguineoDaoImpl") GenericDao<TipoSanguineo, Long> genericDao) {
            super(genericDao);
            this.tipoSanguineoDao = (TipoSanguineoDao) genericDao;
        } 
尉迟越
2023-03-14

答案在堆栈跟踪中:

bean实例化失败;嵌套的异常是org。springframework。豆。BeanInstationException:未能实例化[br.inf.datus.ifisio.service.TipoSanguineoServiceImpl]:未找到默认构造函数

您在该类中指定了字段注入(@autowed字段上的注释tipoSanguineoDoug),但您没有提供默认(无参数)构造函数。您似乎想要构造函数注入(事实证明您在构造函数参数上有一个@Qualifer注释),但这与字段注入冲突。

将注释移动到构造函数。

 类似资料: