0

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:

Folder Structure Image

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 !

Community
  • 1
  • 1
Sajha Party
  • 11
  • 1
  • 6

4 Answers4

0

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:

  1. https://stackoverflow.com/a/21525719/2815219
  2. https://stackoverflow.com/a/25782515/2815219
  3. 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>
    
Community
  • 1
  • 1
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
  • I just tried that but ended up in another path error: `.XmlBeanDefinitionStoreException: Line 5 in XML document from class path resource [META-INF/beans.xml] is invalid` – Sajha Party May 11 '17 at 05:10
  • @SajhaParty that solves the answer this question is addressing. Now your spring is actually able to find `beans.xml`. It's another issue that it's not able to parse that XML. – Raman Sahasi May 11 '17 at 05:12
  • @SajhaParty I've updated my answer to solve `XmlBeanDefinitionStoreException XML document ... invalid` exception. – Raman Sahasi May 11 '17 at 05:18
0

Put beans.xml to outside Meta-inf ,

or use new ClassPathXmlApplicationContext("META-INF/beans.xml");

And http://www.springframework.org/schema/beans/spring-beans-4.0.0.RELEASE.xsd

should change to

http://www.springframework.org/schema/beans/spring-beans-4.0.0.xsd , as spring's xsd filenames don't contain "RELEASE".

The xsd files are in org.springframework.beans.factory.xml package in spring-beans.jar, see if the xsd file is in that package.

Mobility
  • 3,117
  • 18
  • 31
0

According to the directory structure you posted it is very likely that src/main/resources is on your classpath. If you like to reference your spring context file beans.xml you have to specify it relative to the folders on your classpath. Hence you should try:

context = new ClassPathXmlApplicationContext("classpath:/META-INF/beans.xml");

Besides: the notation classpath*:beans.xml means you want to read in all context files having a name of beans.xml.

Harmlezz
  • 7,972
  • 27
  • 35
0

Doing following two things solved my issue.

1) There was an incorrect beans.xml path. I changed that to context = new ClassPathXmlApplicationContext("META-INF/beans.xml");.

2) Also, there was an invalid xsi:schemaLocation attribute value. I changed that attribute's value to http://www.springframework.org/schema/beans/spring-beans-3.0.xsd.

Thank you all for your help.

Sajha Party
  • 11
  • 1
  • 6