• Spring攻略学习笔记(3)调用构造程序创建Bean


         通常,当你为一个Bean指定了class属性,就将要求Spring IoC容器调用构造程序创建Bean。

         如下,有一个Product类,他有两个子类Battery和Disc。

        Product类: 

    /*  
      * Copyright 2013-2015  
      */
    package com.jackie.codeproject.springrecipesnote.springioc.shop;
    
    /**
     * Title: Product.java
     *  产品抽象类
     * 
     * @author jackie
     * @since Apr 14, 2013 8:09:49 PM
     * @version V1.0
     */
    public abstract class Product {
        private String name;
        private double price;
        
        public Product() {
            
        }
    
        /**
          * <p>Title: </p>
          * <p>含参构造函数</p>
          * @param name
          * @param price
          */    
        public Product(String name, double price) {
            this.name = name;
            this.price = price;
        }
    
        /**
         * @return the name
         */
        public String getName() {
            return name;
        }
        
        /**
         * @param name the name to set
         */
        public void setName(String name) {
            this.name = name;
        }
        
        /**
         * @return the price
         */
        public double getPrice() {
            return price;
        }
        
        /**
         * @param price the price to set
         */
        public void setPrice(double price) {
            this.price = price;
        }
        
        public String toString() {
            return name + " " + price;
        }
    }
    

    Battery类:

    /*
     * Copyright 2013-2015
     */
    package com.jackie.codeproject.springrecipesnote.springioc.shop;
    
    /**
     * Title: Battery.java 类的功能说明
     * 
     * @author jackie
     * @since Apr 14, 2013 8:14:26 PM
     * @version V1.0
     */
    public class Battery extends Product {
    
        private boolean rechargeable;
    
        public Battery() {
            super();
        }
    
        public Battery(String name, double price) {
            super(name, price);
        }
    
        /**
         * @return the rechargeable
         */
        public boolean isRechargeable() {
            return rechargeable;
        }
    
        /**
         * @param rechargeable
         *            the rechargeable to set
         */
        public void setRechargeable(boolean rechargeable) {
            this.rechargeable = rechargeable;
        }
    }
    

    Disc类:

    /*
     * Copyright 2013-2015
     */
    package com.jackie.codeproject.springrecipesnote.springioc.shop;
    
    /**
     * Title: Disc.java 类的功能说明
     * 
     * @author jackie
     * @since Apr 14, 2013 8:18:30 PM
     * @version V1.0
     */
    public class Disc extends Product {
    
        private int capacity;
    
        public Disc() {
            super();
        }
    
        public Disc(String name, double price) {
            super(name, price);
        }
    
        /**
         * @return the capacity
         */
        public int getCapacity() {
            return capacity;
        }
    
        /**
         * @param capacity
         *            the capacity to set
         */
        public void setCapacity(int capacity) {
            this.capacity = capacity;
        }
    }

    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.2.xsd">
    
        <bean id="aaa" class="com.jackie.codeproject.springrecipesnote.springioc.shop.Battery">
    	<property name="name" value="AAA" />
    	<property name="price" value="2.5" />
    	<property name="rechargeable" value="true" />
        </bean>
    	
        <bean id="cdrw" class="com.jackie.codeproject.springrecipesnote.springioc.shop.Disc">
    	<property name="name" value="CD-RW" />
    	<property name="price" value="1.5" />
    	<property name="capacity" value="700" />
        </bean>
    </beans>

         如果没有指定<constructor-arg>元素,将会调用默认的不带参数的构造程序。然后对于每个<property>元素,Spring将通过setter方法注入值。前面的Bean配置相当于:  

          Product aaa = new Battery();
    
          aaa.setName("AAA");
    
          aaa.setPrice(2.5);
    
          aaa.setRechargeable(true);
    
          Product aaa = new Battery();
    
          aaa.setName("AAA");
    
          aaa.setPrice(2.5);
    
          aaa.setCapacity(700);

         相反,如果有一个或者多个<constructor-arg>元素,Spring将调用匹配参数最合适的构造程序。

         以下是测试函数:

    /*
     * Copyright 2013-2015
     */
    package com.jackie.codeproject.springrecipesnote.springioc.shop;
    
    import org.junit.After;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Title: ProductTest.java 类的功能说 测试类
     * 
     * @author jackie
     * @since Apr 14, 2013 8:31:45 PM
     * @version V1.0
     */
    public class ProductTest {
    
        private ApplicationContext applicationContext;
    
        @Test
        public void testProduct() {
            applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
            Product aaa = (Product) applicationContext.getBean("aaa");
            Product cdrw = (Product) applicationContext.getBean("cdrw");
            System.out.println(aaa);
            System.out.println(cdrw);
        }
    
        @After
        public void after() {
            if (null != applicationContext) {
                applicationContext = null;
            }
        }
    }
    
  • 相关阅读:
    深入分析JavaWeb Item13 -- jsp指令具体解释
    Caused by: Unable to locate parent package [json-default] for [class com.you.user.action.StudentActi
    二分图学习整理
    mysql字段去重方式
    谈一谈我最喜欢的诗人--法国诗人波德莱尔
    玩转Web之html+CSS(一)---论坛首页表格的实现
    Windows 7旗舰版安装Visual Studio 2013 Ultimate的系统必备及注意事项
    android 去掉listview之间的黑线
    android 5.0新特性学习--RecyclerView
    ListView random IndexOutOfBoundsException on Froyo
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3029466.html
Copyright © 2020-2023  润新知