Ich bin sehr neu in Spring Framework. Ich verwende NetBeans für IDE. Ich folgte ein paar Tutorials, um es selbst zu lernen. Ich stecke jedoch in der Mitte fest und kann nicht weitermachen. Lassen Sie mich Zusammenbruch mein Projekt hier:NoSuchBeanDefinitionException: Keine Bean namens 'FirstPage' ist definiert
Mein Projektordner Struktur sieht wie folgt aus:
Es gibt zwei Klassen; die große Gefahr, MainApp.java enthält folgenden Code:
package com.myprojects.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context;
context = new ClassPathXmlApplicationContext("classpath*:beans.xml");
FirstPage obj;
obj = (FirstPage) context.getBean("firstPage");
obj.getMessage();
}
}
Zweite Klasse Datei FirstPage.java wie folgt aussieht:
package com.myprojects.spring;
public class FirstPage {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
Die beans.xml Datei sieht wie folgt:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-4.0.0.RELEASE.xsd
">
<bean id = "firstPage" class = "com.myprojects.spring.FirstPage">
<property name = "message" value = "Hello World!"/>
</bean>
</beans>
Jetzt die Fehler Ich erhalte ist wie unten:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'firstPage' is defined
Ich weiß, ich habe hier einige dumme Fehler gemacht haben. Vielen Dank im Voraus!
Ich versuchte nur, dass aber endete in einem anderen Pfad Fehler auf: '.XmlBeanDefinitionStoreException: Linie 5 in XML-Dokument aus Pfad Ressourcenklasse [META-INF/beans.xml] ist invalid' –
@SajhaParty, dass die Antwort auf diese Frage löst spricht an. Jetzt kann Ihr Frühling tatsächlich 'beans.xml' finden. Es ist ein anderes Problem, dass es dieses XML nicht parsen kann. –
@SajhaParty Ich habe meine Antwort aktualisiert, um 'XmlBeanDefinitionStoreException XML document ... invalid' Ausnahme zu lösen. –