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入门第十二课的更多相关文章

  1. Spring入门第十九课

    后置通知 看代码: package logan.study.aop.impl; public interface ArithmeticCalculator { int add(int i, int j ...

  2. Spring入门第十八课

    Spring AOP AspectJ:Java社区里最完整最流行的AOP框架 在Spring2.0以上的版本中,可以使用基于AspectJ注解或者基于XML配置的AOP 看代码: package lo ...

  3. Spring入门第十六课

    接上一次讲课 先看代码: package logan.spring.study.annotation.repository; public interface UserRepository { voi ...

  4. Spring入门第十五课

    泛型依赖注入 看代码: package logan.spring.study.generic.di; public class BaseRepository<T> { } package ...

  5. Spring入门第十四课

    基于注解的方式配置bean(基于注解配置Bean,基于注解来装配Bean的属性) 在classpath中扫描组件 组件扫描(component scanning):Spring能够从classpath ...

  6. CTF---Web入门第十二题 程序逻辑问题

    程序逻辑问题分值:20 来源: 实验吧 难度:中 参与人数:6909人 Get Flag:1993人 答题人数:2070人 解题通过率:96% 绕过 解题链接: http://ctf5.shiyanb ...

  7. Spring入门第十课

    Spring表达式语言:SpEL Spring表达式语言(简称SpEL)是一个支持运行时查询和操作对象图的强大的表达式语言. 语法类似于EL:SpEL使用#{...}作为定界符,所有在大括号中的字符都 ...

  8. Spring入门第二十二课

    重用切面表达式 我们有的时候在切面里面有多个函数,大部分函数的切入点都是一样的,所以我们可以声明切入点表达式,来重用. package logan.study.aop.impl; public int ...

  9. C 语言入门第十二章---C语言文件操作

    C语言具有操作文件的能力,比如打开文件.读取和追加数据.插入和删除数据.关闭文件.删除文件等. 在操作系统中,为了同意对各种硬件的操作,简化接口,不同的硬件设备也都被看成一个文件.对这些文件的操作,等 ...

随机推荐

  1. whl文件下载

    到哪找.whl文件?http://www.lfd.uci.edu/~gohlke/pythonlibs/

  2. struts2的 defalut-action-ref 的使用

    这个配置的用法有值得注意的地方,所以才记录下来: 一般default-action-refer配置的action是在浏览器中输入的网址只输入到项目时或输入错误的action时 所进入的action,一 ...

  3. 无法远程访问 MySql Server

    改表法.可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 "use ...

  4. install_driver(mysql) failed

        安装好了mysql监控神器innotop,正得意,innotoop不可用,其错误提示为install_driver(mysql) failed: Can't load '/usr/lib64/ ...

  5. Pentaho BIServer Community Edtion 6.1 使用教程 第三篇 发布和调度Kettle(Data Integration) 脚本 Job & Trans

    Pentaho BIServer Community Edtion 6.1 集成了 Kettle 组件,可以运行Kettle 程序脚本.但由于Kettle没有直接发布到 BIServer-ce 服务的 ...

  6. win7计划任务定时执行PHP脚本设置图解

    做php开发的朋友有时候会希望自己的电脑能每天定时的运行一下某个脚本,但定时执行php脚本这种概念似乎多半是在linux中才提到,下面这篇文章主要和大家分享一下在win7下如何设置计划任务,以实现定时 ...

  7. 近200篇机器学习&深度学习资料分享【转载】

    编者按:本文收集了百来篇关于机器学习和深度学习的资料,含各种文档,视频,源码等.而且原文也会不定期的更新,望看到文章的朋友能够学到更多. <Brief History of Machine Le ...

  8. javascript正则(带g符号) 多次调用test 结果交替出现

    链接:https://segmentfault.com/q/1010000000582051 http://stackoverflow.com/questions/2851308/why-does-m ...

  9. Visual Studio "无法查找或打开PDB文件"解决方法

       1.问题: 使用C#语言时遇到的问题(C/C++类似,方法一致),我用qt和VS2013也遇到这种问题 编译链接都没问题,调试时出现以下错误提示:      2.解决方法 第一步: 第二步:[调 ...

  10. 英语影视台词---无敌破坏王2大脑互联网(3)((Ralph)我们去喝根汁汽水吧)

    英语影视台词---无敌破坏王2大脑互联网(3)((Ralph)我们去喝根汁汽水吧) 一.总结 一句话总结: Let's go get a root beer. 1.(Ralph)让我来瞧瞧你的本事  ...