NoSuchBeanDefinitionException:没有定义名为'FirstPage'的bean(NoSuchBeanDefinitionException: No bea

编程入门 行业动态 更新时间:2024-10-25 07:20:28
NoSuchBeanDefinitionException:没有定义名为'FirstPage'的bean(NoSuchBeanDefinitionException: No bean named 'FirstPage' is defined)

我对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/2815219

Spring配置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/2815219

Spring 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>
 

                    
                     
          

更多推荐

本文发布于:2023-08-07 18:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466013.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定义   FirstPage   NoSuchBeanDefinitionException   defined   named

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!