Three xml-based instantiation methods of beans in Spring
1. JavaBean is a reusable component written in JAVA language. JavaBeans expose member attributes in internal domains by providing common methods that conform to consistent design patterns. Other Java classes can discover and manipulate these JavaBean attributes through their own mechanisms.
2. Types of Bean s
a) Ordinary beans: Instances are created directly by Spring. <bean id=""class="A">
b)FactoryBean: It is a special bean that has the ability of factory to generate objects and can only generate specific objects. The bean must use the FactoryBean interface, which provides the method getObject() for obtaining a specific bean. <bean id=""class="FB">
C) Bean Factory: A factory that generates arbitrary beans.
3. Three Instance Methods of Common Bean s Based on xml
Default construction <beanid="" class=">
(b) Static factories: often used to integrate other frameworks with Spring to generate instance objects, all methods must be static.
<bean id=""class="factory fully qualified class name","factory-method="static method">
(c) Instance factories must have factory instance objects first, and create objects through instance objects. All methods are non-static.
The following are examples, configurations and test classes of static factory method and instance factory method.
1. Static Factory Method
Write UserService interface, UserService Impl implementation class, MyBeanFactory static create instance class, configure bean.xml, and TestStaticFactory test class. Don't forget to guide the bag! ]
/** * */ package com.Lily.SpringLeanring.a_staticFactory; /** * * * @author LilyLee * @date 2017 14 June 2001 * @time 9:10:25 a.m. * @Version 1.0 * @email lilylee_1213@foxmail.com * */ public interface UserService { public void addUser(); }
/** * */ package com.Lily.SpringLeanring.a_staticFactory; /** * * * @author LilyLee * @date 2017 14 June 2001 * @time 9:11:13 a.m. * @Version 1.0 * @email lilylee_1213@foxmail.com * */ public class UserServiceImpl implements UserService { /* (non-Javadoc) * @see com.Lily.SpringLearning.a_staticFacory.UserService#addUser() */ @Override public void addUser() { // TODO Auto-generated method stub System.out.println("Bean Static Factory Method addUser()"); } }
/** * */ package com.Lily.SpringLeanring.a_staticFactory; /** * * * @author LilyLee * @date 2017 14 June 2001 * @time 9:12:09 a.m. * @Version 1.0 * @email lilylee_1213@foxmail.com * */ public class MyBeanFactory { public static UserService createService(){ return new UserServiceImpl(); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="userServiceId" class="com.Lily.SpringLeanring.a_staticFactory.MyBeanFactory" factory-method="createService"></bean> </beans>
/** * */ package com.Lily.SpringLeanring.a_staticFactory; import static org.junit.Assert.*; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * * * @author LilyLee * @date 2017 14 June 2001 * @time 9:22:16 a.m. * @Version 1.0 * @email lilylee_1213@foxmail.com * */ public class TestStaticFactory2 { @Test public void test() { String xmlPath="com/Lily/SpringLeanring/a_staticFactory/beans.xml"; ApplicationContext ac=new ClassPathXmlApplicationContext(xmlPath); UserService u=ac.getBean("userServiceId", UserService.class); u.addUser(); } }
2. Case Factory Method
Write UserService interface, UserService Impl implementation class, MyBeanFactory static create instance class, configure bean.xml, write TestStaticFactory test class.
The difference is that:
(1) The method of creating MyBeanFactory is non-static.
(2) xml first creates the factory instance object, then it can create the object through the instance object.
/** * */ package com.Lily.SpringLeanring.a_staticFactory; /** * * * @author LilyLee * @date 2017 14 June 2001 * @time 9:12:09 a.m. * @Version 1.0 * @email lilylee_1213@foxmail.com * */ public class MyBeanFactory { public UserService createService(){ return new UserServiceImpl(); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="myBeanFactoryId" class="com.Lily.SpringLearning.b_Factory.MyBeanFactory"></bean> <bean id="userServiceId" factory-bean="myBeanFactoryId" factory-method="createService"></bean> </beans>