In this tutorial you will see a very simple example of bean creation in spring framework.
In this tutorial you will see a very simple example of bean creation in spring framework.The Spring bean can typically be POJO(Plain Old Java Object) in the IoC container. Here in this tutorial you will see at first a simple java bean file having two methods getMessage() and setMessage() and a default string value.
BasicBean.java
package net.roseindia;
|
The context.xml connects every bean to every other bean that needs it.
Context.xml
<?xml version="1.0" encoding="UTF-8"?>
|
The following class is a standard Java application with a main method.
A BeanFactory will load bean definitions stored in a configuration source
(such as an XML file)and it use the org.springframework.beans package to
configure the beans. The ClassPathXmlApplicationContext takes the context
defination files from the class path.
BasicBeanTest.java
package net.roseindia; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BasicBeanTest { public static void main(String[] args) { BeanFactory beanfactory = new ClassPathXmlApplicationContext("context.xml"); BasicBean bean = (BasicBean) beanfactory.getBean("basic"); System.out.println(bean.getMessage()); } } |
When you run this application it will display output as shown below:
Spring is simple