Spring框架学习01:ioc的介绍和创建方式

写在前面:

在寒假深入学习了java web的基础知识后,初步掌握了MVC框架。但很早就听老师说过,在实际的开发中会使用各种各样的框架。在百度搜索了一下学习哪种框架后,选择了SSM框架作为学习的对象。SSM,即Spring+SpringMVC+MyBatis,其中需要学习的最基础是Spring框架。今天主要简单的学习了Spring框架中ioc的思想和创建的方式。

ioc的介绍:

ioc,全称Inversion of Control,也就是控制反转。是Spring框架中的一个基本思想。所谓的控制反转,简单理解的话就是将以前由程序创建对象的方式转变为程序被动地接收对象,从而降低各个结构之间的耦合性。这种思想的使用使得Spring框架的耦合性大大降低,转成了一个个的配置。

ioc的创建(配置):

我们可以使用配置xml文件的方式来进行配置,配置ioc有三种方式,即XML、注解、javaConfig配置。今天简单了解了XML的配置方式:
1.反射模式:即直接在bean配置中指明我们需要的bean对象的全名,例如:

<bean id="car1" class="com.home.factoryMethod.Car">
  <property name="id" value="1"></property> 
  <property name="name" value="Honda"></property>   
  <property name="price" value="300000"></property> 
</bean>

在这里,class就是我们创建的bean对象对应的类.
2. factory(工厂)方法模式:我们通过创建一个工厂类,Spring会通过反射机制找到工厂类,再用工厂类去生成bean对象。
2.1 第一种是静态工厂方法
比如我们首先创建一个bean类:

package com.home.factoryMethod;

public class Car {
    private int id;
    private String name;
    private int price;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car [id=" + id + ", name=" + name + ", price=" + price + "]";
    }

  public Car(){

  }

  public Car(int id, String name, int price) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
    }
}

之后我们再定义一个factory类,里面写好获得该bean的方法:

package com.home.factoryMethod;

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

public class CarStaticFactory {
    private static Map<Integer, Car> map = new HashMap<Integer,Car>();

    static{
        map.put(1, new Car(1,"Honda",300000));
        map.put(2, new Car(2,"Audi",440000));
        map.put(3, new Car(3,"BMW",540000));
    }

    public static Car getCar(int id){
        return map.get(id);
    }
}

最后,我们在xml中进行配置:

<bean id="bmwCar" class="com.home.factoryMethod.CarStaticFactory" factory-method="getCar">
        <constructor-arg value="3"></constructor-arg>           
    </bean>

    <bean id="audiCar" class="com.home.factoryMethod.CarStaticFactory" factory-method="getCar">
        <constructor-arg value="2"></constructor-arg>           
    </bean>

这样我们就可以使用静态工厂来获取bean对象了,但这样的做法有很明显的缺点,我们的数据要在class中定义,这是非常不符合我们常识的做法。
2.2 第二种方法,使用动态工厂来获取bean对象,我们先写一个实例工厂类,

package com.home.factoryMethod;

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

public class CarInstanceFactory {
    private Map<Integer, Car> map = new HashMap<Integer,Car>();

    public void setMap(Map<Integer, Car> map) {
        this.map = map;
    }

    public CarInstanceFactory(){
    }

    public Car getCar(int id){
        return map.get(id);
    }
}

接着在xml中进行配置

<bean id="carFactory" class="com.home.factoryMethod.CarInstanceFactory">
        <property name="map">
            <map>
                <entry key="4">
                        <bean class="com.home.factoryMethod.Car">
                            <property name="id" value="4"></property>   
                            <property name="name" value="Honda"></property> 
                            <property name="price" value="300000"></property>   
                        </bean>
                </entry>    

                <entry key="6">
                        <bean class="com.home.factoryMethod.Car">
                            <property name="id" value="6"></property>   
                            <property name="name" value="ford"></property>  
                            <property name="price" value="500000"></property>   
                        </bean>
                </entry>
            </map>  
        </property>
     </bean>

     <bean id="car4" factory-bean="carFactory" factory-method="getCar">
        <constructor-arg value="4"></constructor-arg>           
     </bean>

     <bean id="car6" factory-bean="carFactory" factory-method="getCar">
        <constructor-arg value="6"></constructor-arg>           
     </bean>

因为工厂本身需要实例化,所以我们可以直接在xml中指定其data,可以解决以上的缺点咯。

总结:

总的来说,xml的配置方法略显繁琐,且配错一步就会无法运行web项目。但作为学习的一环必然要经过这样的过程,望自己更加努力。

猜你喜欢

转载自www.cnblogs.com/wushenjiang/p/12398091.html
今日推荐