实例化bean

    从bean.xml中<bean>标签内容可以看出bean其实是一个管理对象的东西,我们只需要修改xml配置文件,就可以改变对象之间的依赖关系,不需要去修改任何源代码。我觉得学习好spring这个框架,对于配置文件以及bean的实例化是了解springIoc的关键。

    spring IoC容器则需要根据Bean定义里的配置元数据使用反射机制来创建Bean。在Spring IoC容器中根据Bean定义创建Bean主要有以下几种方式:

一.使用构造器进行定义,上篇文章在介绍搭建简单环境的时候,用的方法就是构造器的方法。

  这里面又包括了空构造器和有参构造器:我们看如下代码:(相关代码大家可以看我上一篇文章)

    

package com.spring.service.impl;
import com.spring.service.GreetingService;
public class GreetingServiceImpl implements GreetingService {
 private String greeting;

 public void setGreeting(String greeting) {
  this.greeting = greeting;
 }
 public GreetingServiceImpl(){
  System.out.println("空构造法");
}
public GreetingServiceImpl(String greeting){ this.greeting=greeting; } public void sayGreeting(){ System.out.println(greeting); }

我们在这个java中有两个构造函数:无参和有参;对于这个区别,在相应的beans.xml中肯定也要有相应变化了。我们在beans.xml中加入一个bean来使用空构造,beans.xml代码如下:

<?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="greetingService" class="com.spring.service.impl.HelloStaticFactory" factory-method="newInstance">
  <!-- property name="greeting" value="45631"/-->
    <!-- collaborators and configuration for this bean go here -->
    <constructor-arg index="0" value="458214"/>
  </bean>
  <bean id="bean2" class="com.spring.service.impl.GreetingServiceImpl"/>
  <bean id="bean3" class="com.spring.service.impl.GreetingServiceImpl">
  <property name="greeting" value="bean3"/>
  </bean>
</beans>

大家看bean2和bean3,其中bean2对应的是空构造法,bean3对应的事有参构造法(上面的greetingService后面会用到,先不用看)。对于有参构造法在传递参数是可以用  

    <property name="greeting" value="bean3"/>
     <constructor-arg index="0" value="458214"/>
然后在测试代码中测试:改变上篇文章中的getBean函数的参数就可以了。


二.静态构造法:因为上面中我已经把beans.xml中已经加入了这个bean,所以下面只给出HelloStaticFactory.java代码:
  
package com.spring.service.impl;
import com.spring.service.GreetingService;
public class HelloStaticFactory{
    public static GreetingService newInstance(String Greeting){
        return new GreetingServiceImpl(Greeting);
    }
}

大家注意在beans.xml中,使用静态构造法,在<bean>标签中要插入factory-method属性。

三.实例工厂化方法实例bean:  HelloInstanceFactory.java  把上段代码中的static给去掉就行。如果不去掉因为是static,所以实例化工厂时会出错

package com.spring.service.impl;

import com.spring.service.GreetingService;

public class HelloInstanceFactory{
    public GreetingService newInstance(String greeting){
        return new GreetingServiceImpl(greeting);
    }
}

下面我给出所有的xml代码,记得在尝试各种方法的时候要把无参的bean给注释起来 不然会解析这个bean,打印的结果就是两行了

<?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="greetingService" class="com.spring.service.impl.HelloStaticFactory" factory-method="newInstance">
  <!-- property name="greeting" value="45631"/-->
    <!-- collaborators and configuration for this bean go here -->
    <constructor-arg index="0" value="458214"/>
  </bean>
  <!-- 无参构造法 -->
      <bean id="bean2" class="com.spring.service.impl.GreetingServiceImpl"/>
   <!-- 有参构造法 -->
  <bean id="bean3" class="com.spring.service.impl.GreetingServiceImpl">
      <constructor-arg index="0" value="hello"/>
  </bean>
  <!-- 实例工厂构造法 -->
          <!-- 定义实例工厂bean -->
          <bean id="beanInstanceFactory" 
                  class="com.spring.service.impl.HelloStaticFactory"/>
          <!-- 使用实例化工厂bean创建bean -->
          <bean  id="bean4" factory-bean="beanInstanceFactory" factory-method="newInstance">
               <constructor-arg index="0" value="Hello Spring!"/>
          </bean>
</beans>

四、三种方式的理解和应用:

    其实这三种实例化bean还是相当于通过new一个对象执行某些函数,就像java一样。构造器方法和直接java实例化一个对象完全是一个道理;而静态工厂方法,就有点像我们在一个类中定义的一个静态函数式一样的,我们可以直接调用这个静态函数(内存中只有一份),而调用过程就是通过factory-method。下面在具体说说实例化工厂方法:

  以构造器方法为例,我们可以有多个构造方法,但是在调用各个方法时,我们每次其实都相当于重新new了一个对象,如果我们想调用一个类中不同的方法时,这样做就很浪费内存了,我们完全只要需要创建一个对象就可以了啊,所以用实例化工厂方法就解决了这个问题。看这段代码(我表述的可能有些模糊 ,但是我觉得看了代码应该就秒懂了):

<bean id="serviceLocator" class="examples.DefaultServiceLocator">
  <!-- inject any dependencies required by this locator bean -->
</bean>
<bean id="clientService"
      factory-bean="serviceLocator"
      factory-method="createClientServiceInstance"/>

<bean id="accountService"
      factory-bean="serviceLocator"
      factory-method="createAccountServiceInstance"/>
public class DefaultServiceLocator {
  private static ClientService clientService = new ClientServiceImpl();
  private static AccountService accountService = new AccountServiceImpl();

  private DefaultServiceLocator() {}

  public ClientService createClientServiceInstance() {
    return clientService;
  }

  public AccountService createAccountServiceInstance() {
    return accountService;
  }
}

原文地址:https://www.cnblogs.com/wangkeustc/p/3481611.html