Spring-MVC - Fehler beim erstellen bean mit dem Namen 'productsController': Injektion von autowired Abhängigkeiten ist fehlgeschlagen
Ich habe ein problem mit einer spring-mvc-Projekt. Die @autowiring ist nicht zu schaffen die Bohnen benötigt. Ive arbeitete an diesem für über 4 Tage und alle Folgen die Ergebnisse der Suche. Aber hat nichts geklappt. Kann jemand bitte einen Blick darauf werfen. Danke
Den Fehler-stack wird dies:
org.springframework.Bohnen.factory.BeanCreationException: Fehler beim erstellen bean mit dem Namen 'productsController': Injektion von autowired Abhängigkeiten ist fehlgeschlagen; verschachtelte Ausnahme ist org.springframework.Bohnen.factory.BeanCreationException: Konnte nicht autowire Bereich: privat com.davis.ty.service.ProductsService com.davis.ty.controller.ProductsController.productsService; verschachtelte Ausnahme ist org.springframework.Bohnen.factory.NoSuchBeanDefinitionException: Keine Qualifikation bean des Typs [Kom.davis.ty.service.ProductsService] gefunden für die Abhängigkeit: erwartet mindestens 1 Bohne, die als qualifiziert autowire-candidate für diese Abhängigkeit. Abhängigkeit Anmerkungen: {@org.springframework.Bohnen.factory.Anmerkung.Autowired(required=true)}
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
Meine servlet.xml ist:
<?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>
Mein controller ist:
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";
}
}
Mein service Programm ist:
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);
}
}
Du musst angemeldet sein, um einen Kommentar abzugeben.
Gut, Ihre
ProductsServiceImpl
Klasse nicht implementierenProductsService
... also gibt es keine Möglichkeit, dass bean injiziert, die in einem Feld vom TypProductsService
.Aus meiner Erfahrung, ich habe nur noch
@Repository
an meine DAO, vielleicht können Sie versuchen, es zuWie Frühling autowire funktioniert, Spring-Anwendung Kontext-scans für alle Klassen im Paket und sub-Pakete angegeben, die durch die scan-Komponente und erstellt intern eine Karte von Typ und Namen. Incase-Typ, - Wert Liste der Implementierung von Klassen, und durch seinen Namen ein.
Dann, wenn ein @Autowire wenn Begegnung,
ersten check durch Art, so, wenn Sie autowire unter Verwendung der Schnittstelle prüft alle die Durchführung dieser Schnittstelle, und wenn es nur 1, wenn gefunden, dann injizieren die gleiche. (Bei mehr als 1 gefunden wird, dann müssen Sie qaulify mit dem Qualifier und geben richtigen Namen.
Wenn Sie die oben schlägt fehl, wenn der check von name und injiziert.
Wenn beide schlägt fehl, wenn gibt NoSuchBeanDefinitionException,
So, in Ihrem Fall, müssen Sie legen Sie die component-scan, das ist richtig. Dann, während des autowiring, Sie geben der Interface-Klasse name und die Umsetzung Klasse nicht implementiert das interface. So geben Sie schauen Sie ist nicht, und auch die Namen auch nicht, und daher sind Sie immer NoSuchBeanDefinitionException.
Dieses Problem zu beheben, müssen Sie das tun, was @JB Nizet wird suggeriert, so dass #1 funktioniert und Bohne wird injiziert richtig.
Hinzufügen
implements ProductService
nachpublic class ProductsServiceImpl