Spring入门第十二课

Posted alittlecomputer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring入门第十二课相关的知识,希望对你有一定的参考价值。

Bean的配置方法

通过工厂方法(静态工厂方法&实例工厂方法),FactoryBean

通过调用静态工厂方法创建Bean

调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中,当客户端需要对象时,只需要简单的调用静态方法,而不用关心创建对象的细节。

要声明通过静态方法创建的Bean,需要在Bean的class属性里指定拥有该工厂的方法类,同时在factory-method属性里指定工厂方法的名称,最后,使用<constructor-arg>元素为该方法传递方法参数。

 

看代码

package logan.spring.study.factory;

public class Car {
    
    private String brand;
    private int price;
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    @Override
    public String toString() {
        return "Car [brand=" + brand + ", price=" + price + "]";
    }
    public Car(String brand, int price) {
        super();
        this.brand = brand;
        this.price = price;
    }
}
package logan.spring.study.factory;

import java.util.HashMap;
import java.util.Map;

public class StaticCarFactory {
    
    private static Map<String, Car> cars = new HashMap<String, Car>();
    
    static{
        cars.put("audi", new Car("audi",3000000));
        cars.put("ford", new Car("ford", 4000000));
    }
    
    public static Car getCar(String name){
        return cars.get(name);
    }

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

    <!-- 通过静态工厂方法来配置bean,注意不是配置静态工厂方法实例,而是配置bean实例 -->
    <bean id="car1" class="logan.spring.study.factory.StaticCarFactory"
    factory-method="getCar">
    <constructor-arg value="audi"></constructor-arg>
    </bean>
</beans>
package logan.spring.study.factory;

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

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-factory.xml");
        
        Car car1 = (Car) ctx.getBean("car1");
        System.out.println(car1);

    }

}

下面是输出结果:

五月 21, 2017 11:21:42 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org[email protected]7aec35a: startup date [Sun May 21 11:21:42 CST 2017]; root of context hierarchy
五月 21, 2017 11:21:42 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-factory.xml]
Car [brand=audi, price=3000000]

 下面看实例工厂方法:

实例工厂方法就是实例工厂的方法,

package logan.spring.study.factory;

import java.util.HashMap;
import java.util.Map;

public class InstanceCarFactory {
    private Map<String, Car> cars = null;
    public InstanceCarFactory(){
        cars = new HashMap<String,Car>();
        cars.put("audi", new Car("audi",300000));
        cars.put("ford", new Car("ford",500000));
    }
    
    public Car getCar(String brand){
        return cars.get(brand);
    }

}

下面是配置文件:

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

    <!-- 通过静态工厂方法来配置bean,注意不是配置静态工厂方法实例,而是配置bean实例 -->
    <bean id="car1" class="logan.spring.study.factory.StaticCarFactory"
    factory-method="getCar">
    <constructor-arg value="audi"></constructor-arg>
    </bean>
    <!-- 配置工厂的实例 -->
    <bean id="carFactory" class="logan.spring.study.factory.InstanceCarFactory"></bean>
    <!-- 通过实例工厂方法来配置bean -->
    <bean id="car2" factory-bean="carFactory" factory-method="getCar">
        <constructor-arg value="ford"></constructor-arg>
    </bean>
</beans>
package logan.spring.study.factory;

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

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-factory.xml");
        
        Car car1 = (Car) ctx.getBean("car1");
        System.out.println(car1);
        
        Car car2 = (Car) ctx.getBean("car2");
        System.out.println(car2);

    }

}

下面是输出结果

五月 21, 2017 7:27:18 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org[email protected]7aec35a: startup date [Sun May 21 19:27:18 CST 2017]; root of context hierarchy
五月 21, 2017 7:27:18 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-factory.xml]
Car [brand=audi, price=3000000]
Car [brand=ford, price=500000]

 

以上是关于Spring入门第十二课的主要内容,如果未能解决你的问题,请参考以下文章

Spring入门第二课

HTML(入门第二课)

python入门第二课

Hibernate入门第二课 Hibernate Tools for Eclipse Plugins安装

StoneDB Class入门第二课:StoneDB整体架构解析

StoneDB Class入门第二课:StoneDB整体架构解析