Tutorials

Spring IOC Container


Bean Factories


Bean factory is a Spring Container.

 

Java POJO

public class SampleExample {

  public void printTest(){
   System.out.println("Testing Bean factory");
  }
}

XML configuration

  

<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
 
<bean id="SampleExampleBean" class="SampleExample"></bean
</beans>

	
Test class
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

   public class TestSpringClass {

     public static void main(String[] args) {
       Resource resource = new ClassPathResource("applicationContext.xml");
       BeanFactory factory = new XmlBeanFactory(resource);

       SampleExample example = (SampleExample) factory.getBean("SampleExampleBean");
       example.printTest();
     }
}

	
OUTPUT:
Testing Bean factory


Please Share this page
Views : 87
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!