2015-06-19 49 views
1

我的spring-mvc项目存在问题。 @autowiring不会创建所需的bean。我已经为此工作了4天以上,并且关注了所有搜索结果。但没有任何工作。有人可以请看看。谢谢Spring-MVC-创建名为'productsController'的bean时出错:自动布线依赖关系注入失败

错误堆栈是这样的:

org.springframework.beans.factory.BeanCreationException:错误创建名为“ProductsController的”豆:自动装配依赖注入失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装入字段:private com.davis.ty.service.ProductsService com.davis.ty.controller.ProductsController.productsService;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到符合要求的[com.davis.ty.service.ProductsService]类型的合格bean:期望至少1个符合此依赖关系自动装配候选资格的bean。依赖注解:{@ org.springframework.beans.factory.annotation.Autowired(必需=真)} org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)

我的servlet。 XML是:

<?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:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 


    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 
    <context:annotation-config /> 
    <context:component-scan base-package="com.davis.ty" /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
    <mvc:resources mapping="/resources/**" location="/resources/" /> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/views/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

    <bean id="propertyConfigurer" 
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
     p:location="/WEB-INF/jdbc.properties" /> 

    <bean id="messageSource" 
     class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
     <property name="basename" value="classpath:messages" /> 
     <property name="defaultEncoding" value="UTF-8" /> 
    </bean> 

    <bean id="dataSource" 
     class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close" 
     p:driverClassName="${jdbc.driverClassName}" 
     p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" 
     p:password="${jdbc.password}" /> 


    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="configLocation"> 
      <value>classpath:hibernate.cfg.xml</value> 
     </property> 
     <property name="configurationClass"> 
      <value>org.hibernate.cfg.AnnotationConfiguration</value> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${jdbc.dialect}</prop> 
       <prop key="hibernate.show_sql">true</prop> 
      </props> 
     </property> 
    </bean> 

    <tx:annotation-driven /> 
    <bean id="transactionManager" 
     class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 

</beans> 

我的控制器:

package com.davis.ty.controller; 

import java.util.Map; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.validation.BindingResult; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

import com.davis.ty.domain.Products; 
import com.davis.ty.service.ProductsService; 

@Controller 
public class ProductsController { 

    @Autowired 
    private ProductsService productsService; 

    @RequestMapping(value = "/index", method = RequestMethod.GET) 
    public String listProducts (Map<String, Object> map) { 

     System.out.println("index"); 

      map.put("products", new Products()); 
      map.put("productsList", productsService.listProducts()); 

      return "index"; 
    }  


    @RequestMapping(value = "/add", method = RequestMethod.POST) 
    public String addProducts(@ModelAttribute("products") 
     Products products, BindingResult result) { 

      productsService.addProducts(products); 

      return "redirect:/index"; 
     } 

    @RequestMapping("/delete/{Id}") 
    public String deleteProducts(@PathVariable("Id") 
     Integer Id) { 

      productsService.removeProducts(Id); 

      return "redirect:/index"; 
     } 
}  

我的服务程序是:

package com.davis.ty.service; 

import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Transactional; 

import com.davis.ty.dao.ProductsDAO; 
import com.davis.ty.domain.Products; 

@Service 
public class ProductsServiceImpl { 

    @Autowired 
    private ProductsDAO productsDAO; 

    @Transactional 
    public void addProducts(Products products){ 

     productsDAO.addProduct(products); 
    } 

    @Transactional 
    public List<Products> listProducts() { 

     return productsDAO.listProducts(); 

    } 

    @Transactional 
    public void removeProducts(Integer id) { 

     productsDAO.removeProducts(id); 

    } 
} 

回答

0

那么,你的ProductsServiceImpl类没有实现ProductsService ...所以没有办法将这个bean注入到ProductsService类型的字段中。

+0

非常感谢您的回复。 –

+0

我不相信这个,但我得到完全相同的错误。但我无法获得发布或清理服务器工作区的选项。我想这可能与此有关。我正在努力。当我进行更改以实现界面时,我的项目已经失灵。 Eclipse甚至不会将它们视为服务器,就像另一个项目一样 –

0

从我的经验,我只需添加@Repository在我的DAO,也许你可以尝试使用它太

@Repository 
public class ProductsDAO 
{ 
} 
0

弹簧自动装配的工作原理是,Spring应用程序上下文扫描包和子里面所有的类软件包,由组件扫描指定,并按类型和名称在内部创建映射。类型的Incase,值可以是实现类的列表,并且可以是其名称。

然后每当@Autowire若遇,

  • 首先,通过类型检查,因此,如果您使用的自动装配界面,它会检查该接口的所有实现,而如果只有1,如果发现再注入一样。 (如果多于1被发现,那么你就需要使用限定符qaulify,并给予适当的名称。

  • 如果上面的失败,如果检查通过名称,并注入。

如果两个失败,如果给出NoSuchBeanDefinitionException,

所以在你的情况下,你已经设置了组件扫描,这是正确的,然后在自动装配时,你给的接口类的名称和实现类没有实现接口,所以类型检查失败,也按名称也是失败的,因此你得到NoSuchBeanDefinitionException。

为了解决这个问题,你需要做一下@JB Nizet的建议,这样#1才能正常工作,并且bean被正确注入。

相关问题