Spring学习笔记(十一)

1、Bean实例的创建方式:

1)、第一种,最普通的,就是在配置文件中声明<bean id = "" class = "" />,需要给出具体实现类。
2)、第二种,静态工厂方法创建,配置文件中不需要配置具体实现类,只需要填上工厂类和相应的工厂类的静态方法名,<bean id = "" class = "工厂类名" factory-method = “静态方法名”/>
3)、第三种,实例工厂方法创建,与静态工厂方法类型,只不过方法不是静态而已,配置文件还需要先声明实例工厂Bean,然后引用这个Bean的id,其他的类似。

2、静态工厂方法创建Bean(Person接口):

package com.sxit.service;

public interface Person {
	
}

3、两个实现类:

package com.sxit.service;

public class Chinese implements Person{
	
	private String type;

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	public void info(){
		System.out.println("我是:"+type);
	}
}

  

package com.sxit.service;

public class Us implements Person{
	
	private String type;

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	public void info(){
		System.out.println("我是:"+type);
	}
}

4、静态工厂:

package com.sxit.service;

public class BeanFactory {

	public static Person getPerson(String arg){
		if(arg.equals("chinese")){
			return new Chinese();
		}else{
			return new Us();
		}
	}
}

5、配置文件:

<?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-3.0.xsd ">

	<!-- 这里的class配置的是工程类名,工程方法配置的就是创建Bean的方法名 -->
	<bean id="chinese" class="com.sxit.service.BeanFactory" factory-method="getPerson">
		<constructor-arg name="arg" value="chinese"/>
		<property name="type" value="中国人"/>
	</bean>
	
	<bean id="us" class="com.sxit.service.BeanFactory" factory-method="getPerson">
		<constructor-arg name="arg" value="us"/>
		<property name="type" value="美国人"/>
	</bean>
	
</beans>

 6、测试类:

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sxit.service.Chinese;
import com.sxit.service.Us;


public class Test {

	public static void main(String[] args) {

		ApplicationContext apc = new ClassPathXmlApplicationContext("Bean.xml");
		Chinese chinese = apc.getBean("chinese",Chinese.class);
		Us us = apc.getBean("us",Us.class);
		
		chinese.info();
		us.info();
	}
}

7、打印信息:

我是:中国人
我是:美国人

8、实例工厂创建Bean:   

package com.sxit.service;

public class BeanFactory2 {

	public Person getPerson(String arg){
		if(arg.equals("chinese")){
			return new Chinese();
		}else{
			return new Us();
		}
	}
}

9、配置文件:

<?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-3.0.xsd ">

	<bean id="factory" class="com.sxit.service.BeanFactory2"/>

	<!-- 这里的factory-bean配置的是工厂Bean Id,工程方法配置的就是创建Bean的方法名 -->
	<bean id="chinese" factory-bean="factory" factory-method="getPerson">
		<constructor-arg name="arg" value="chinese"/>
		<property name="type" value="中国人"/>
	</bean>
	
	<bean id="us" factory-bean="factory" factory-method="getPerson">
		<constructor-arg name="arg" value="us"/>
		<property name="type" value="美国人"/>
	</bean>
	
</beans>

 10、测试类及打印信息:

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sxit.service.Chinese;
import com.sxit.service.Us;


public class Test2 {

	public static void main(String[] args) {

		ApplicationContext apc = new ClassPathXmlApplicationContext("Bean2.xml");
		Chinese chinese = apc.getBean("chinese",Chinese.class);
		Us us = apc.getBean("us",Us.class);
		
		chinese.info();
		us.info();
	}
}

我是:中国人
我是:美国人

11、总结:

1)、都需要用到factory-method来指定长生Bean的方法。
2)、工厂方法如果需要参数,则可以通过<constructor-arg name="arg" value="us"/>来设值。
3)、静态class填写工厂类名,实例工厂则先声明,再通过factory-bean = “声明的实例工厂Bean Id”。

   

猜你喜欢

转载自luan.iteye.com/blog/1717863