Hallo, ich bin neu im Frühling und ich benutze Spring Base Annotation in meinem einfachen Projekt, das ich versuche mit Stero Type Annotation zu erstellen ein Blick von meinem ProjektKeine eindeutige Bean vom Typ [com.Car] definiert: erwartete einzelne Bean, aber gefunden 0:
Engine.java
package com;
public class Engine {
private String engname;
public Engine() {
// TODO Auto-generated constructor stub
System.out.println("Engine Object Created");
}
public String getEngname() {
return engname;
}
public void setEngname(String engname) {
this.engname = engname;
}
}
Car.java
package com;
import org.springframework.beans.factory.annotation.Autowired;
public class Car {
@Autowired
private Engine engine;
public void printEngineData() {
System.out.println("Engine Name"+engine.getEngname());
}
}
applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com"/>
<context:annotation-config/>
<bean class="com.Engine">
<property name="engname" value="Audi Engine"></property>
</bean>
</beans>
und mein Test-Klasse ist wie Client.java aussehen Paket com;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
ApplicationContext ap=new ClassPathXmlApplicationContext("applicationContext.xml");
Car c = (Car)ap.getBean(Car.class);
c.printEngineData();
}
}
ich die Client.java Klasse leite, führt er den Motor Klasse Constructor und druckt Motor Objekt erstellt nach, dass es mir wirft Ausnahme org.springframework.beans.factory.NoSuchBeanDefinitionException.
Ich verwende spring3.2.0 für dieses Projekt.
Mögliches Duplikat von https://StackOverflow.com/Questions/46487567/No-Qualifying-Bean-Of-Type-Available – Afridi
ok, aber diese Antwort funktioniert nicht für mich können Sie bitte überprüfen, wo ich Fehler mache –
Erstellen Sie ein Bean für 'Car' Klasse auch in Ihrer XML-Konfiguration – Afridi