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
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.