Announcement Announcement Module
Collapse
No announcement yet.
Running simple IOC example Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Running simple IOC example

    Hi,
    I am very new to Spring, just started it...
    I had written simple bean called MyBean.java

    public class MyBean {

    private String name = null;

    public MyBean() {
    System.out.println("MyBean obj created");
    }
    public void setName(String name) {
    System.out.println("Setting name = " + name);
    this.name = name;
    }

    public String getName() {
    return name;
    }
    }

    also I had written config file like

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    <bean id="myBean" class="MyBean">
    <property name="name">
    <value>Test Data</value>
    </property>
    </bean>
    </beans>


    But how can I run this example so that MyBean object should create and it displays proper o/p on concel?

    Thanks
    Abhay

  • #2
    Your application needs an entrypoint, that is a main method. From there you have to create an ApplicationContext which allows you to retrieve your bean. Have a look at the reference manual for more informations.

    Regards,
    Andreas

    Comment


    • #3
      As the previous reply mentioned, you need an entry point to run (an application to excercise your bean). The following should work for you:

      public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext(
      "spring.xml");

      MyBean myBean = (MyBean) context.getBean("myBean");

      System.out.println(myBean.getName());
      }

      The xml file that you made is assumed to be named spring.xml and is located in the default package along with your MyBean class. You will need to have the spring framework jar and any others that it requires in your classpath. For example, if you are using the spring-1.1.3.jar you will also need a commons-logging.jar.

      Comment

      Working...
      X