码迷,mamicode.com
首页 > 编程语言 > 详细

Spring使用教程(二)配置factorybean

时间:2015-06-01 22:10:06      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
/**
 * 自定义的factorybean需要实现spring提供的fenctorybean接口
 * */
public class CarFactoryBean implements FactoryBean<Car>{
    //返回bean对象
    public Car getObject() throws Exception {
        // TODO Auto-generated method stub
        return new Car("BENCHI", 500000);
    }
    //返回bean类型
    public Class<?> getObjectType() {
        // TODO Auto-generated method stub
        return Car.class;
    }
    //是否单实例
    public boolean isSingleton() {
        // TODO Auto-generated method stub
        return true;
    }

}
View Code
技术分享
public class Car {
    private String brand;
    private double price;
    get...
        set...
    
}
View Code
技术分享
public class Main {
    public static void main(String[] args) {
        ApplicationContext con= new ClassPathXmlApplicationContext("car_factorybean.xml");
        Car car=(Car) con.getBean("car");
        System.out.println(car);
    }
}
View Code
技术分享
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:util="http://www.springframework.org/schema/util"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
        <!-- 实际返回的是getObject方法 -->
        <bean id="car" class="com.test.spring.factorybean1.CarFactoryBean">
            
        </bean>            
</beans>
View Code

 

Spring使用教程(二)配置factorybean

标签:

原文地址:http://www.cnblogs.com/img-zoom/p/4545039.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!