本文共 1509 字,大约阅读时间需要 5 分钟。
下面是FactoryBean接口中的方法
演示: Car_FactoryBean.javapackage com.atguigu.spring.bean;import org.springframework.beans.factory.FactoryBean;//自定义的FactoryBean,需要实现FactoryBean的接口public class Car_FactoryBean implements FactoryBean{ private String brand; public void setBrand(String brand) { this.brand=brand; } //返回bean的对象 @Override public Car getObject() { return new Car(brand, 80000); } //返回bean的类型 @Override public Class getObjectType(){ return null; } @Override public boolean isSingleton() { return false; }}
Car.java
package com.atguigu.spring.bean;public class Car { private String brand; private double price; public Car(String brand, double price) { super(); this.brand = brand; this.price = price; } @Override public String toString() { return "Car [brand=" + brand + ", price=" + price + "]"; }}
application.xml
Main.java
package com.atguigu.spring.bean;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main { public static void main(String[] args) { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml"); Car car1=(Car) applicationContext.getBean("car1"); System.out.println(car1); }}
转载地址:http://qrozi.baihongyu.com/