Spring探究-----配置Bean详解

2020-12-13 02:23

阅读:443

标签:@param   arp   org   auth   code   strong   com   method   this   

    声明一个实体类Car

package com.spring.configureBean;

/**
 * 车辆类
 * 
 * @author yyx 2019年6月11日
 */
public class Car {
    /**
     * 车辆名称
     */
    private String carName;
    /**
     * 车辆价格
     */
    private Integer carPrice;
    
    public Car() {
        super();
    }

    public Car(String carName, Integer carPrice) {
        super();
        this.carName = carName;
        this.carPrice = carPrice;
    }
    
    public String getCarName() {
        return carName;
    }

    public void setCarName(String carName) {
        this.carName = carName;
    }

    public Integer getCarPrice() {
        return carPrice;
    }

    public void setCarPrice(Integer carPrice) {
        this.carPrice = carPrice;
    }

    @Override
    public String toString() {
        return "Car [carName=" + carName + ", carPrice=" + carPrice + "]";
    }
}

   1.setter方式配置Bean

    需要getter和setter方法

xml version="1.0" encoding="UTF-8"?>
beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    bean id="carAttribute" class="com.spring.configureBean.Car">
        property name="carName">
            
            value>]]>value>
        property>
        property name="carPrice" value="250000">property>
    bean>
beans>

   2.构造方法配置Bean


    bean id="carConstruct" class="com.spring.configureBean.Car">
        
        constructor-arg index="0">
            value>]]>value>
        constructor-arg>
        constructor-arg value="250000" index="1">constructor-arg>
    bean>

   3.工厂方式配置Bean

3.1 静态工厂方法

   声明一个工厂类

package com.spring.configureBean;
/**
 * 静态工厂方法:直接调用某一个类的静态方法就可以返回Bean
 * @author yyx
 * 2019年6月11日
 */

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

public class StaticCarFactory {
    private static Map cars = new HashMap();
    static {
        cars.put("Audi", new Car("Audi", 350000));
        cars.put("Ford", new Car("Ford", 200000));
    }

    /**
     * 静态方法
     * 
     * @param keyValue
     * @return
     */
    public static Car getCar(String keyValue) {
        return cars.get(keyValue);
    }
}

   XML配置如下


bean id="staticFactoryCar" class="com.spring.configureBean.StaticCarFactory" factory-method="getCar">
       
       constructor-arg value="Audi">constructor-arg>
bean>

3.2 普通工厂方法

   工厂类

package com.spring.configureBean;

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

/**
 * 实例工厂方法:即需要创建工厂本身,再调用工厂的实例方法来返回Bean的实例
 * 
 * @author yyx 2019年6月16日
 */
public class InstanceCarFactory {
    private Map cars = null;

    public InstanceCarFactory() {
        cars = new HashMap();
        cars.put("Audi", new Car("Audi", 350000));
        cars.put("Ford", new Car("Ford", 200000));
    }

    public Car getCar(String keyValue) {
        return cars.get(keyValue);
    }
}

   XML配置


    bean id="carFactory" class="com.spring.configureBean.InstanceCarFactory">bean>
    
    bean id="instanceFactoryCar" factory-bean="carFactory"
    factory-method="getCar">
       constructor-arg value="Ford">constructor-arg>
    bean>

   测试

   4.通过FactoryBean配置Bean

    实现FactoryBean接口的类

package com.spring.configureBean;

import org.springframework.beans.factory.FactoryBean;

public class FactoryBeanCar implements FactoryBean {
    //可以设置参数
    private String carName;
    
    public void setCarName(String carName) {
        this.carName = carName;
    }

    /**
     * 返回Bean对象
     */
    @Override
    public Car getObject() throws Exception {
        return new Car(carName, 500000);
    }
    
    /**
     * 返回Bean的类型
     */
    @Override
    public Class> getObjectType() {
        return Car.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

}

    XML配置

bean id="factoryBeanCar" class="com.spring.configureBean.FactoryBeanCar">
        property name="carName" value="BMW">property>
    bean>

 

Spring探究-----配置Bean详解

标签:@param   arp   org   auth   code   strong   com   method   this   

原文地址:https://www.cnblogs.com/fengfuwanliu/p/10987992.html


评论


亲,登录后才可以留言!