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
- public class Student {
- private int id;
- private String name;
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- }
Factory class: StuFactory. java
- public class StuFactory implements FactoryBean, InitializingBean {
-
- public void afterPropertiesSet() throws Exception {
- System.out.println("student class initialized.");
- }
-
- public Object getObject() throws Exception {
- return new Student();
- }
-
- public Class getObjectType() {
- return Student.class;
- }
-
- public boolean isSingleton() {
- return true;
- }
- }
Inject to the configuration file:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:lang="http://www.springframework.org/schema/lang"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd">
-
- <bean id="stuFactory" class="com.alibaba.spring.StuFactory">
- </bean>
- </beans>
Test:
- public class StuFactoryTest {
- @Test
- public void test1() throws Throwable {
- ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
- Student stuFactory1 = (Student) context.getBean("stuFactory");
- Student stuFactory2 = (Student) context.getBean("stuFactory");
-
- System.out.println(stuFactory1 == stuFactory2);
- }
- }
Test results:
- student class initialized.
- 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
- Public class FactoryBeanDemo implements FactoryBean {
- Private String name;
-
- Public void setName (String name ){
- This. name = name;
- }
-
- Public Object getObject () throws Exception {
- If ("date". equals (name ))
- Return new Date ();
- Else
- Return new String ("this is a String! ");
- }
-
- Public Class getObjectType (){
- Return "date". equals (name )? Date. class: String. class;
- }
-
- Public boolean isSingleton (){
- Return false;
- }
- }
Configuration file:
- <bean name="myFactoryBean" class="com.alibaba.spring.FactoryBeanDemo" />
-
- <bean name="myFactoryBean1" class="com.alibaba.spring.FactoryBeanDemo">
- <property name="name" value="date"></property>
- </bean>
Test:
- Resource res = new ClassPathResource("applicationContext.xml");
- BeanFactory factory = new XmlBeanFactory(res);
- System.out.println(factory.getBean("myFactoryBean").getClass());
- System.out.println(factory.getBean("myFactoryBean1").getClass());
- System.out.println("=====================================");
- System.out.println(factory.getBean("&myFactoryBean").getClass());
- 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:
- class java.lang.String
- class java.util.Date
- =====================================
- class com.alibaba.spring.FactoryBeanDemo
- 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 ());