spring---IOC相关配置详解(工厂IOC)

1、IOC之静态工厂方法创建Bean:

实验15:配置通过静态工厂方法创建的bean

创建一个工厂类

public class PersonFactory {

	public static Person createPerson() {
		Person person = new Person();
		person.setId(17);
		person.setName("这是静态工厂方法");
		return person;
	}

}

在applicationContext.xml
	<!-- 使用静态工厂方法
		class 是工厂 的全类名
		factory-method 这是工厂方法
	 -->	
	<bean id="person17" class="com.xypuxing.pojo.factory.PersonFactory" factory-method="createPerson" />

测试代码
	@Test
	public void test17() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person17 = (Person) applicationContext.getBean("person17");
		System.out.println( person17 );
	}


2、IOC之工厂实例方法创建Bean:

实验16:配置通过实例工厂方法创建的bean
在PersonFactory类中添加获取Person对象的方法
public class PersonFactory {
	public Person getPerson() {
		Person person = new Person();
		person.setId(18);
		person.setName("这是工厂实例方法");
		return person;
	}
}

在applicationContext.xml配置文件中的配置:
	<!-- 工厂实例方法分两步:
		第一步:先使用bean标签配置一个bean对象
		第二步:使用bean标签配置factory-bean和factory-method方法
	 -->
	<bean id="personFactory" class="com.xypuxing.pojo.factory.PersonFactory" />
	<!-- 
		factory-bean表示使用哪一个工厂对象的实现
		factory-method表示调用对象的哪一个方法去获取对象实例
	 -->
	<bean id="person18" factory-bean="personFactory" factory-method="getPerson">

测试的代码:
	@Test
	public void test18() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person18 = (Person) applicationContext.getBean("person18");
		System.out.println( person18 );
	}

3、IOC之FactoryBean接口方式创建对象:
实验17:配置FactoryBean接口创建Bean对象
创建一个FactoryBean实现对象
public class PersonFactoryBean implements FactoryBean<Person> {

	public Person getObject() throws Exception {
		Person person = new Person();
		person.setId(20);
		person.setName("这是PersonFactoryBean创建出来的");
		return person;
	}

	public Class<?> getObjectType() {
		return Person.class;
	}

	/**
	 * 判断是否是单例<br/>
	 * 		返回true是单例 <br/>
	 *  	返回false是多例
	 */
	public boolean isSingleton() {
		return true;
	}
}

在applicataion.xml配置文件中的内容
	<!-- 使用FactoryBean接口方式创建Bean对象 -->
	<bean id="person19" class="com.xypuxing.pojo.factory.PersonFactoryBean" />

测试的代码
	@Test
	public void test19() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		Person person19 = (Person) applicationContext.getBean("person19");
		System.out.println(person19);

		Person person191 = (Person) applicationContext.getBean("person19");
		System.out.println(person19 == person191);
	}
4、IOC之继承Bean配置:
实验18:通过继承实现bean配置信息的重用
在applicationContext.xml配置文件中配置
	<!-- 先定义一个base的 person对象 -->
	<bean id="base" class="com.xypuxing.pojo.Person" p:name="basePerson" p:age="18" p:id="12312"/>
	<!-- 然后在另一个bean中。使用parent继承所有的 -->
	<bean id="person20" class="com.xypuxing.pojo.Person" parent="base" p:id="20" />

测试代码
	@Test
	public void test20() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		Person person20 = (Person) applicationContext.getBean("person20");
		System.out.println(person20);
		
		System.out.println( "这是base的---" + applicationContext.getBean("base") );
	}



  • 14
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值