zoukankan      html  css  js  c++  java
  • 学习Spring(二) 调用静态工厂方法创建Bean

    1,创建抽象的产品类

    package com.robert.spring.shop;
    
    public abstract class Product {
    
    }
    

    2,创建具体产品类

    package com.robert.spring.shop;
    
    public class Battery extends Product {
    	private String name;
    	private double price;
    
    	public Battery(String name, double price) {
    		this.name = name;
    		this.price = price;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public double getPrice() {
    		return price;
    	}
    
    	public void setPrice(double price) {
    		this.price = price;
    	}
    
    	@Override
    	public String toString() {
    		return "name=" + name + "	price=" + price;
    	}
    }
    
    
    
    
    package com.robert.spring.shop;
    
    public class Disc extends Product {
    	
    	private String name;
    	private double price;
    	
    	public Disc(String name,double price){
    		this.name = name;
    		this.price = price;
    	}
    	
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public double getPrice() {
    		return price;
    	}
    
    	public void setPrice(double price) {
    		this.price = price;
    	}
    
    	@Override
    	public String toString() {
    		return "name="+name+"	price="+price;
    	}
    	
    	
    }
    

    3,创建工厂类

    package com.robert.spring.shop;
    
    public class ProductCreator {
    	
    	public static Product createProduct(String productId){
    		if("aaa".equals(productId)){
    			return new Battery("AAA",2.5);
    		} else if("cdrw".equals(productId)){
    			return new Disc("CD-RW",1.5);
    		}
    		throw new IllegalArgumentException("Unknown product");
    	}
    }
    


    4,创建测试类

    package com.robert.spring.shop;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class ProductTest {
    	
    	public static void main(String[] args) {
    		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/spring/products.xml");
    		Product battery = (Battery)context.getBean("aaa");
    		System.out.println(battery.toString());
    		
    		Product cdrw = (Disc)context.getBean("cdrw");
    		System.out.println(cdrw.toString());
    	}
    }
    

    Spring配置文件内容如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Application context definition. -->
    <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" xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:aop="http://www.springframework.org/schema/aop"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans.xsd
    	http://www.springframework.org/schema/context
    	http://www.springframework.org/schema/context/spring-context.xsd
    	http://www.springframework.org/schema/tx
    	http://www.springframework.org/schema/jdbc
    	http://www.springframework.org/schema/aop
    	http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"
    	default-autowire="byName">
    	
    	<bean id="aaa" class="com.robert.spring.shop.ProductCreator" factory-method="createProduct">
    		<constructor-arg value="aaa"/>
    	</bean>
    	
    	<bean id="cdrw" class="com.robert.spring.shop.ProductCreator" factory-method="createProduct">
    		<constructor-arg value="cdrw"/>
    	</bean>
    </beans>
    

    输出结果为:

    name=AAA price=2.5
    name=CD-RW price=1.5




     

  • 相关阅读:
    (转)Esri微博地址收录
    使用liquibasemavenplugin实现持续数据库集成
    CenOS系统中安装Tomcat7并设置为自启动服务
    Androidx86虚拟机安装配置全攻略
    jdk8根据某一属性去重方法 Collectors.collectingAndThen
    [转]恶心的C语言strtok函数
    人生七日忠告 人生征途须记
    VTK中二维Delaunay三角剖分
    Python编程题41原地反转字符串
    Python编程题42除自身以外元素的乘积
  • 原文地址:https://www.cnblogs.com/mengjianzhou/p/5986809.html
Copyright © 2011-2022 走看看