Summary:
Instance code details: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + xL/CvL3hubk8L3A + CjxwPjxpbWcgc3JjPQ = "http://www.2cto.com/uploadfile/Collfiles/20140607/20140607091120354.png" alt = "\">
Car. java
package com.coslay.beans.factorybean;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;}}
CarFactoryBean. java
Package com. coslay. beans. factorybean; import org. springframework. beans. factory. FactoryBean; // you need to implement the FactoryBean interface public class CarFactoryBean implements FactoryBean
{Private String brand; public void setBrand (String brand) {this. brand = brand ;}// return the bean object @ Overridepublic Car getObject () throws Exception {return new Car ("BMW", 5000000 );} // return the bean type @ Overridepublic Class
GetObjectType () {return Car. class ;}@ Overridepublic boolean isSingleton () {return true ;}}
Main. java
package com.coslay.beans.factorybean;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-beanfactory.xml");Car car = (Car) ctx.getBean("car");System.out.println(car);}}
Beans-beanfactory.xml