标签:@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 + "]"; } }
需要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"> <!-- 若字面值中包含特殊字符, 则可以使用 DCDATA 来进行赋值 --> <value><![CDATA[<BMW>]]></value> </property> <property name="carPrice" value="250000"></property> </bean> </beans>
<!-- 构造器注入值,在 Bean 中必须有对应的构造器 --> <bean id="carConstruct" class="com.spring.configureBean.Car"> <!--根据 index和 value精确的定位,index按属性顺序对应 --> <constructor-arg index="0"> <value><![CDATA[<BMW>]]></value> </constructor-arg> <constructor-arg value="250000" index="1"></constructor-arg> </bean>
声明一个工厂类
package com.spring.configureBean; /** * 静态工厂方法:直接调用某一个类的静态方法就可以返回Bean * @author yyx * 2019年6月11日 */ import java.util.HashMap; import java.util.Map; public class StaticCarFactory { private static Map<String, Car> 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:不推荐 注意:不是配置静态工厂方法实例,而是配置bean实例 --> <bean id="staticFactoryCar" class="com.spring.configureBean.StaticCarFactory" factory-method="getCar"> <!-- 如果静态方法需要参数,则用constructor-arg配置参数 --> <constructor-arg value="Audi"></constructor-arg> </bean>
工厂类
package com.spring.configureBean; import java.util.HashMap; import java.util.Map; /** * 实例工厂方法:即需要创建工厂本身,再调用工厂的实例方法来返回Bean的实例 * * @author yyx 2019年6月16日 */ public class InstanceCarFactory { private Map<String, Car> 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>
测试
实现FactoryBean接口的类
package com.spring.configureBean; import org.springframework.beans.factory.FactoryBean; public class FactoryBeanCar implements FactoryBean<Car> { //可以设置参数 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>
标签:@param arp org auth code strong com method this
原文地址:https://www.cnblogs.com/fengfuwanliu/p/10987992.html