创建名称为'vendorController'的bean时出错:通过字段'vensorinterface'表示的不满意的依赖关系;

时间:2019-12-11 14:21:17

标签: spring dependency-injection controller autowired

Error creating bean with name 'vendorController': Unsatisfied dependency expressed through field 'vensorinterface'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'vendorSerInterfaceImplement': Unsatisfied dependency expressed through field 'vendordaointerface'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.WeddinG.Vendor.Dao.VendorDaoInterface' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    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:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4682)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5150)

这是我的控制人:

@RestController
@RequestMapping(value="vendors")
public class VendorController {


    @Autowired(required=true)
    private VendorServiceInterface vensorinterface;

    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    @RequestMapping(value = "/vendor/add", method = RequestMethod.POST)
    public ModelAndView registreVendor(@RequestBody Vendor vendor)
    {
    Vendor v=vendor;
        log.info("vendor controller called"+vendor.getBussinessName());
    vendor= vensorinterface.registerVendor(vendor);
    return new ModelAndView("hello","vendor",v);
    }

这是VendorServiceInterface:

public interface VendorServiceInterface {

    Vendor registerVendor(Vendor vendor);

}

这是实现类:

@Service
public class VendorSerInterfaceImplement implements VendorServiceInterface {

    @Autowired(required=true)
    private VendorDaoInterface vendordaointerface;

    public Vendor registerVendor(Vendor vendor) {

        return vendordaointerface.registerVendor(vendor);
    }

这是dao接口和实现类:

public interface VendorDaoInterface {

    Vendor registerVendor(Vendor vendor);

}

这是dao的实现:

@Transactional
public class VendorIntefaceImplement implements VendorDaoInterface {

    @Autowired
    private HibernateTemplate hibernatetemplate;


    public Vendor registerVendor(Vendor vendor) {
    Session session=hibernatetemplate.getSessionFactory().getCurrentSession();
    session.beginTransaction();

    session.save(vendor);
    System.out.println("VEndor saved");
    session.close();


        return vendor;
    }

这是db-config:

<?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:tx="http://www.springframework.org/schema/tx"
    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/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="classpath:database.properties"/> 
    <bean id="basicDataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${database.driverClassName}" />
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.username}" />
        <property name="password" value="${database.password}" />
    </bean>
    <bean id="localSessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="basicDataSource"/>
        <property name="hibernateProperties">
            <props>
               <prop key="hibernate.dialect">${hibernate.dialect}</prop>
               <prop key="hibernate.id.new_generator_mappings">${hibernate.id.new_generator_mappings}</prop>
               <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
               <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>  
        <prop key="hibernate.hbm2ddl.auto">create-drop</prop>          
            </props>
        </property>
        <property name="packagesToScan" value="com.WeddinG.Models"/> 
    </bean>
    <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="localSessionFactory" />
    </bean>
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
        <constructor-arg name="sessionFactory" ref="localSessionFactory"/>  
    </bean> 
    <!-- <bean class="com.concretepage.dao.ArticleDAO" /> -->
    <!--  <bean class="com.WeddinG.Vendor.Dao.VendorIntefaceImplement" /> -->


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

这是servlet-dispatcher.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.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:component-scan base-package="com.WeddinG" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/views/"/>
      <property name="suffix" value=".jsp"/> 
    </bean>
     <mvc:resources location="/resources/" mapping="/resources/**" />
        <bean name="jackson2ObjectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
        <property name="indentOutput" value="true"/>
    </bean>    
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper" ref="jackson2ObjectMapper" />
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
</beans>

这是web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
           /WEB-INF/dispatcher-servlet.xml
         <!--   /WEB-INF/security-config.xml -->
           /WEB-INF/db-config.xml
        </param-value>          
    </context-param>    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    </web-app>

2 个答案:

答案 0 :(得分:0)

No qualifying bean of type 'com.WeddinG.Vendor.Dao.VendorDaoInterface'

您指定了要Spring的容器将VendorIntefaceImplement提取为bean的任何位置。

实际上是

@Transactional
public class VendorIntefaceImplement implements VendorDaoInterface {
   ...
}

不定义bean,但是可以:

@Service
@Transactional
public class VendorIntefaceImplement implements VendorDaoInterface {
   ...
}

答案 1 :(得分:0)

如果您使用的是 Open-Feign,请禁用 Netflix-Ribbon 将解决问题:

spring:
  cloud:
      loadbalancer:
        ribbon:
          enabled: false