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 ...
随机推荐
- git使用(一)----git安装
windows安装git msysgit是windows版本的Git 下载地址:https://git-for-windows.github.io/ 安装步骤 linux安装git https://g ...
- Synplify9.6.2破解(转帖)
Synplify9.6.2破解(转帖) 转载自:http://www.cnblogs.com/mark-sun/archive/2012/02/26/2368773.html Abstract本文 ...
- jquery.roundabout.js图片叠加3D旋转
Roundabout是一个jQuery插件能够将一组静态HTML元素转换成可以灵活定制具有类似于转盘旋转效果的交互区域.有多种旋转形状可供选择.官方说明:roundabout.js是一个jQuery插 ...
- ccTouchBegan
CCLayer中的setTouchEnabled(true)会开启多点触摸. 如果使用CCDirector::sharedDirector()->getTouchDispatcher()-> ...
- MAC下cmake安装
可以参考网上手动下载cmake的源码包进行安装,http://www.cmake.org/download/ 解压后运行sudo ./bootstrap && sudo make &a ...
- 每日英语:Tech Firms Flock to Vietnam
Opening up a Korean restaurant among the rice fields and limestone karsts north of Hanoi might seem ...
- Spring 是如何解决并发访问的线程安全性问题的
springmvc的controller是singleton的(非线程安全的),这也许就是他和struts2的区别吧!和Struts一样,Spring的Controller默认是Singleton的, ...
- 使用Eclipse创建的第一个javabean,cannot resolved to a type
第一个原因是我没有把eclipse编译好的class文件放在项目的build文件夹中的(我创建的是dynamic web project),你要把/build/classes/文件夹给合并到WebCo ...
- VBA学习笔记(7)--vba的数组函数
说明(2017.3.25): 1. split(str,"-")和join(arr,",")函数,用法跟其他语言差不多. 2. filter函数,filter( ...
- calloc内存分配函数
calloc是一个C语言函数 函数名: calloc void *calloc(unsigned n,unsigned size): 功 能: 在内存的动态存储区中分配n个长度为size的连续空间,函 ...