spring------>Helloworld

先写一个helloworld的类。

public class HelloWorld {
    private String name;
    public void setName2(String name) {
        this.name = name;
    }
    public void hello(){
        System.out.println("hello:"+name);
    }
}

然后写配置文件

<bean id="helloworld" class="com.spring.beans.HelloWorld">
        <property name="name2" value="luwei"/>
    </bean>

这是set方法注入。set后面的名字要一样。

然后就调用了

//1:创建spring的IOC容器对象
        ApplicationContext ctx =new ClassPathXmlApplicationContext("applicationContext.xml");

  //applicationContext是IOC容器的一个接口。

  //ClassPathXmlApplicationContext从类路径下加载配置文件
        //2:从IOC中获取Bean
        HelloWorld  helloWorld=(HelloWorld) ctx.getBean("helloworld");

//getBean("")中对应的是Bean中的id。是用反射的技术。也可以getBean(Helloworld.class);但是要确保一个id。
        //3:掉方法
        helloWorld.hello();

对比传统应用:

HelloWorld helloWorld=new HelloWorld();
        helloWorld.setName("luwei");
        helloWorld.hello();

-----------------------------------------------------------------

依赖注入

  讲到spring,IOC和AOP是两大基石。

  IOC;(inversion of  control)

    IOC控制反转,不是什么技术,而是一种设计思想。在java开发中,IOC意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制。

    学习IOC要明白两个问题:

    1:谁控制谁,控制什么:传统JAVA SE中,直接在对象内部通过new进行创建对象。是程序主动去创建依赖对象;而IOC是有专门的容器来创建这些对象。由IOC容器来控制对象的 创建。谁控制谁,当然是IOC控制对象。控制什么:控制外部资源(不只是对象包括比如文件等);

  

  2:为何是反转,哪些方面反转了:有 反转就有正转,传统应用程序是由我们自己在对象中主动控制去直接获取依赖对象,也就是正转;而反转则是由容器来帮忙创建及注入依赖对象;为何是反转?因为 由容器帮我们查找及注入依赖对象,对象只是被动的接受依赖对象,所以是反转;哪些方面反转了?依赖对象的获取被反转了。
用图例说明一下,传统程序设计如图2-1,都是主动去创建相关对象然后再组合起来:
用IOC:
  
DI:依赖注入:是组件间依赖关系由容器在运行期决定的。
  谁依赖谁:应用程序依赖IOC容器;
  为什么需要依赖:应用程序需要IOC容器来提供对象需要的外部资源;
  谁注入谁:IOC容器注入应用程序某个对象。应用程序依赖的对象;
  注入了什么:就是注入了某个对象所需要的外部资源(包括对象,资源。常量数据)
类之间的关系:
  泛化:表示类与类之间的继承关系。接口与接口之间的继承关系;
  实现:类对接口的实现;
  依赖:当类与类之间有使用关系时候就是依赖关系。表现为:拥有关系“;具体代码可以用实例变量来表示;
  聚合:是关联的特殊情况,体现部分-整体关系。是一种弱拥有关系。整体和部分可以有不一样 的生命周期;
  组合:关联的特殊情况。体现部分-整体关系。但是是一种强拥有关系。整体和部分有相同的生命周期;是强关联‘
然后就是注入:
  注入分为三种:属性注入。构造器注入。静态工厂方法注入
  属性注入:
      <bean id="helloworld" class="com.spring.beans.HelloWorld">
                <property name="name2" value="luwei"/> name中的就是set后面的对应的值;
           </bean>
  构造器注入:
      实体:
      package com.spring.beans;

public class Car {
    private String brand;
    private String corp;
    private int price;
    private int maxSpeed;
    public Car(String brand, String corp, int price, int maxSpeed) {
        super();
        this.brand = brand;
        this.corp = corp;
        this.price = price;
        this.maxSpeed = maxSpeed;
    }
    @Override
    public String toString() {
        return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price
                + ", maxSpeed=" + maxSpeed + "]";
    }
    
}
配置文件
  <bean id ="car" class="com.spring.beans.Car">
        <constructor-arg value="audi" index="0"></constructor-arg>
        <constructor-arg value="shanghai" index="1"></constructor-arg>
        <constructor-arg value="30000" index="2"></constructor-arg>
        <constructor-arg value="300" index="3"></constructor-arg>
       
    </bean>
添加 index属性就是按构造器中的顺序。
换成type 就是按照类型 不过要写包名
  <constructor-arg value="audi" type="java.lang.String"></constructor-arg>
        <constructor-arg value="30000" type="int"></constructor-arg>
工厂方法注入:
静态工厂方法。不用创建实例而是调用方法。
java文件:
package com.spring.beans;

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

public class StaticCarFactory {
    private static Map<String,Car> cars=new HashMap<String, Car>();
    static{
        cars.put("audi", new Car("audi","shanghai",30000,300));
        cars.put("ford", new Car("ford","shanghai",30000,300));
    }
    
    public static Car getCar(String name){
        return cars.get(name);
    }
}
配置文件:
  <bean id="car1" class="com.spring.beans.StaticCarFactory" factory-method="getCar">
            <constructor-arg value="audi"></constructor-arg>
    </bean>    
实例工厂方法:一定先创建实例本身
package com.spring.beans;

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

public class InstanceCarFactory {
    private Map<String, Car> cars=null;
    public InstanceCarFactory(){
        cars=new HashMap<String, Car>();
        cars.put("audi", new Car("audi","shanghai",30000,300));
        cars.put("ford", new Car("ford","shanghai",30000,300));

    }
    public Car getCar(String brand){
        return cars.get(brand);
    }
}
配置文件:
<bean id="carFactory" class="com.spring.beans.InstanceCarFactory"></bean>
    <bean id="car2" factory-bean="carFactory" factory-method="getCar">
        <constructor-arg value="ford"></constructor-arg>
    </bean>
  
---------------------------------spring生命周期----------------------------------------------

           创建的时候,spring启动的时候所有的单例的bean都被创建。
          关闭的时候:工厂关闭。
顺序:构造器-----》set------>init---------->destory
在Bean中配置init-method和destory-method属性,为bean指定初始化和销毁方法。
lazy-init="true" 延迟加载。
--------------------------Bean作用域-------------------
<bean id="helloworld" class="com.spring.beans.HelloWorld" scope="singleton"> 单例的 不写就是默认
scope="prototype"非单例的
单例:在容器创建的时候就创建了,只创建这一次。
非单例:在每次请求的时候创建一个新的bean实例。
--------------------自动装配-------
    <!--
        autowire:自动装载
        byType:将工厂中与目标组件的属性同类型的bean,赋值给对应属性
        byName:将工厂中与目标组件的属性同名的bean,赋值给对应属性
     -->
    <bean id="ioc49" class="com.c47.IOC.test.TestIOC3" autowire="byName"></bean>
posted @ 2016-08-13 19:11  陆伟  阅读(164)  评论(0编辑  收藏  举报