16.笔记JAVA Spring框架学习————通过FactoryBean配置Bean
来源:互联网 发布:淘宝新店能开通直播吗 编辑:程序博客网 时间:2024/05/16 01:27
16.笔记JAVA Spring框架学习————通过FactoryBean配置Bean
• Spring 中有两种类型的 Bean, 一种是普通Bean, 另一种是工厂Bean, 即FactoryBean.
• 工厂 Bean 跟普通Bean不同, 其返回的对象不是指定类的一个实例, 其返回的是该工厂 Bean 的 getObject 方法所返回的对象
通过FactoryBean来配置Bean的示例,class指向F
创建一个类CarFactoryBean实现接口FactoryBean的全类目,property配置FactoryBean的属性。
import org.springframework.beans.factory.FactoryBean;
publicclass CarFactoryBeanimplements FactoryBean<Car>{
private Stringbrand;
publicvoidsetBrand(Stringbrand) {
this.brand = brand;
}
@Override
public Car getObject()throws Exception {
//TODO Auto-generated method stub
returnnew Car(brand,40000);
}
@Override
publicClass<?> getObjectType() {
//TODO Auto-generated method stub
return Car.class;
}
@Override
publicbooleanisSingleton(){
//TODO Auto-generated method stub
returntrue;
}
}
配置app.xml内容如下
<beanid="carFactory"class="CarFactoryBean">
<propertyname="brand"value="bmw"></property>
</bean>
然后配置main.java如下
import java.sql.SQLException;
import javax.sql.DataSource;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.AbstractApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
import com.mchange.v2.c3p0.DataSources;
public class Main {
publicstatic void main(String[] args) throws SQLException {
//1.创建 Spring的 IOC容器
ClassPathXmlApplicationContextapx = new ClassPathXmlApplicationContext("app.xml");
Carcar = (Car) apx.getBean("carFactory");
System.out.println(car);
}
}
指向如下:
Car[company=bmw, brand=null, maxSpeed=0, price=40000.0]
- 16.笔记JAVA Spring框架学习————通过FactoryBean配置Bean
- 15.笔记JAVA Spring框架学习————通过工厂配置Bean
- spring学习笔记十 通过factoryBean配置bean
- Spring学习笔记之通过FactoryBean配置Bean
- spring学习笔记(11)——使用FactoryBean创建bean
- Spring框架学习(9):通过FactoryBean的方法配置bean
- 12.Spring学习笔记_通过FactoryBean配置Bean(by尚硅谷_佟刚)
- 17.笔记JAVA Spring框架学习————基于注解配置Bean之一
- Spring学习笔记——Spring中的BeanFactory与FactoryBean
- Spring-11:通过FactoryBean配置Bean
- Spring中通过FactoryBean来配置bean
- Spring 通过FactoryBean来配置bean
- Spring入门学习——使用Spring的FactoryBean创建Bean
- spring学习笔记——spring框架bean装配流水线
- spring 10 bean配置-通过FactoryBean配置bean
- Spring学习第十二天:FactoryBean配置bean
- spring FactoryBean配置Bean
- Spring - bean配置-FactoryBean
- Spring-jdbc
- android 显示当前时间(EEEE)
- git常用命令
- spfa算法(洛谷模板题)
- QT 自定义IP地址输入框
- 16.笔记JAVA Spring框架学习————通过FactoryBean配置Bean
- Effective Java读书笔记-覆盖equals时遵守的通用约定
- 爬虫小白之————爬取csdn博客列表页面
- 单调栈
- 17.笔记JAVA Spring框架学习————基于注解配置Bean之一
- 在myeclipse中如何进行调试
- Android中的ListView设置setOnItemClickListener后发现onItemClick毫无作用的问题
- WorkerMan和ThingPHP的结合使用
- 三道典型C语言题(4)