Instance factory: You must have an instance object of an existing factory and create an object from an instance object. All methods are non-static (this is different from static factory)
(See Code directly)
The difference between a factory (and a static factory is the same as a non-static one)
Package c_inject.c_factory; /* */Publicclasspublic userservice CreateService () { return New Userserviceimpl ();} }
Configuration (configuration and static a bit of a difference take a closer look)
<?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 "><!--creating a factory instance-<bean id= "Mybeanfactoryid"class= "C_inject.c_factory." Mybeanfactory "> <!--Get UserService Factory-Bean: Determine the factory instance factory-method: Determine the methods in the factory--<bean id= "Userserviceid" factory-bean= "Mybeanfactoryid" factory-method= "CreateService" ></bean>& Lt;/beans>
Test class (Static customization and instance customization differ a little bit when using spring exactly the same)
Packagec_inject.c_factory;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportA.ioc.uservice; Public classteststaticfactory {@Test/** Custom Instance Factory*/ Public voiddemo01 () {//Custom Instance Factory//1. Create a factoryMybeanfactory factory=Newmybeanfactory (); //get objects from the factoryUserService uservice=Factory.createservice (); Uservice.adduser (); }; @Test/** Spring's factory*/ Public voiddemo02 () {applicationcontext application=NewClasspathxmlapplicationcontext ("C_inject/c_factory/beans.xml"); UserService UserService= (UserService) application.getbean ("Userserviceid"); Userservice.adduser (); }; }
Spring Learning Note (5) Assembly Bean Instance Factory "resources from the network copyright not himself"