spring FactoryBean配置Bean
概要:
实例代码具体解释:
文件夹结构
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;
}
@Override
public 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; //自己定义的FactoryBean须要实现 FactoryBean接口
public class CarFactoryBean implements FactoryBean<Car>{ private String brand; public void setBrand(String brand){
this.brand = brand;
} //返回bean的对象
@Override
public Car getObject() throws Exception {
return new Car("BMW",5000000);
} //返回bean的类型
@Override
public Class<?> getObjectType() {
return Car.class;
} @Override
public 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
<? xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
通过FactoryBean来配置Bean的实例
class:指向FactoryBean的全类名
property:配置FactoryBean的属性 但实际返回的实例确是FactoryBean的getObject()方法返回的实例
-->
<bean id="car" class="com.coslay.beans.factorybean.CarFactoryBean">
<property name="brand" value="BMW"></property>
</bean> </beans>
spring FactoryBean配置Bean的更多相关文章
- Spring学习记录(十)---使用FactoryBean配置Bean
之前学了,配置bean可以用普通全类名配置.用工厂方法配置,FactoryBean又是什么呢 有时候配置bean要用到,IOC其他Bean,这时,用FactoryBean配置最合适. FactoryB ...
- Spring工厂方法(factory-bean)配置bean
在spring的世界中, 我们通常会利用bean config file 或者 annotation注解方式来配置bean. 在第一种利用bean config file(spring xml)方式中 ...
- Spring(十四):使用FactoryBean配置Bean
FactoryBean简介: 1)Spring中Bean包含两种一种是普通Bean,另外一种是FactoryBean.它们都受IOC容器管理,但是也有不同之处. 2)普通Bean与FactoryBea ...
- Spring 通过FactoryBean配置Bean
1.实现FactoryBean接口 import org.springframework.beans.factory.FactoryBean; public class CarFactoryBean ...
- 12.Spring通过FactoryBean配置Bean
为啥要使用FactoryBean: 在配置Bean的时候,需要用到IOC容器中的其它Bean,这个时候使用FactoryBean配置最合适. public class Car { private St ...
- Spring4.0学习笔记(7) —— 通过FactoryBean配置Bean
1.实现Spring 提供的FactoryBean接口 package com.spring.facoryBean; import org.springframework.beans.factory. ...
- 通过FactoryBean配置Bean
这是配置Bean的第三种方式,FactoryBean是Spring为我们提供的,我们先来看看源码: 第一个方法:public abstract T getObject() throws Excepti ...
- spring4-2-bean配置-10-通过FactoryBean配置bean
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAk8AAAFHCAIAAAA3Hj/JAAAgAElEQVR4nO2dzdX0rA2Gp6asclwQTW
- Spring_通过 FactoryBean 配置 Bean
beans-factorybean.xml <?xml version="1.0" encoding="UTF-8"?><beans xmln ...
随机推荐
- matlab直方图均衡
clear all; i=imread('cameraman.tif'); figure; subplot(2,2,1); imshow(i); title('原始图像'); subplot(2,2, ...
- 解决maven构建webapp index.jsp报错问题
今天早上想用maven 构建一个webapp 然后index.jsp华华丽丽的报错了 当时我的心情是一万头草泥马奔过啊,为啥你给我创建的webapp 还会报错啊!!!!!! 然后百度了一下,各种说少 ...
- 编辑控件的警告提示是:This text field does not specify an inputType or a hint
没有设置editText的inputtype属性,比如<android:inputtype="textpassword"> http://binuu.blog.51ct ...
- C语言 函数参数不确定时 需要用到va_start和va_end函数
1.在C中,当我们无法列出传递函数的所有实参的类型和数目时,可以用省略号指定参数表 void foo(...);void foo(parm_list,...); 这种方式和我们以前认识的不大一样,但我 ...
- vim+cscope简易教程
Cscope具有纯正的Unix血统,它最早是由贝尔实验室为PDP-11计算机开发的,后来成为商用的AT&T Unix发行版的组成部分.直到2000年4月,这个工具才由SCO公司以BSD lic ...
- Mac升级bash到最新版本
mac自带的bash为3.2版本,而最新的bash是4.9,需要升级了,才能支持关联数组等新特性. 1.brew install bash 2.安装到/usr/local/bin/bash里面.可以通 ...
- [na]acl拒绝访问流量审计
acl审计拒绝的流量及拒绝提示 interface Ethernet0/0 ip address 12.1.1.2 255.255.255.0 ip access-group 10 in half-d ...
- 基于jquery响应式网站图片无限加载瀑布流布局
分享一款效果非常酷的jQuery瀑布流布局无限加载图片效果.整个页面采用响应式布局,图片采用jQuery.Lazyload延时加载技术,提升整个页面的加载速度.效果图如下: 在线预览 源码下载 实 ...
- (function($){...})(jQuery)是什么意思?
本文转自:http://blog.csdn.net/rambo_china/article/details/7742321 最近在工作中看到这调用定义方法,并且同时调用的方式,觉得很疑惑,看到这篇博客 ...
- xml与dataset的数据交互
1. this.dsConfig = new DataSet(); //使用指定的文件将 XML 架构和数据读入 System.Data.DataSet. this.dsConfig.ReadXml( ...