Spring 装配Bean 基于XML

从Spring3.0开始,spring容器提供了两种配置Bean的方式
(1)基于XML
(2)基于注解


1.基于xml装配Bean -实例化方式

使用一个或者多个xml作为配置文件,Spring配置文件的根元素是。
bean的实例化方式有3种:默认构造、静态工厂、实例工厂

(1)默认构造

<bean id="" class="">

(2)静态工厂
常用与spring整合其他框架(工具)
静态工厂:用于生成实例对象,所有的方法必须是static

<bean id=""  class="工厂全限定类名(包名+类名)"  factory-method="静态方法">

例子:
1.工厂 MyBeanFactory.java

public class MyBeanFactory {    
    /** 
     * 创建实例 
     * @return 
     */  
    public static UserService createService(){  
        return new UserServiceImpl();  
    }  
}

2.Spring配置 bean.xml

  <!--    
        将静态工厂创建的实例交予spring   
        class 确定静态工厂全限定类名  
        factory-method 确定静态方法名  
   -->  
<bean id="userServiceId" class="com.spring.MyBeanFactory" factory-method="createService"></bean>

3.Test

public void demo02(){  
  //spring 工厂  
  String xmlPath = "com/factory/beans.xml";  
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
  //userService.class 强转  
  UserService userService = applicationContext.getBean("userServiceId"
                                                      ,UserService.class);
   userService.addUser();  
}

(3)实例工厂
实例工厂:必须先有工厂实例对象,通过实例对象创建对象。提供所有的方法都是“非静态”的。
例子:
1.MyBeanFactory.java

/** 
 * 实例工厂,所有方法非静态 
 */  
public class MyBeanFactory {       
    /** 
     * 创建实例 
     * @return 
     */  
    public UserService createService(){  
        return new UserServiceImpl();  
    }   
} 

2.Spring配置 bean.xml
因为不是静态方法,所以要new一个MyBeanFactory的实例来调用createService方法

<!-- 创建工厂实例 -->  
<bean id="myBeanFactoryId" class="com.spring.MyBeanFactory"></bean>  
<!-- 获得userservice   
    * factory-bean 确定工厂实例  
    * factory-method 确定普通方法  
-->  
<bean id="userServiceId" factory-bean="myBeanFactoryId" factory-method="createService"></bean>  

3.Test

public void demo02(){  
   //spring 工厂  
   String xmlPath = "comfactory/beans.xml";  
   ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);  
   UserService userService = applicationContext.getBean("userServiceId" 
                                                     ,UserService.class);                                        
   userService.addUser();  
}  

2.Bean的种类

1)普通bean:之前操作的都是普通bean。,spring直接创建A实例,并返回
2) FactoryBean:是一个特殊的bean,具有工厂生成对象能力,只能生成特定的对象。
bean必须使用 FactoryBean接口,此接口提供方法getObject()用于获得特定bean。
先创建FB实例,使用调用getObject()方法,并返回方法的返回值

FB fb = new FB();  
return fb.getObject(); 

BeanFactory和FactoryBean对比?
 BeanFactory:工厂,用于生成任意bean。
 FactoryBean:特殊bean,用于生成另一个特定的bean。
例如:ProxyFactoryBean,此工厂bean用于生产代理。
< bean id=”” class=”….ProxyFactoryBean” >获得代理对象实例。AOP使用


3.作用域

作用域:用于确定spring创建bean实例个数
Spring提供“singleton”和“prototype”两种基本作用域,另外提供“request”、“session”、“global session”三种web作用域;
这里写图片描述
取值:
singleton 单例,Bean只会在每个Spring IoC容器中存在一个实例,默认值。
prototype 多例,每执行一次getBean将获得一个实例。

配置信息

<bean id="userServiceId" class="com.UserServiceImpl"  scope="prototype" ></bean> 

TEST:多例,输出两个不同的userService

public void demo02(){  
        //spring 工厂  
        String xmlPath = "com/itheima/d_scope/beans.xml";  
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);  
        UserService userService = applicationContext.getBean("userServiceId" ,UserService.class);  
        UserService userService2 = applicationContext.getBean("userServiceId" ,UserService.class);  

        System.out.println(userService);  
        System.out.println(userService2);  
    }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值