FactoryBean and InitializingBean

Source: Internet
Author: User

 

Spring has two types of beans: common Bean and factory Bean, namely FactoryBean. The factory Bean is different from the common Bean. The returned object is not an instance of the specified class, and the returned object is the object returned by the getObject method of the factory Bean.

Use Cases: 1. Check whether the class is a singleton by using external controls. 2. initialize the class before it is created, which is completed in afterPropertiesSet.

Instance 1:

Basic Class: Student. java

 
  1. public class Student { 
  2.     private int    id; 
  3.     private String name; 
  4.  
  5.     public int getId() { 
  6.         return id; 
  7.     } 
  8.  
  9.     public void setId(int id) { 
  10.         this.id = id; 
  11.     } 
  12.  
  13.     public String getName() { 
  14.         return name; 
  15.     } 
  16.  
  17.     public void setName(String name) { 
  18.         this.name = name; 
  19.     } 
  20.  
  21. } 

Factory class: StuFactory. java

 
  1. public class StuFactory implements FactoryBean, InitializingBean { 
  2.  
  3.     public void afterPropertiesSet() throws Exception { 
  4.         System.out.println("student class initialized."); 
  5.     } 
  6.  
  7.     public Object getObject() throws Exception { 
  8.         return new Student(); 
  9.     } 
  10.  
  11.     public Class getObjectType() { 
  12.         return Student.class; 
  13.     } 
  14.  
  15.     public boolean isSingleton() { 
  16.         return true; 
  17.     } 
  18. } 

Inject to the configuration file:

 
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lang="http://www.springframework.org/schema/lang" 
  4.     xsi:schemaLocation=" 
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
  6. http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd"> 
  7.  
  8.     <bean id="stuFactory" class="com.alibaba.spring.StuFactory"> 
  9.     </bean> 
  10. </beans> 

Test:

 
  1. public class StuFactoryTest { 
  2.     @Test 
  3.     public void test1() throws Throwable { 
  4.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 
  5.         Student stuFactory1 = (Student) context.getBean("stuFactory"); 
  6.         Student stuFactory2 = (Student) context.getBean("stuFactory"); 
  7.  
  8.         System.out.println(stuFactory1 == stuFactory2); 
  9.     } 
  10. } 

Test results:

 
  1. student class initialized. 
  2. true 

Note]

1. The returned type is described in getObject (), instead of the type used to create a factory. It is different from the normal javabean.

2. Whether the created object belongs to the singleton instance is determined by the return value in isSingleton.

Example 2:

FactoryBeanDemo. java

 
  1. Public class FactoryBeanDemo implements FactoryBean {
  2. Private String name;
  3.  
  4. Public void setName (String name ){
  5. This. name = name;
  6. }
  7.  
  8. Public Object getObject () throws Exception {
  9. If ("date". equals (name ))
  10. Return new Date ();
  11. Else
  12. Return new String ("this is a String! ");
  13. }
  14.  
  15. Public Class getObjectType (){
  16. Return "date". equals (name )? Date. class: String. class;
  17. }
  18.  
  19. Public boolean isSingleton (){
  20. Return false;
  21. }
  22. }

Configuration file:

 
  1. <bean name="myFactoryBean" class="com.alibaba.spring.FactoryBeanDemo" /> 
  2.  
  3. <bean name="myFactoryBean1" class="com.alibaba.spring.FactoryBeanDemo"> 
  4.     <property name="name" value="date"></property> 
  5. </bean> 

Test:

 
  1. Resource res = new ClassPathResource("applicationContext.xml"); 
  2.       BeanFactory factory = new XmlBeanFactory(res); 
  3.       System.out.println(factory.getBean("myFactoryBean").getClass()); 
  4.       System.out.println(factory.getBean("myFactoryBean1").getClass()); 
  5.       System.out.println("====================================="); 
  6.       System.out.println(factory.getBean("&myFactoryBean").getClass()); 
  7.       System.out.println(factory.getBean("&myFactoryBean1").getClass()); 

We can see that we can get different types of object instances by using FactoryBean. This means that in AOP, by setting calss as ProxyFactoryBean, we can return the principles of different types of business objects. In practical applications, if FactoryBean can be used flexibly, many magic functions can be added to the application.

Test results:

 
  1. class java.lang.String 
  2. class java.util.Date 
  3. ===================================== 
  4. class com.alibaba.spring.FactoryBeanDemo 
  5. class com.alibaba.spring.FactoryBeanDemo 

TIPS:
FactoryBean also contains a trick, that is, what to do when we actually need to return the FactoryBean instance. That is, we need to get a FactoryBean through the container in the application. In this case, you need to add a special symbol "&" before the bean name. That is, System. out. println (factory. getBean ("& myFactoryBean"). getClass ());


 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.