Spring Factory-method and Factory-bean use __ssh

Source: Internet
Author: User
Tags string format
Configuring a factory bean is usually created by the application using new, and in order to separate the creation and use of the object, the factory pattern is used, where the application takes the creation and initialization responsibility of the object to the factory object. In general, the application has its own factory object to create the bean. If you give the application's own factory object to spring management, then spring manages not the normal bean, but the factory bean. Calling the Getbean () method, spring returns not an instance of the bean that was created directly, but rather a bean instance created by the factory bean. The factory bean is typically configured in spring, with 3 different types of factory beans configured. 1. Static factory to create a concrete bean instance is a static method
Import Java.util.Random; public class Staticfactorybean {public static Integer Createrandom () {return new integer (New Random (). Nextint ());
}
To be managed by incorporating it into the spring container, you need to specify static method names via Factory-method <bean id= "random"
Class= "Example.chapter3.StaticFactoryBean"
factory-method= "Createrandom"The Createrandom method must be static in order to find
Scope= "Prototype"
/> Test: public static void Main (string[] args) {

Xmlbeanfactory factory = new Xmlbeanfactory (New Classpathresource ("Config.xml"));
System.out.println (Factory.getbean ("random"). ToString ()); Staticfactorybean SFB = (Staticfactorybean) factory.getbean ("random");
System.out.println (Sfb.createrandom (). toString ()); Returns a random number when Getbean () is invoked. If not specified Factory-method,Returns an instance of Staticfactorybean, which returns an instance of the factory bean
}
2. Instance factory to create a concrete bean instance is an instance, not a static method
Import Java.text.SimpleDateFormat;
Import Java.util.Date; public class Instancefactorybean {private String format = ' yy-mm-dd HH:mm:ss '; public void SetFormat (String format) {
This.format = format;
Public String Createtime () {
return new SimpleDateFormat (format). Format (new Date ());
}
The profile needs to be configured with two beans: the first bean is indistinguishable from the normal bean, and the second bean defines how to get the bean through the factory bean, specifying name and method name of the factory bean<bean id= "Instancefactorybean" class= "Example.chapter3.InstanceFactoryBean" >
<property name= "format" value= "Yyyy-mm-dd HH:mm:ss"/>
</bean> <bean id= "CurrentTime"
Factory-bean= "Instancefactorybean"
Factory-method= "Createtime"
/> Test: public static void Main (string[] args) {

Xmlbeanfactory factory = new Xmlbeanfactory (New Classpathresource ("Config.xml"));
System.out.println (Factory.getbean ("currenttime"));

3. Implement Factorybean interface public class Pifactorybean implements Factorybean {public Object getObject () throws Exception {
return new Double (3.14159265358979);
Public Class Getobjecttype () {
return double.class;
public Boolean Issingleton () {
return true;
The bean that implements the Factorybean interface is no longer considered an ordinary bean.spring automatically detected. <bean id= "PI" class= "Example.chapter3.PiFactoryBean"/> test public static void Main (string[) args) throws Exception {

Xmlbeanfactory factory = new Xmlbeanfactory (New Classpathresource ("Config.xml"));
System.out.println (Factory.getbean ("PI"))//Return Pifactorybean factory method getobject a double object instance returned
Pifactorybean p = (pifactorybean) factory.getbean ("&pi"); Plus "&" returns an instance of the factory bean.
System.out.println (P.getobject ());
}

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.