Spring学习--IoC控制反转

Spring是J2EE应用程序框架,解决了业务逻辑层和其他各层的松耦合问题。轻量级的Java开发框架IoC和AOP的容器框架,主要是针对javaBean的生命周期进行管理的轻量级容器,可以单独使用,也可以和Struts框架等组合使用。

IoC(Inversion of Control)控制反转,对象创建责任的反转,在Spring中BeanFactory是IoC容器的

核心接口,负责实例化、定位,配置应用程序中的对象及建立这些对象间的依赖。

IoC容器类型:
从注入方式上看,主要可以分为三种类型:构造器注入、set属性注入、接口注入。 Spring支持构造函数注入和属性注入。



在构造函数注入中,我们通过调用类的构造函数将接口实现类通过构造函数变量传入。
public
interface Student { public void information(); } public class Stu implements Student { private String name; private String sex; private String age; public Stu(String name, String sex, String age) { this.name = name; this.sex = sex; this.age = age; } public void information() { System.out.println("姓名:"+this.name); System.out.println("性别:"+this.sex); System.out.println("年龄:"+this.age); } } /** * Created by samsung on 2017/10/26. * 构造器注入 */ public class Test { public static void main(String[] args) { BeanFactory bf=new ClassPathXmlApplicationContext("beans.xml"); Student stu=(Student) bf.getBean("stu"); stu.information(); } } /*<constructor-arg>是<bean>标签的子标签。通过其<value>子标签可以为构造方法传递参数*/ <bean id="stu" class="com.wanyu.B.Stu"> <constructor-arg> <value>小明</value> </constructor-arg> <constructor-arg> <value>男</value> </constructor-arg> <constructor-arg> <value>20</value> </constructor-arg> </bean>

在属性注入中,可以随时随地注入变量,更加方便灵活。

/*
通过set注入属性*/ public class Stu01 implements Student { private String name; private String sex; private int age; public void information() { System.out.println("姓名:"+name); System.out.println("性别:"+sex); System.out.println("年龄:"+age); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } /** * Created by samsung on 2017/10/26. * set(设值)注入 */ public class Test { public static void main(String[] args) { BeanFactory bf=new ClassPathXmlApplicationContext("beans.xml"); Student stu=(Student) bf.getBean("stu01"); stu.information(); } } <bean id="stu01" class="com.wanyu.B.Stu01"> <property name="name" value="亮亮"/> <property name="sex" value="男"/> <property name="age" value="23"/> </bean>
在接口注入中,在接口中定义要注入的信息,并通过接口完成注入(效果和属性注入并无本质区别)。

public abstract class Game {
   public abstract void getGame();
}

public class phoneGame extends Game {
    public void getGame() {
        System.out.println("这是款手机游戏");
    }
}

 public interface IPlay {//在接口中注入信息
    public void createGame(Game game);
}

public class Play implements IPlay {
    private Game game;
    public void createGame(Game game) {//接口注入
        this.game=game;
    }
    //根据注入的游戏 取得要玩的游戏类型
    public void getGames(){
        game.getGame();
    }
}

/**
 * Created by samsung on 2017/10/27.
 * 接口注入
 */
public class Test {
    public static void main(String[] args) {
     Game game=new phoneGame();//若要改变游戏类型 只需改变此处 即可达到重用目的
        Play p=new Play();
        p.createGame(game);
        p.getGames();
    }
}

 

通过Spring提供给容器来完成类的初始化和装配工作。

通过new XmlBeanfactory("beans.xml")等方式即可启动容器。在容器启动时,Spring根据配置文件的描述信息,自动实例化Bean并完成依赖关系的建立,从容器中即可返回准备就绪的Bean实例,以被使用。
Spring通过一个配置文件就能把相应的类实例化,充分的使用了JAVA的反射机制。
Bean工厂(com.springframework.beans.factory.BeanFactory)是Spring框架最核心的接口,它提供了高级IoC的配置机制。

应用上下文(com.springframework.context.ApplicationContext)建立在Beanfactory基础上,提供了更多面向应用的功能。

一般称为BeanFactory为IoC容器,称ApplicationContext为应用上下文,又称Spring容器。
因此,BeanFactory是Spring框架的基础设置,面向Spring本身;ApplicationContext面向使用Spring框架的开发者,几乎所有的应用场合我们都直接使用ApplicationContext而非底层的BeanFactory。

ApplicationContext接口由BeanFactory派生而来,提供了更多面向实际应用的功能。如果配置文件在类路径下,用户可以通过ClassPathXmlApplicationContext实现类。在获取ApplicationContext实例后,就可以调用getBean返回Bean了。
ApplicationContext初始化和BeanFactory有一个重要区别:BeanFactory在初始化容器时,并未实例化Bean,直到第一次访问某个Bean时才实例目标Bean;而ApplicationContext则在初始化应用上下文时就实例化所有实例的Bean,所以ApplicationContext初始化时间较长。
ApplicationContext实现了ResourceLoader接口,拥有装载各种资源的能力。

几种获得BeanFactory的方法:
Resource resource=new ClassPathResource("applicationContext.xml");

//绝对路径的写法
Resource resource=new FileSystemResource("c:/applicationContext.xml");

ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");

BeanFactory bf=new ClassPathXmlApplicationContext("applicationContext.xml"

 

posted @ 2017-10-27 00:25  11·59  阅读(90)  评论(0编辑  收藏  举报