我对Spring Framework非常陌生。 我正在使用NetBeans for IDE。 我跟着几个教程来自己学习。 但是,我陷入中间,无法继续前进。 让我在这里分解我的项目:
我的项目文件夹结构如下所示:
有两类; 主要的MainApp.java包含以下代码:
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(); } }第二个类文件FirstPage.java看起来像这样:
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); } }beans.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" 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>现在,我得到的错误如下所示:
org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为'firstPage'的bean
我知道我在这里犯了一个愚蠢的错误。 先谢谢你 !
I am very new to Spring Framework. I am using NetBeans for IDE. I followed couple of tutorials to learn it by myself. However, I am stuck in the middle and cannot proceed further. Let me breakdown my project here:
My project folder structure looks like this:
There are two classes; the major one MainApp.java contains following 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(); } }Second class file FirstPage.java looks like this:
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); } }The beans.xml file looks like below:
<?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>Now, the error I am getting is like below:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'firstPage' is defined
I know I have been doing some silly mistake here. Thank you in Advance !
最满意答案
以前讨论过几乎类似的问题。 我认为你的程序无法找到beans.xml 。
尝试这样做:
context = new ClassPathXmlApplicationContext("META-INF/beans.xml");编辑:
这个新的错误XmlBeanDefinitionStoreException意味着你的模式无效。 按照以下答案之一所述尝试更改架构:
https://stackoverflow.com/a/21525719/2815219 https://stackoverflow.com/a/25782515/2815219Spring配置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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <bean id = "firstPage" class = "com.myprojects.spring.FirstPage"> <property name = "message" value = "Hello World!"/> </bean> </beans>Almost similar problem have been discussed before. I think your program is unable to locate beans.xml.
Try doing this:
context = new ClassPathXmlApplicationContext("META-INF/beans.xml");EDIT:
This new error XmlBeanDefinitionStoreException means that your schema is not valid. Try changing your schema as described in one of these answers:
https://stackoverflow.com/a/21525719/2815219 https://stackoverflow.com/a/25782515/2815219Spring configuration XML schema: with or without version?
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <bean id = "firstPage" class = "com.myprojects.spring.FirstPage"> <property name = "message" value = "Hello World!"/> </bean> </beans>
更多推荐
发布评论