Spring configures Bean through the factory Method

Source: Internet
Author: User

Summary:



Create a Bean by calling the static factory Method

Calling the static factory method to create a Bean encapsulates the object creation process into the static method. When the client needs an object, it simply needs to call the static method without worrying about the details of creating the object. To declare a Bean created using a static method, you must specify the class of the method that owns the factory in the Bean class attribute, and specify the name of the factory method in the factory-method attribute. Finally, use Element to pass the method parameters for this method
By calling the instance factory method to create a Bean instance factory method: encapsulate the object creation process in another object instance method. When the client needs to request an object, you only need to simply call the instance method without worrying about the creation details of the object. You need to declare that the Bean created through the instance factory method should be specified in the factory-bean attribute of the bean that owns the factory method. in the factory-method attribute, specify the name of the factory method and use the construtor-arg element to pass the method parameters for the factory method.

Detailed description of the Instance code: directory structure

Car. java
package com.coslay.beans.factory;public class Car {private String brand;private double price;public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}@Overridepublic String toString() {return "Car [brand=" + brand + ", price=" + price + "]";}public Car() {System.out.println("Car's Constructor...");}public Car(String brand, double price) {super();this.brand = brand;this.price = price;}}


StaticCarFactory. java
Package com. coslay. beans. factory; import java. util. hashMap; import java. util. map;/** static factory method: directly call the static method of a class to return the Bean instance */public class StaticCarFactory {private static Map
  
   
Cars = new HashMap
   
    
(); Static {cars. put ("audi", new Car ("audi", 300000); cars. put ("ford", new Car ("ford", 400000);} // static factory method public static Car getCar (String name) {return cars. get (name );}}
   
  


InstanceCarFactory
Package com. coslay. beans. factory; import java. util. hashMap; import java. util. map;/** instance factory method: the method of the Instance factory, that is, the factory itself needs to be created first, and then the factory instance method is called to return the bean instance */public class InstanceCarFactory {private Map
  
   
Cars = null; public InstanceCarFactory () {cars = new HashMap
   
    
(); Cars. put ("audi", new Car ("audi", 300000); cars. put ("ford", new Car ("ford", 400000);} public Car getCar (String brand) {return cars. get (brand );}}
   
  


Main. java
package com.coslay.beans.factory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-factory.xml");Car car1 = (Car) ctx.getBean("car1");System.out.println(car1);Car car2 = (Car) ctx.getBean("car2");System.out.println(car2);}}


Beans-factory.xml
  
  
   
   
   
    
   
   
   
   
   
   
    
   
  



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.