Spring Bean实例的创建方式以及依赖配置
创建Bean通常有三种方法:
1、调用构造器创建;
2、静态工厂创建;
3、实力工厂创建。
A、构造器创建:最常用使用设置注入的方式,要求该类提供无参构造器。这种情况下,class元素是必须的,除非采用继承,class属性值就是Bean的实现类。
Java代码如下:
public interface Person{
public void useAxe();
}
public class Chinese implements Person{
private Axe axe;
public Chinese(){
System.out.println("通过构造器初始化Bean");
}
public void setAxe(Axe axe){
System.out.println("Spring执行依赖注入");
this.axe = axe;
}
public void useAxe(){
System.out.println(axe.chop());
}
}
public interface Axe{
public String chop();
}
public SteelAxe implements Axe{
public SteelAxe(){
System.out.println("Spring初始化依赖Bean....");
}
public String chop(){
System.out.println("Spring依赖Bean初始化完成");
}
}
Spring-base.xml文件配置如下:
<bean id = "steelAxe" class="StellAxe"></bean>
<bean id = "chinese" class="Chinese">
<property name="axe" ref="steelAxe"/>
</bean>
测试代码:
public class Test{
public static void main(String[] args){
ApplicationContext cxt = ClassPathXmlApplicationContext("spring-base.xml");
Chinese cc = cxt.getBean("chinese");
cc.useAxe();
}
}
测试结果:
通过构造器初始化Bean
Spring初始化依赖Bean....
Spring依赖Bean初始化完成
B、静态工厂创建:使用静态工厂创建实例bean,class指向静态工厂类,添加factory-methd属性,用来创建所需的实例Bean.
Java代码如下:
public interface Product{
void buy();
}
public class Book implements Product{
private String msg;
public void buy(){
System.out.println("你好,我是:"+msg);
}
}
public class Food implements Product{
private String msg;
public void buy(){
System.out.println("你好,我是:"+msg);
}
}
public class StaticFactory{
public static Product getBean(String arg){
if(arg.equalsIgnoreCase("book")){
return new Book();
}else{
return new Food();
}
}
}
Spring-base.xml文件配置如下:
<bean id = "book" class="StaticFactory" factory-method="getBean">
<constructor-arg value="book"/>
<property name = "msg" value="book"/>
</bean>
测试代码:
public class Test{
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-base.xml");
Book book = ctx.getBean("book", Book.class);
book.buy();
}
}
C、使用实例工厂:实例工厂不需要class属性,直接用factory-bean属性指向工厂实例,其余配置和静态工厂无异。
public interface Person{
void sayHello();
}
public class Chinese implements Person{
public void sayHello(){
System.out.println("你好,我是中国人!");
}
}
public class American implements Person{
public void sayHello(){
System.out.println("你好,我是美国ren!");
}
}
public class InstanceFactory{
public static Person getInstance(String arg){
if (arg.equelsIgnoreCase("chinese")){
return new Chinese();
}
}
}
Spring-base.xml文件配置如下:
<bean id = "instanceFactory" class = "InstanceFactory"/>
<bean id = "chinese" factory-bean="instanceFactory" factory-method="getInstance">
<constructor-arg value="chinese"/>
</bean>
测试代码:
public class Test{
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-base.xml");
Chinese chinese = ctx.getBean("chinese", Chinese.class);
chinese.sayHello();
}
}
创建Bean通常有三种方法:
1、调用构造器创建;
2、静态工厂创建;
3、实力工厂创建。
A、构造器创建:最常用使用设置注入的方式,要求该类提供无参构造器。这种情况下,class元素是必须的,除非采用继承,class属性值就是Bean的实现类。
Java代码如下:
public interface Person{
public void useAxe();
}
public class Chinese implements Person{
private Axe axe;
public Chinese(){
System.out.println("通过构造器初始化Bean");
}
public void setAxe(Axe axe){
System.out.println("Spring执行依赖注入");
this.axe = axe;
}
public void useAxe(){
System.out.println(axe.chop());
}
}
public interface Axe{
public String chop();
}
public SteelAxe implements Axe{
public SteelAxe(){
System.out.println("Spring初始化依赖Bean....");
}
public String chop(){
System.out.println("Spring依赖Bean初始化完成");
}
}
Spring-base.xml文件配置如下:
<bean id = "steelAxe" class="StellAxe"></bean>
<bean id = "chinese" class="Chinese">
<property name="axe" ref="steelAxe"/>
</bean>
测试代码:
public class Test{
public static void main(String[] args){
ApplicationContext cxt = ClassPathXmlApplicationContext("spring-base.xml");
Chinese cc = cxt.getBean("chinese");
cc.useAxe();
}
}
测试结果:
通过构造器初始化Bean
Spring初始化依赖Bean....
Spring依赖Bean初始化完成
B、静态工厂创建:使用静态工厂创建实例bean,class指向静态工厂类,添加factory-methd属性,用来创建所需的实例Bean.
Java代码如下:
public interface Product{
void buy();
}
public class Book implements Product{
private String msg;
public void buy(){
System.out.println("你好,我是:"+msg);
}
}
public class Food implements Product{
private String msg;
public void buy(){
System.out.println("你好,我是:"+msg);
}
}
public class StaticFactory{
public static Product getBean(String arg){
if(arg.equalsIgnoreCase("book")){
return new Book();
}else{
return new Food();
}
}
}
Spring-base.xml文件配置如下:
<bean id = "book" class="StaticFactory" factory-method="getBean">
<constructor-arg value="book"/>
<property name = "msg" value="book"/>
</bean>
测试代码:
public class Test{
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-base.xml");
Book book = ctx.getBean("book", Book.class);
book.buy();
}
}
C、使用实例工厂:实例工厂不需要class属性,直接用factory-bean属性指向工厂实例,其余配置和静态工厂无异。
public interface Person{
void sayHello();
}
public class Chinese implements Person{
public void sayHello(){
System.out.println("你好,我是中国人!");
}
}
public class American implements Person{
public void sayHello(){
System.out.println("你好,我是美国ren!");
}
}
public class InstanceFactory{
public static Person getInstance(String arg){
if (arg.equelsIgnoreCase("chinese")){
return new Chinese();
}
}
}
Spring-base.xml文件配置如下:
<bean id = "instanceFactory" class = "InstanceFactory"/>
<bean id = "chinese" factory-bean="instanceFactory" factory-method="getInstance">
<constructor-arg value="chinese"/>
</bean>
测试代码:
public class Test{
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-base.xml");
Chinese chinese = ctx.getBean("chinese", Chinese.class);
chinese.sayHello();
}
}