Spring Learning Note (5) Assembly Bean Instance Factory "resources from the network copyright not himself"

Source: Internet
Author: User

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"

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.