Spring 小结(二)

IOC与DI

  • IOC(Inversion of Control):其思想是反转资源获取的方向,传统的资源查找方式要求组件向容器发起请求查找资源,作为回应,容器适时的返回资源,* 而应用了IOC之后,则是容器主动的将资源推送给它所管理的组件,组件所要做的仅是选择一种合适的方式来接受资源,这种行为也被称为查找的被动形式 *。
  • DI(Dependency Injection)——IOC的另一种表达方式:即组件以一些预先定义好的方式(例如:setter方法)接受来自如容器的资源注入。相对IOC而言,这种表述更直接
  • IOC采用反转控制: 容器主动的向组件进行返回资源,组件需要什么类型的资源,不需要发起请求,只需要列出条件,容器自然就会给组件进行发送资源

Spring的Bean配置

配置形式:基于xml文件的方式;基于注解的方式
Bean的配置方式:通过全类名(反射),通过工厂方法(静态工厂方法&实例工厂方法),FactoryBean
- IOC容器Bean Factory & ApplicationContext概述
- 依赖注入的方式:属性注入;构造器注入
- 注入属性值的细节
- 自动转配
- bean之间的关系;继承;依赖
- bean的作用域:singleton;protot;WEB环境作用域
- 使用外部属性文件
- spEl
- IOC容器中Bean的生命周期
- Sprin4.x新特性:泛型依赖注入    

在IOC中配置Bean

        <!--配置bean-->
            <!--
                class : bean的全类名,通过反射的方式在IOC容器中创建Bean,所以要求Bean中必须有无参数的构造器
                id : 标识容器中的Bean id唯一
            -->
            <bean id="helloWorld" class="com.hongkun.spring.beans.HelloWorld">
                <property name="name2" value="Spring"></property>
            </bean>

Spring容器

    在SpringIOC容器读取Bean配置创建Bean实例之前,必须对它进行实例化,只有在容器实例化后,才可以从IOC容器里
        获取Bean实例并使用。
    Spring提供了两种类型的IOC容器实现
        - BeanFactory IOC容器的基本实现
        - ApplicationContext 提供了更多高级的特性,是BeanFactory的子接口
        - BeanFactory是Spring框架的基础设施,面向Spring本身;
          ApplicationContext面向使用Spring框架的开发者,几乎所有的应用场合都直接使用ApplicationContext而非
          底层的BeanFactory
        - 无论使用何种方式,配置文件时相同

ApplicationContext的主要实现类

    - ClassPathXmlApplicationContext:从类路径下加载配置文件
    - FileSystemXmlApplicationContext:从文件系统中加载配置文件
    - ApplicationContext的实现与继承关系图
                        ApplicationContext
                               ||
                               ▽
                  ConfigurableApplicationContext(实现ApplicationContext)
                    ||                   ||
                            (继承关系)
                    ▽                   ▽
      ClassPathXmlApplicationContext     FileSystemXmlApplicationContext

在IOC容器中获取Bean

调用ApplicationContext的getBean()方法
//2.从IO从容器中获取Bean实例
//利用id定位到IOC容器中的bean
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
//利用类型返回IOC容器中的Bean,但要求IOC容器中必须只能有一个该类型的Bean
//HelloWorld helloWorld = ctx.getBean(HelloWorld.class);

属性的注入方式

  • 属性注入
属性注入即通过setter方法注入bean的属性值或依赖的对象
属性注入使用<property>元素,使用name属性指定Bean的属性名称,value属性或<value>子节点指定属性值
属性注入是实际应用中最常用的注入方式
例:
<bean id="helloWorld"class="com.hongkun.spring.beans.HelloWorld">
        <property name="name2" value="Spring"></property>
</bean>

向HelloWorld类的name2属性注入Spring值
  • 构造器注入
1.通过构造方法注入Bean的属性值或依赖对象,它保证了Bean实例在实例化后就可以使用
2.构造器注入在<constructor-arg>元素里声明属性,<constructor-arg>中没有name属性
例:
   1. 创建一个Car类
        public class Car {
            private String brand;
            private String corp;
            private int price;
            private int maxSpeed;

            public Car(String brand, String corp, int price) {
                this.brand = brand;
                this.corp = corp;
                this.price = price;
            }     
            @Override
            public String toString() {
                return "Car{" +
                        "brand='" + brand + '\'' +
                        ", corp='" + corp + '\'' +
                        ", price=" + price +
                        ", maxSpeed=" + maxSpeed +
                        '}';
            }
        }
2.    <!--通过构造方法来配置bean的属性-->
    <bean id="car" class="com.hongkun.spring.beans.Car">
        <constructor-arg value="Audi"></constructor-arg>
        <constructor-arg value="Shanghai"></constructor-arg>
        <constructor-arg value="300000"></constructor-arg>
    </bean>
3. 在类中使用
Car car = (Car) ctx.getBean("car");
System.out.println(car);

控制台输出: >>Car{brand=’Audi’, corp=’Shanghai’, price=300000.0, maxSpeed=0}

当该类中有两个重载的构造器时

1.car类中有两个构造器
public class Car {
    private String brand;
    private String corp;
    private double price;
    private int maxSpeed;

    public Car(String brand, String corp, double price) {
        this.brand = brand;
        this.corp = corp;
        this.price = price;
    }

    public Car(String brand, String corp, int maxSpeed) {
        this.brand = brand;
        this.corp = corp;
        this.maxSpeed = maxSpeed;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", corp='" + corp + '\'' +
                ", price=" + price +
                ", maxSpeed=" + maxSpeed +
                '}';
    }
}
2.bean配置
<!--通过构造方法来配置bean的属性-->
    <bean id="car" class="com.hongkun.spring.beans.Car">
        <constructor-arg value="Audi"></constructor-arg>
        <constructor-arg value="Shanghai"></constructor-arg>
        <constructor-arg value="300000" ></constructor-arg>
    </bean>
    <bean id="car2" class="com.hongkun.spring.beans.Car">
        <constructor-arg value="BenChi"></constructor-arg>
        <constructor-arg value="Shanghai"></constructor-arg>
        <constructor-arg value="200"></constructor-arg>
    </bean>

3.调用输出
Car car = (Car) ctx.getBean("car");
Car car2 = (Car) ctx.getBean("car2");
System.out.println(car);
System.out.println(car2);

此时控制台输出为 >> Car{brand=’Audi’, corp=’Shanghai’, price=300000.0, maxSpeed=0}
Car{brand=’BenChi’, corp=’Shanghai’, price=200.0, maxSpeed=0}

但我们需要的是所有属性全部进行赋值,所以在Bean中的配置应当是如下
 <!--通过构造方法来配置bean的属性-->
    <bean id="car" class="com.hongkun.spring.beans.Car">
        <constructor-arg value="Audi" index="0"></constructor-arg>
        <constructor-arg value="Shanghai" index="1"></constructor-arg>
        <constructor-arg value="300000" type="double"></constructor-arg>
    </bean>
    <!--使用构造器注入属性值可以指定参数的位置和参数的类型,以区分重载的构造器-->
    <bean id="car2" class="com.hongkun.spring.beans.Car">
        <constructor-arg value="BenChi" type="java.lang.String"></constructor-arg>
        <constructor-arg value="Shanghai" type="java.lang.String"></constructor-arg>
        <constructor-arg value="200" type="int"></constructor-arg>
    </bean>

使用构造器注入属性值可以指定参数的位置和参数的类型,以区分重载的构造器。
constructor-arg 的index属性代表了该参数在构造器中的位置,type为该参数的类型,两者可搭配使用。
此时控制台输出 >> Car{brand=’Audi’, corp=’Shanghai’, price=300000.0, maxSpeed=0}
Car{brand=’BenChi’, corp=’Shanghai’, price=0.0, maxSpeed=200}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值