码迷,mamicode.com
首页 > 编程语言 > 详细

Spring框架之控制反转和依赖注入

时间:2015-08-13 22:00:29      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:

学Spring框架必须理解控制反转和依赖注入。下面各自举一个例子,来说明。

 

IOC(控制反转):应用本身创建和维护的依赖对象;现在交由外部容器(Spring)来创建和维护;这个控制权的转移;
就叫做控制反转。

第一步:配置applicationContextcreateproject.xml和applicationcontext.xml(总体)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
        
     <bean id="create" class="createObject.CreateHelloWorld"  factory-method="create">
     
     
     </bean>  
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- 引用其他配置文件 -->

    <import resource="createObject/applicationContextcreateproject.xml"/>
</beans>
package createObject;

public class CreateHelloWorld {
    //静态工厂
    public static HelloWord create(){
        
        return new HelloWord();
    }

}

 

package createObject;

public class HelloWord {
    public void hellow(){
        System.out.print("hellow spring");
    }

}

测试类:

package createObject;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class texthell{
    public static void main(String[] args) {

    // TODO Auto-generated method stub
   //加载配置文件
       ApplicationContext context=new ClassPathXmlApplicationContext("applicationcontext.xml");
       HelloWord te=(HelloWord) context.getBean("create");
       te.hellow();
       
}
}

效果图:

技术分享

DI(依赖注入):(控制反转的一个实现)在程序运行期间;动态的将依赖对象注入到组件当中(创建对象)new的工作交给spring来做;前提留接口;set get方法

第一步:配置Springapplicationcontext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="wanlix" class="com.Test.test">
<property name="name" value="张嘎"/>
<property name="speak" value="一天不打黄鼠狼,就憋屈"/>

</bean>

</beans>

第二步:测试类

package com.Test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    public String name;
    public String speak;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSpeak() {
        return speak;
    }
    public void setSpeak(String speak) {
        this.speak = speak;
    }
    public static void main(String[] args) {
        
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationcontext.xml");
        
        test te=(test) context.getBean("wanlix");
        System.out.print(te.getName()+"说:"+te.getSpeak());
    }

}

效果图:

技术分享

Spring框架之控制反转和依赖注入

标签:

原文地址:http://www.cnblogs.com/wlx520/p/4728300.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!