Spring入门第十二课
Bean的配置方法
通过工厂方法(静态工厂方法&实例工厂方法),FactoryBean
通过调用静态工厂方法创建Bean
调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中,当客户端需要对象时,只需要简单的调用静态方法,而不用关心创建对象的细节。
要声明通过静态方法创建的Bean,需要在Bean的class属性里指定拥有该工厂的方法类,同时在factory-method属性里指定工厂方法的名称,最后,使用<constructor-arg>元素为该方法传递方法参数。
看代码
package logan.spring.study.factory; public class Car { private String brand;
private int price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
public Car(String brand, int price) {
super();
this.brand = brand;
this.price = price;
}
}
package logan.spring.study.factory; import java.util.HashMap;
import java.util.Map; public class StaticCarFactory { private static Map<String, Car> cars = new HashMap<String, Car>(); static{
cars.put("audi", new Car("audi",3000000));
cars.put("ford", new Car("ford", 4000000));
} public static Car getCar(String name){
return cars.get(name);
} }
<?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"> <!-- 通过静态工厂方法来配置bean,注意不是配置静态工厂方法实例,而是配置bean实例 -->
<bean id="car1" class="logan.spring.study.factory.StaticCarFactory"
factory-method="getCar">
<constructor-arg value="audi"></constructor-arg>
</bean>
</beans>
package logan.spring.study.factory; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) {
// TODO Auto-generated method stub ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-factory.xml"); Car car1 = (Car) ctx.getBean("car1");
System.out.println(car1); } }
下面是输出结果:
五月 21, 2017 11:21:42 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7aec35a: startup date [Sun May 21 11:21:42 CST 2017]; root of context hierarchy
五月 21, 2017 11:21:42 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-factory.xml]
Car [brand=audi, price=3000000]
下面看实例工厂方法:
实例工厂方法就是实例工厂的方法,
package logan.spring.study.factory; import java.util.HashMap;
import java.util.Map; public class InstanceCarFactory {
private Map<String, Car> cars = null;
public InstanceCarFactory(){
cars = new HashMap<String,Car>();
cars.put("audi", new Car("audi",300000));
cars.put("ford", new Car("ford",500000));
} public Car getCar(String brand){
return cars.get(brand);
} }
下面是配置文件:
<?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"> <!-- 通过静态工厂方法来配置bean,注意不是配置静态工厂方法实例,而是配置bean实例 -->
<bean id="car1" class="logan.spring.study.factory.StaticCarFactory"
factory-method="getCar">
<constructor-arg value="audi"></constructor-arg>
</bean>
<!-- 配置工厂的实例 -->
<bean id="carFactory" class="logan.spring.study.factory.InstanceCarFactory"></bean>
<!-- 通过实例工厂方法来配置bean -->
<bean id="car2" factory-bean="carFactory" factory-method="getCar">
<constructor-arg value="ford"></constructor-arg>
</bean>
</beans>
package logan.spring.study.factory; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) {
// TODO Auto-generated method stub ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-factory.xml"); Car car1 = (Car) ctx.getBean("car1");
System.out.println(car1); Car car2 = (Car) ctx.getBean("car2");
System.out.println(car2); } }
下面是输出结果
五月 21, 2017 7:27:18 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7aec35a: startup date [Sun May 21 19:27:18 CST 2017]; root of context hierarchy
五月 21, 2017 7:27:18 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-factory.xml]
Car [brand=audi, price=3000000]
Car [brand=ford, price=500000]
Spring入门第十二课的更多相关文章
- Spring入门第十九课
后置通知 看代码: package logan.study.aop.impl; public interface ArithmeticCalculator { int add(int i, int j ...
- Spring入门第十八课
Spring AOP AspectJ:Java社区里最完整最流行的AOP框架 在Spring2.0以上的版本中,可以使用基于AspectJ注解或者基于XML配置的AOP 看代码: package lo ...
- Spring入门第十六课
接上一次讲课 先看代码: package logan.spring.study.annotation.repository; public interface UserRepository { voi ...
- Spring入门第十五课
泛型依赖注入 看代码: package logan.spring.study.generic.di; public class BaseRepository<T> { } package ...
- Spring入门第十四课
基于注解的方式配置bean(基于注解配置Bean,基于注解来装配Bean的属性) 在classpath中扫描组件 组件扫描(component scanning):Spring能够从classpath ...
- CTF---Web入门第十二题 程序逻辑问题
程序逻辑问题分值:20 来源: 实验吧 难度:中 参与人数:6909人 Get Flag:1993人 答题人数:2070人 解题通过率:96% 绕过 解题链接: http://ctf5.shiyanb ...
- Spring入门第十课
Spring表达式语言:SpEL Spring表达式语言(简称SpEL)是一个支持运行时查询和操作对象图的强大的表达式语言. 语法类似于EL:SpEL使用#{...}作为定界符,所有在大括号中的字符都 ...
- Spring入门第二十二课
重用切面表达式 我们有的时候在切面里面有多个函数,大部分函数的切入点都是一样的,所以我们可以声明切入点表达式,来重用. package logan.study.aop.impl; public int ...
- C 语言入门第十二章---C语言文件操作
C语言具有操作文件的能力,比如打开文件.读取和追加数据.插入和删除数据.关闭文件.删除文件等. 在操作系统中,为了同意对各种硬件的操作,简化接口,不同的硬件设备也都被看成一个文件.对这些文件的操作,等 ...
随机推荐
- 【BZOJ3956】Count 主席树+单调栈
[BZOJ3956]Count Description Input Output Sample Input 3 2 0 2 1 2 1 1 1 3 Sample Output 0 3 HINT M,N ...
- EasyPlayerPro windows播放器在播放RTMP视频显示重复异常问题解决
问题来源 2017.12.18 今日有杭州某教育领域客户反馈EasyPlayerPro在播放一个rtmp源时,画面显示异常的问题.截图如下: 问题复现 一番思考, 将显示格式改为D3D显示, 正常, ...
- Vue中div高度自适应
Vue中尽量不使用dom的高度计算 <template> <div :style="conheight"> </template> <sc ...
- ArcGIS api for js OverviewMap(鹰眼/概览图)
说明.本篇博客中主要介绍 地图显示在某个div情况 1.运行效果 2.HTML <!DOCTYPE html> <html> <head> <meta htt ...
- Android databinding 开发参考
1,android studio添加databinding依赖需要注意事项: http://www.zhihu.com/question/33538477?sort=created 2, databi ...
- Wannafly挑战赛12 B T95要减肥 【贪心】
链接:https://www.nowcoder.com/acm/contest/79/B 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...
- 3D文字特效
在线演示 本地下载
- koa-bodyparser返回413状态码的问题
413 Request Entity Too Large(请求实体太大) 数日前,我用 node.js 写的一个日志服务抛出了这个状态码-- 自己写的服务抛出了一个自己都不认识的状态码,这是最气的!( ...
- plsql连接oracle 64位数据库ORA-12514错误
今天帮小师妹还原数据库,删除表空间的时候把数据库搞坏了,plsql连接时报了 ORA-12514: TNS:listener does not currently know of service re ...
- Antler 工具使用(.g 转.java / .cs)
1. JAVA环境 2. Antler 工具包: antlr-3.5.1-complete-no-st3.jar 路径加入classpath 3. cmd命令行: java org.antlr.Too ...