Bean实例的创建方式及对应配置

1.调用构造器创建bean实例

 

package org.cric.util;

public interface Axe {
	//Axe 接口有个砍得方法
	public String chop();
}
 
package org.cric.util;

public class SteelAxe implements Axe {
	//默认构造器
	public SteelAxe(){
		System.out.println("Spring实例化依赖Bean:SteelAxe实例……");
	}
	//实现Axe接口的chop方法
	public String chop() {
		
		return "钢斧砍材真快";
	}
}
 
package org.cric.util;

public interface Person {
	//Person 接口一个使用斧子的方法
	public void userAxe();
}
 
package org.cric.util;

public class Chinese implements Person {
	
	private Axe axe;
	//默认的构造器
	public Chinese(){
		System.out.println("Spring 实例化主调Bean:Chinese 实例……");
	}
	//设值注入所需的setter方法
	public void setAxe(Axe axe) {
		System.out.println("Spring 执行依赖关系注入");
		this.axe = axe;
	}
	//实现Person接口的useAxe()方法
	public void userAxe() {
		System.out.println(axe.chop());
	}

}


<?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-2.5.xsd ">
	<bean id="axe" class="org.cric.util.SteelAxe"/>
	<bean id="person" class="org.cric.util.Chinese">
		<property name="axe" ref="axe"/>
	</bean>
</beans>
 
package org.cric.util;

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

public class BeanTest {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		System.out.println("程序已经实例化Spring 容器");
		Chinese chinese = (Chinese)context.getBean("person");
		System.out.println("程序已经完成了chinese bean 的实例化");
		chinese.userAxe();
	}

}
 

2.使用静态工厂方法创建bean

package org.cric.staticfactory;

public interface Bing {
	//接口定义testBeing()方法
	public void testBing();
}
 
package org.cric.staticfactory;

public class Cat implements Bing {
	
	private String msg;
	//依赖注入时必需的setter方法
	public void setMsg(String msg) {
		this.msg = msg;
	}
	//实现接口必须实现的testBing方法
	public void testBing() {
		System.out.println(msg + "猫喜欢吃老鼠");
	}

}
 

package org.cric.staticfactory;

public class Dog implements Bing {
	
	private String msg;
	//依赖注入式必需的setter方法
	public void setMsg(String msg) {
		this.msg = msg;
	}
	//实现接口必须实现的testBing方法
	public void testBing() {
		System.out.println(msg + "狗爱啃骨头");
	}

}
 
package org.cric.staticfactory;

public class BingFactory {
	
	public static Bing getBing(String arg){
		if(arg.equalsIgnoreCase("dog")){
			return new Dog();
		}else{
			return new Cat();
		}
	}
}
 

<?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-2.5.xsd ">
	<!--
	<bean id="axe" class="org.cric.util.SteelAxe"/>
	<bean id="person" class="org.cric.util.Chinese">
		<property name="axe" ref="axe"/>
	</bean>
	-->
	<!-- 配置BingFactory的getBing()方法产生dog bean -->
	<bean id="dog" class="org.cric.staticfactory.BingFactory" factory-method="getBing">
		<!-- 配置静态工厂方法的参数 -->
		<constructor-arg value="dog"/>
		<!-- 配置普通依赖注入属性 -->
		<property name="msg" value="我是狗!"/>
	</bean>
	<!-- 配置BingFactory的getBing()方法产生cat bean -->
	<bean id="cat" class="org.cric.staticfactory.BingFactory" factory-method="getBing">
		<!-- 配置静态工厂方法的参数 -->
		<constructor-arg value="cat"/>
		<!-- 普通属性依赖注入属性 -->
		<property name="msg" value="我是猫!"/>
	</bean>
</beans>
 

package org.cric.staticfactory;

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

public class BeanTest {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		Bing b1 = (Bing)context.getBean("dog");
		b1.testBing();
		Bing b2 = (Bing)context.getBean("cat");
		b2.testBing();
	}

}
 
3.使用实例工厂方法创建bean

package org.cric.other;

public interface Person {
	//定义一个打招呼的方法
	public String sayHello(String name);
	//定义一个告别的方法
	public String sayGoodBye(String name);
}
 
package org.cric.other;

public class Chinese implements Person {

	public String sayGoodBye(String name) {
		
		return name + ",下次再见";
	}
	public String sayHello(String name) {
		
		return name + ",你好";
	}

}
 

package org.cric.other;

public class American implements Person {

	public String sayGoodBye(String name) {

		return name + ",GoodBye";
	}
	public String sayHello(String name) {

		return name + ",Hello";
	}
}
 

package org.cric.other;

public class PersonFactory {
	
	public Person getPerson(String arg){
		if(arg.equalsIgnoreCase("chinese")){
			return new Chinese();
		}else{
			return new American();
		}
	}
}
 
<?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-2.5.xsd ">
	<!--
	<bean id="axe" class="org.cric.util.SteelAxe"/>
	<bean id="person" class="org.cric.util.Chinese">
		<property name="axe" ref="axe"/>
	</bean>
	-->
	<!-- 配置BingFactory的getBing()方法产生dog bean -->
	<bean id="dog" class="org.cric.staticfactory.BingFactory" factory-method="getBing">
		<!-- 配置静态工厂方法的参数 -->
		<constructor-arg value="dog"/>
		<!-- 配置普通依赖注入属性 -->
		<property name="msg" value="我是狗!"/>
	</bean>
	<!-- 配置BingFactory的getBing()方法产生cat bean -->
	<bean id="cat" class="org.cric.staticfactory.BingFactory" factory-method="getBing">
		<!-- 配置静态工厂方法的参数 -->
		<constructor-arg value="cat"/>
		<!-- 普通属性依赖注入属性 -->
		<property name="msg" value="我是猫!"/>
	</bean>
	<!-- 配置工厂Bean,该Bean负责产生其他Bean实例 -->
	<bean id="personFactory" class="org.cric.other.PersonFactory"/>
	<bean id="chinese" factory-bean="personFactory" factory-method="getPerson">
		<!-- 调用工厂方法时,传入的参数通过 constructor-arg 指定-->
		<constructor-arg value="chinese"/>
	</bean>
	<bean id="american" factory-bean="personFactory" factory-method="getPerson">
		<constructor-arg value="american"/>
	</bean>
</beans>
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值