Spring IOC 编程实现

                                                                                                                                         20190609 田超凡

                                                                       知网用户_进击的猿宝宝(田超凡)版权所有,转载注明原作者

1.   接口和依赖对象初始化

ManyFruits 种植水果类

ManyVegetables 种植蔬菜类

Fruit 水果父类

   Apple 苹果子类

   Banana 香蕉子类

  Vegetable 蔬菜父类

     Tomato 西红柿子类

     Potato 马铃薯子类

  Farm 农场父接口

    FruitFarm 水果农场子接口  

        FruitFarmImpl 实现类

VegetableFarm 蔬菜农场子接口

        VegetableFarmImpl实现类

依赖关系:

   ManyFruits 种植的水果 —> Fruit 具体种植的水果

   ManyVegetables 种植的蔬菜 —> Vegetable 具体种植的蔬菜

   FruitFarmImpl 水果农场 —> ManyFruits需要种植的水果

FruitFarmImpl 水果农场 —> Fruit 需要采购的水果

VegetableFarmImpl 蔬菜农场 —> ManyVegetables 需要种植的蔬菜

VegetableFarmImpl 蔬菜农场 —> Vegetable 需要采购的蔬菜

FarmWorker 农场工作类 —> Farm 农场

 

package com.ioc.tcf.xml.fruit;

/***

 * TODO TCF 采购的水果父类

 * @author 71485

 *

 */

public class Fruit {

 

    //TODO TCF 产地

    private String madePlace;

   

    //TODO TCF 新鲜度

    private Integer freshTemp;

   

    public String getMadePlace() {

       return madePlace;

    }

    public void setMadePlace(String madePlace) {

       this.madePlace = madePlace;

    }

    public Integer getFreshTemp() {

       return freshTemp;

    }

    public void setFreshTemp(Integer freshTemp) {

       this.freshTemp = freshTemp;

    }

   

    public Fruit(String madePlace,Integer freshTemp)

    {

       this.madePlace=madePlace;

       this.freshTemp=freshTemp;

    }

   

    public Fruit()

    {

      

    }

}

package com.ioc.tcf.xml.vegetables;

/**

 * TODO TCF 采购的蔬菜父类

 * @author 71485

 *

 */

public class Vegetable {

 

    //TODO TCF 叶绿素

    private String leafGreenTemp;

   

    //TODO TCF光照程度

    private Integer lightLevel;

   

    public String getLeafGreenTemp() {

       return leafGreenTemp;

    }

    public void setLeafGreenTemp(String leafGreenTemp) {

       this.leafGreenTemp = leafGreenTemp;

    }

    public Integer getLightLevel() {

       return lightLevel;

    }

    public void setLightLevel(Integer lightLevel) {

       this.lightLevel = lightLevel;

    }

   

    public Vegetable(String leafGreenTemp,Integer lightLevel)

    {

       this.leafGreenTemp=leafGreenTemp;

       this.lightLevel=lightLevel;

    }

    public Vegetable()

    {

   

    }

}

package com.ioc.tcf.xml.fruit;

/**

 * TODO TCF 苹果

 * @author 71485

 *

 */

public class Apple extends Fruit {

 

    public Apple(String madePlace,Integer freshTemp)

    {

       super(madePlace, freshTemp);

    }

   

    //TODO TCF 品种

    private String type;

   

    public String getType() {

       return type;

    }

    public void setType(String type) {

       this.type = type;

    }

}

package com.ioc.tcf.xml.fruit;

/***

 * TODO TCF 香蕉

 * @author 71485

 *

 */

public class Banana extends Fruit{

 

    public Banana(String madePlace,Integer freshTemp)

    {

       super(madePlace, freshTemp);

    }

   

    //TODO TCF 成熟度

    private String readyTemp;

   

    public String getReadyTemp() {

       return readyTemp;

    }

    public void setReadyTemp(String readyTemp) {

       this.readyTemp = readyTemp;

    }

   

}

package com.ioc.tcf.xml.vegetables;

/**

 * TODO TCF 西红柿

 * @author 71485

 *

 */

public class Tomato extends Vegetable{

 

    public Tomato(String leafGreenTemp, Integer lightLevel)

    {

       super(leafGreenTemp, lightLevel);

    }

   

    //TODO TCF 甜度

    private String sweetTemp;

   

    public String getSweetTemp() {

       return sweetTemp;

    }

    public void setSweetTemp(String sweetTemp) {

       this.sweetTemp = sweetTemp;

    }

}

package com.ioc.tcf.xml.vegetables;

/**

 * TODO TCF 马铃薯

 * @author 71485

 *

 */

public class Potato extends Vegetable{

 

    public Potato(String leafGreenTemp, Integer lightLevel)

    {

       super(leafGreenTemp, lightLevel);

    }

   

    //TODO TCF 规则度

    private String ruleTemp;

   

    public String getRuleTemp() {

       return ruleTemp;

    }

    public void setRuleTemp(String ruleTemp) {

       this.ruleTemp = ruleTemp;

    }

}

package com.ioc.tcf.xml.fruit;

/**

 * TODO TCF 需要种植的水果

 * @author 71485

 *

 */

 

import java.util.List;

 

public class ManyFruits {

 

    //TODO TCF 需要种植的水果

    private List<Fruit> fruits;

   

    public List<Fruit> getFruits() {

       return fruits;

    }

    public void setFruits(List<Fruit> fruits) {

       this.fruits = fruits;

    }

}

package com.ioc.tcf.xml.vegetables;

/**

 * TODO TCF 需要种植的蔬菜

 * @author 71485

 *

 */

 

import java.util.List;

 

public class ManyVegetables {

 

    //TODO TCF 需要种植的蔬菜

    private List<Vegetable> vegetables;

   

    public List<Vegetable> getVegetables() {

       return vegetables;

    }

    public void setVegetables(List<Vegetable> vegetables) {

       this.vegetables = vegetables;

    }

}

 

package com.ioc.tcf.xml.util;

/***

 * TODO TCF 工厂设计模式

 * @author 71485

 *

 */

 

import com.ioc.tcf.xml.farm.Farm;

import com.ioc.tcf.xml.farm.fruit.FruitFarm;

import com.ioc.tcf.xml.farm.fruit.FruitFarmImpl;

import com.ioc.tcf.xml.farm.vegetable.VegetableFarm;

import com.ioc.tcf.xml.farm.vegetable.VegetableFarmImpl;

import com.ioc.tcf.xml.fruit.Fruit;

import com.ioc.tcf.xml.fruit.ManyFruits;

import com.ioc.tcf.xml.vegetables.ManyVegetables;

import com.ioc.tcf.xml.vegetables.Vegetable;

 

public class FarmFactory {

 

    //TODO TCF 构建的目标农场

    private static Farm farm;

   

    //TODO TCF 私有构造函数,外界无法创建工厂实例

    private FarmFactory()

    {

      

    }

   

    //TODO TCF 构造不同类型的农场实例

    public static Farm getFarm(Object obj)

    {

       if(farm==null)

       {

           if(obj instanceof FruitFarm)

           {

              //TODO TCF 水果农场

              FruitFarm fruitFarm=(FruitFarm)obj;

              farm=fruitFarm;

             

              FruitFarmImpl fruitFarmImpl=(FruitFarmImpl)farm;

              fruitFarmImpl.setFruit(new Fruit("中国",100));

              fruitFarmImpl.setManyFruits(new ManyFruits());

           }

           else if (obj instanceof VegetableFarm)

           {

              //TODO TCF 蔬菜农场

              VegetableFarm vegetableFarm=(VegetableFarm)obj;

              farm=vegetableFarm;

             

              VegetableFarmImpl vegetableFarmImpl=(VegetableFarmImpl)farm;

              vegetableFarmImpl.setVegetable(new Vegetable("淡绿色",98));

              vegetableFarmImpl.setManyVegetables(new ManyVegetables());

           }

       }

      

       return farm;

    }

}

package com.ioc.tcf.xml.farm;

/***

 * TODO TCF 农场接口

 * @author 71485

 *

 */

public interface Farm {

 

   //TODO TCF 采购物资

   public void buyThings();

}

 

package com.ioc.tcf.xml.farm.fruit;

 

import com.ioc.tcf.xml.farm.Farm;

 

/***

 * TODO TCF 水果农场

 * @author 71485

 *

 */

public interface FruitFarm extends Farm{

 

   //TODO TCF 种植水果ֲ

   public void growFruits();

}

package com.ioc.tcf.xml.farm.vegetable;

 

import com.ioc.tcf.xml.farm.Farm;

 

/***

 * TODO TCF 蔬菜农场

 * @author 71485

 *

 */

public interface VegetableFarm extends Farm{

 

   //TODO TCF 种植蔬菜

   public void growVegetables();

}

package com.ioc.tcf.xml.farm.fruit;

 

import java.util.List;

 

import com.ioc.tcf.xml.fruit.Apple;

import com.ioc.tcf.xml.fruit.Banana;

import com.ioc.tcf.xml.fruit.Fruit;

import com.ioc.tcf.xml.fruit.ManyFruits;

 

/***

 * TODO TCF 水果农场接口实现类

 * @author 71485

 *

 */

public class FruitFarmImpl implements FruitFarm{

 

     //TODO TCF 需要种植的水果

     private ManyFruits manyFruits;

    

     //TODO TCF 需要采购的水果

     private Fruit fruit;

    

     public ManyFruits getManyFruits() {

           return manyFruits;

     }

     public void setManyFruits(ManyFruits manyFruits) {

           this.manyFruits = manyFruits;

     }

     public Fruit getFruit() {

           return fruit;

     }

     public void setFruit(Fruit fruit) {

           this.fruit = fruit;

     }

    

     //TODO TCF 采购水果

     public void buyThings()

     {

           System.out.println("水果农场====>采购水果");

           System.out.println("产地:"+fruit.getMadePlace());

           System.out.println("新鲜度:"+fruit.getFreshTemp());

     }

 

     //TODO TCF 种植水果

     public void growFruits()

     {

           System.out.println("水果农场====>种植水果");

           System.out.println("种植数量:"+manyFruits.getFruits().size());

           System.out.println("种植水果信息:");

         List<Fruit> fruits=manyFruits.getFruits();

         if(fruits!=null)

         {

              int i=1;

              for(Fruit fruit:fruits)

              {

                    if(fruit instanceof Apple)

                    {

                          Apple apple=(Apple)fruit;

                          System.out.println(i+".苹果");

                          System.out.println("品种:"+apple.getType());

                    }

                    else if(fruit instanceof Banana)

                    {

                        Banana banana=(Banana)fruit;

                        System.out.println(i+".香蕉");

                        System.out.println("成熟度:"+banana.getReadyTemp());

                    }

                   

                    i++;

              }

         }

     }

 

    

}

package com.ioc.tcf.xml.farm.vegetable;

 

import java.util.List;

 

import com.ioc.tcf.xml.vegetables.ManyVegetables;

import com.ioc.tcf.xml.vegetables.Potato;

import com.ioc.tcf.xml.vegetables.Tomato;

import com.ioc.tcf.xml.vegetables.Vegetable;

 

/***

 * TODO TCF 蔬菜农场

 * @author 71485

 *

 */

public class VegetableFarmImpl implements VegetableFarm{

 

   //TODO TCF 需要种植的蔬菜

   private ManyVegetables manyVegetables;

  

   //TODO TCF 需要采购的蔬菜

   private Vegetable vegetable;

  

   public ManyVegetables getManyVegetables() {

      return manyVegetables;

   }

   public void setManyVegetables(ManyVegetables manyVegetables) {

      this.manyVegetables = manyVegetables;

   }

   public Vegetable getVegetable() {

      return vegetable;

   }

   public void setVegetable(Vegetable vegetable) {

      this.vegetable = vegetable;

   }

  

   //TODO TCF 采购蔬菜

   public void buyThings()

   {

      System.out.println("蔬菜农场====>采购蔬菜");

      System.out.println("叶绿素:"+vegetable.getLeafGreenTemp());

      System.out.println("光照等级:"+vegetable.getLightLevel());

   }

  

   //TODO TCF 种植蔬菜

   public void growVegetables()

   {

      System.out.println("蔬菜农场====>种植蔬菜");

      System.out.println("种植数量:"+manyVegetables.getVegetables().size());

      System.out.println("种植蔬菜信息:");

       List<Vegetable> vegetables=manyVegetables.getVegetables();

       if(vegetables!=null)

       {

          int i=1;

          for(Vegetable vegetable:vegetables)

          {

             if(vegetable instanceof Tomato)

             {

                Tomato tomato=(Tomato)vegetable;

                System.out.println(i+".西红柿");

                System.out.println("甜度:"+tomato.getSweetTemp());

             }

             else if(vegetable instanceof Potato)

             {

                 Potato potato=(Potato)vegetable;

                 System.out.println(i+".马铃薯");

                 System.out.println("规则度:"+potato.getRuleTemp());

             }

            

             i++;

          }

       }

   }

}

 

2.   工厂设计模式

(1).定义工厂类FarmFactory,根据农场类型构造对应农场实例

package com.ioc.tcf.xml.util;

/***

 * TODO TCF 工厂设计模式

 * @author 71485

 *

 */

 

import com.ioc.tcf.xml.farm.Farm;

import com.ioc.tcf.xml.farm.fruit.FruitFarm;

import com.ioc.tcf.xml.farm.fruit.FruitFarmImpl;

import com.ioc.tcf.xml.farm.vegetable.VegetableFarm;

import com.ioc.tcf.xml.farm.vegetable.VegetableFarmImpl;

import com.ioc.tcf.xml.fruit.Fruit;

import com.ioc.tcf.xml.fruit.ManyFruits;

import com.ioc.tcf.xml.vegetables.ManyVegetables;

import com.ioc.tcf.xml.vegetables.Vegetable;

 

public class FarmFactory {

 

   //TODO TCF 构建的目标农场

   private static Farm farm;

  

   //TODO TCF 私有构造函数,外界无法创建工厂实例

   private FarmFactory()

   {

     

   }

  

   //TODO TCF 构造不同类型的农场实例

   public static Farm getFarm(Object obj)

   {

      if(farm==null)

      {

          if(obj instanceof FruitFarm)

          {

             //TODO TCF 水果农场

             FruitFarm fruitFarm=(FruitFarm)obj;

             farm=fruitFarm;

            

             FruitFarmImpl fruitFarmImpl=(FruitFarmImpl)farm;

             fruitFarmImpl.setFruit(new Fruit("中国",100));

             fruitFarmImpl.setManyFruits(new ManyFruits());

          }

          else if (obj instanceof VegetableFarm)

          {

             //TODO TCF 蔬菜农场

             VegetableFarm vegetableFarm=(VegetableFarm)obj;

             farm=vegetableFarm;

            

             VegetableFarmImpl vegetableFarmImpl=(VegetableFarmImpl)farm;

             vegetableFarmImpl.setVegetable(new Vegetable("淡绿色",98));

             vegetableFarmImpl.setManyVegetables(new ManyVegetables());

          }

      }

     

      return farm;

   }

}

(2).编写测试类

package com.ioc.tcf.test;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import com.ioc.tcf.xml.farm.Farm;

import com.ioc.tcf.xml.farm.fruit.FruitFarmImpl;

import com.ioc.tcf.xml.farm.vegetable.VegetableFarmImpl;

import com.ioc.tcf.xml.util.FarmFactory;

 

/***

 * TODO TCF 测试类

 * @author 71485

 *

 */

public class Test {

 

   @SuppressWarnings("unused")

   public static void main(String[] args)

   {

      //TODO TCF 启动Spring容器

      ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");

     

      //TODO TCF 1.工厂设计模式

      //TODO TCF 构建水果农场,采购水果

      Farm fruitFarm=FarmFactory.getFarm(new FruitFarmImpl());

      fruitFarm.buyThings();

     

      //TODO TCF 构建蔬菜农场,采购蔬菜

      Farm vegetableFarm=FarmFactory.getFarm(new VegetableFarmImpl());

      vegetableFarm.buyThings();

     

      //TODO TCF ......

     

   }

}

 

运行结果如下:

Spring IOC 编程实现

 

3.   Spring IOC配置式实现

(1).设值注入

<!-- TODO TCF 水果类 -->

    <bean id="fruit" class="com.ioc.tcf.xml.fruit.Fruit">

   

       <!-- TODO TCF 产地 -->

       <property name="madePlace" value="中国"></property>

      

       <!-- TODO TCF 新鲜度 -->

       <property name="freshTemp" value="100"></property>

      

    </bean>

   

    <!-- TODO TCF 蔬菜类 -->

    <bean id="vegetable" class="com.ioc.tcf.xml.vegetables.Vegetable">

      

       <!-- TODO TCF 叶绿素 -->

       <property name="leafGreenTemp" value="淡绿"></property>

      

       <!-- TODO TCF 光照等级 -->

       <property name="lightLevel" value="98"></property>

      

</bean>

(2).构造注入

<!-- TODO TCF 水果类 -->

    <bean id="fruit" class="com.ioc.tcf.xml.fruit.Fruit">

   

       <constructor-arg index="0" value="中国"></constructor-arg>

       <constructor-arg index="1" value="100"></constructor-arg>

      

    </bean>

   

    <!-- TODO TCF 蔬菜类 -->

    <bean id="vegetable" class="com.ioc.tcf.xml.vegetables.Vegetable">

      

       <constructor-arg index="0" value="淡绿"></constructor-arg>

       <constructor-arg index="1" value="98"></constructor-arg>

      

</bean>

 

(3).p命名空间注入

<!-- TODO TCF 水果类 -->

    <bean id="fruit" class="com.ioc.tcf.xml.fruit.Fruit" p:madePlace="中国" p:freshTemp="100"></bean>

   

    <!-- TODO TCF 蔬菜类 -->

<bean id="vegetable" class="com.ioc.tcf.xml.vegetables.Vegetable" p:leafGreenTemp="淡绿" p:lightLevel="98"></bean>

 

(4).其他常用类型的属性注入元素

工具类:

package com.ioc.tcf.xml.util;

 

import java.util.Date;

import java.util.List;

import java.util.Map;

import java.util.Properties;

import java.util.Set;

import com.ioc.tcf.xml.fruit.Fruit;

import com.ioc.tcf.xml.vegetables.Vegetable;

 

/***

 * TODO TCF 多种类型属性注入工具类

 * @author 71485

 *

 */

public class InjectUtil {

 

   private String normal;

   private List<String> normalList;

   private Set<String> normalSet;

   private Map<String,Object> normalMap;

   private Properties normalProperties;

   private Double number;

   private Date time;

   private Fruit fruit;

   private List<Vegetable> vegetables;

  

   public String getNormal() {

      return normal;

   }

   public void setNormal(String normal) {

      this.normal = normal;

   }

   public List<String> getNormalList() {

      return normalList;

   }

   public void setNormalList(List<String> normalList) {

      this.normalList = normalList;

   }

   public Set<String> getNormalSet() {

      return normalSet;

   }

   public void setNormalSet(Set<String> normalSet) {

      this.normalSet = normalSet;

   }

   public Map<String, Object> getNormalMap() {

      return normalMap;

   }

   public void setNormalMap(Map<String, Object> normalMap) {

      this.normalMap = normalMap;

   }

   public Properties getNormalProperties() {

      return normalProperties;

   }

   public void setNormalProperties(Properties normalProperties) {

      this.normalProperties = normalProperties;

   }

   public Double getNumber() {

      return number;

   }

   public void setNumber(Double number) {

      this.number = number;

   }

   public Date getTime() {

      return time;

   }

   public void setTime(Date time) {

      this.time = time;

   }

   public Fruit getFruit() {

      return fruit;

   }

   public void setFruit(Fruit fruit) {

      this.fruit = fruit;

   }

   public List<Vegetable> getVegetables() {

      return vegetables;

   }

   public void setVegetables(List<Vegetable> vegetables) {

      this.vegetables = vegetables;

   }

  

  

}

 

配置文件注入不同类型属性值:

    <!-- TODO TCF 多种类型属性注入 -->

    <bean id="injectUtil" class="com.ioc.tcf.xml.util.InjectUtil">

     

       <!-- TODO TCF 字符串 -->

       <property name="normal" value="测试"></property>

      

       <!-- TODO TCF List -->

       <property name="normalList">

         <list>

           <value>List测试1</value>

           <value>List测试2</value>

         </list>

       </property>

      

       <!-- TODO TCF Set -->

       <property name="normalSet">

         <set>

           <value>Set测试1</value>

           <value>Set测试2</value>

         </set>

       </property>

      

       <!-- TODO TCF Map -->

       <property name="normalMap">

         <map>

           <entry>

             <key><value>Map1</value></key>

             <value>Map测试1</value>

           </entry>

           <entry>

             <key><value>Map2</value></key>

             <value>Map测试2</value>

           </entry>

         </map>

       </property>

      

       <!-- TODO TCF Properties -->

       <property name="normalProperties">

         <props>

           <prop key="Properties1">Properties测试1</prop>

           <prop key="Properties2">Properties测试2</prop>

         </props>

       </property>

      

       <!-- TODO TCF Double -->

       <property name="number">

         <value>125.5</value>

       </property>

      

       <!-- TODO TCF Date -->

       <property name="time">

         <bean factory-bean="dateFormat" factory-method="parse">

           <constructor-arg value="2019-06-09 23:13:15"></constructor-arg>

         </bean>

       </property>

      

       <!-- TODO TCF ref -->

       <property name="fruit" ref="fruit"></property>

      

       <!-- TODO TCF List<Vegetable> -->

       <property name="vegetables">

         <list>

           <bean id="vegetable1" class="com.ioc.tcf.xml.vegetables.Vegetable">

             <property name="leafGreenTemp" value="淡绿"></property>

             <property name="lightLevel">

               <value>95</value>

             </property>

           </bean>

           <bean id="vegetable2" class="com.ioc.tcf.xml.vegetables.Vegetable">

             <property name="leafGreenTemp" value="绿色"></property>

             <property name="lightLevel">

               <value>98</value>

             </property>

           </bean>

           <bean id="vegetable3" class="com.ioc.tcf.xml.vegetables.Vegetable">

             <property name="leafGreenTemp" value="深绿"></property>

             <property name="lightLevel">

               <value>100</value>

             </property>

           </bean>

         </list>

       </property>

      

    </bean>

   

    <!-- TODO TCF 格式化日期 -->

    <bean id="dateFormat" class="java.text.SimpleDateFormat"

       <constructor-arg value="yyyy-MM-dd HH:mm:ss" /> 

    </bean> 

   

    <!-- TODO TCF 引用的Bean -->

    <bean id="fruit" class="com.ioc.tcf.xml.fruit.Fruit">

       <property name="madePlace" value="中国"></property>

       <property name="freshTemp">

         <value>100</value>

       </property>

    </bean>

 

</beans>

 

编写测定类:

package com.ioc.tcf.test;

 

import java.text.SimpleDateFormat;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import com.ioc.tcf.xml.fruit.Fruit;

import com.ioc.tcf.xml.util.InjectUtil;

import com.ioc.tcf.xml.vegetables.Vegetable;

 

/***

 * TODO TCF 测试类

 * @author 71485

 *

 */

public class Test {

 

   @SuppressWarnings("unused")

   public static void main(String[] args)

   {

      //TODO TCF 启动Spring容器

      ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");

     

      //TODO TCF 构造注入DEMO

      InjectUtil injectUtil=(InjectUtil)context.getBean("injectUtil");

      System.out.println("String==>"+injectUtil.getNormal());

   System.out.println("List<String>==>"+injectUtil.getNormalList().size());

      for(String str:injectUtil.getNormalList())

      {

          System.out.print(str+"\t");

      }

      System.out.println();

   System.out.println("Set<String>==>"+injectUtil.getNormalSet().size());

      for(String str:injectUtil.getNormalSet())

      {

          System.out.print(str+"\t");

      }

      System.out.println();

   System.out.println("Map<String,Object>==>"+injectUtil.getNormalMap());

      for(String key:injectUtil.getNormalMap().keySet())

      {

          Object value=injectUtil.getNormalMap().get(key);

          System.out.print(String.valueOf(key)+"\t");

      }

      System.out.println();

System.out.println("Properties==>"+injectUtil.getNormalProperties().size());

       System.out.println(injectUtil.getNormalProperties().getProperty("Properties2"));

      System.out.println("Double==>"+injectUtil.getNumber());

      System.out.println("Date==>"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(injectUtil.getTime()));

      System.out.println("Fruit产地==>"+injectUtil.getFruit().getMadePlace());

      System.out.println("Fruit新鲜度==>"+injectUtil.getFruit().getFreshTemp());

   System.out.println("List<Vegetable>==>"+injectUtil.getVegetables().size());

      for(Vegetable vegetable:injectUtil.getVegetables())

      {

      System.out.println("==============================================");

          System.out.println("Vegetable叶绿素:"+vegetable.getLeafGreenTemp());

          System.out.println("Vegetable光照等级:"+vegetable.getLightLevel());

      System.out.println("==============================================");

      }

   }

}

 

运行结果:

Spring IOC 编程实现

 

编写测试类:

@SuppressWarnings("unused")

    public static void main(String[] args)

    {

       //TODO TCF 启动Spring容器

       ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");

      

       //TODO TCF 设值注入DEMO

       Fruit fruit=(Fruit)context.getBean("fruit");

       Fruit fruit2=context.getBean(Fruit.class);

       Vegetable vegetable=(Vegetable)context.getBean("vegetable");

       Vegetable vegetable2=context.getBean(Vegetable.class);

       System.out.println("水果产地:"+fruit.getMadePlace());

       System.out.println("水果新鲜度:"+fruit.getFreshTemp());

       System.out.println(fruit==fruit2);

       System.out.println("蔬菜叶绿素:"+vegetable.getLeafGreenTemp());

       System.out.println("蔬菜光照等级:"+vegetable.getLightLevel());

       System.out.println(vegetable==vegetable2);

  }

 

运行结果:

Spring IOC 编程实现

 

4.   Spring IOC注解驱动实现

(1).定义组件

@Component("fruit")

public class Fruit

@Component("vegetable")

public class Vegetable

@Repository("injectUtil")

public class InjectUtil

@Service("fruitFarm")

public class FruitFarmImpl implements FruitFarm

@Service("vegetableFarm")

public class VegetableFarmImpl implements VegetableFarm

@Controller

public class TestController

@Component定义普通Bean组件,定义在需要注册的组件类上方

@Repository定义持久层组件,一般标注在持久层接口实现类上方,在业务层直接引用

@Service定义业务层组件,一般标注在业务接口实现类上方,在控制器直接引用

@Controller定义MVC模式下的控制器组件

 

(2).自动装配

package com.ioc.tcf.xml.util;

 

import javax.annotation.Resource;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

 

import com.ioc.tcf.xml.farm.fruit.FruitFarm;

import com.ioc.tcf.xml.farm.vegetable.VegetableFarm;

import com.ioc.tcf.xml.fruit.Fruit;

import com.ioc.tcf.xml.vegetables.Vegetable;

 

@Controller

public class TestController {

 

   @Autowired

   private VegetableFarm vegetableFarm;

  

   @Autowired

   private FruitFarm fruitFarm;

  

   @Resource(name="fruit")

   private Fruit fruit;

  

   @Resource(name="vegetable")

   private Vegetable vegetable;

}

@Autowired按类型匹配,没有找到抛异常,除非指定required=false,则没有找到注入null

@Resource按名称匹配,没有找到抛异常

 

(3).扫描注解

<!-- TODO TCF 扫描组件 -->

    <context:component-scan base-package="com.ioc.tcf.xml.farm,com.ioc.tcf.xml.util,com.ioc.tcf.xml.fruit,com.ioc.tcf.xml.vegetables"></context:component-scan>

相关文章: