spring细节讨论

来源:互联网 发布:gis软件开发工程师 编辑:程序博客网 时间:2024/04/30 04:31

一 传统的方法和使用spring的方法
使用spring,没有new对象,我们把创建对象的任务交给spring框架。

二 spring的运行原理图

三 案例
1 UserService
package com.service;public class UserService {        private String name;                private BybService byeService;        public BybService getByeService() {                return byeService;        }        public void setByeService(BybService byeService) {                this.byeService = byeService;        }        public String getName() {                return name;        }        public void setName(String name) {                this.name = name;        }                public void sayHello(){                System.out.println("hello "+name );                byeService.sayBye();        }        }


2 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:context="http://www.springframework.org/schema/context";                xmlns:tx="http://www.springframework.org/schema/tx";                xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd";><!-- 在容器文件中配置bean(service/dao/domain/action/数据源) --><!-- bean元素的作用是,当我们的spring框架加载时候,spring就会自动的创建一个bean对象,并放入内存        UserService userSerivce=new UserService();        userSerivce.setName("韩顺平");        --><bean id="userService" class="com.service.UserService">        <!-- 这里就体现出注入的概念. -->        <property name="name">                <value>韩顺平</value>        </property>        <property name="byeService" ref="bybService">        </property></bean><bean id="bybService" class="com.service.BybService">        <!-- 这里就体现出注入的概念. -->        <property name="name" value="小明">        </property></bean></beans>


3 BybService
package com.service;public class BybService {        private String name;        public String getName() {                return name;        }        public void setName(String name) {                this.name = name;        }                public void sayBye(){                System.out.println("bye"+name);        }}


4 Test
package com.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.service.BybService;import com.service.UserService;import com.util.ApplicaionContextUtil;public class Test {    /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub        //我们先使用传统的方法,来调用UserService的 sayHello方法//        UserService userService=new UserService();//        userService.setName("顺平");//        userService.sayHello();                //我们现在使用spring来完成上面的任务        //1.得到spring 的applicationContext对象(容器对象)            //UserService us=(UserService) ac.getBean("userService");        //us.sayHello();        ((UserService)ApplicaionContextUtil.getApplicationContext()        .getBean("userService")).sayHello();                //从ac[代表applicatonContext容器]中        //BybService bybService=(BybService) ac.getBean("bybService");        //bybService.sayBye();            }}



ApplicaionContextUtil
package com.util;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;final public class ApplicaionContextUtil {    private static ApplicationContext ac=null;        private ApplicaionContextUtil(){            }        static{        ac=new ClassPathXmlApplicationContext("applicationContext.xml");    }    public static ApplicationContext getApplicationContext(){        return ac;    }    }


6 运行结果
hello 韩顺平
bye小明

四 案例总结
spring实际上是一个容器框架,可以配置各种bean(action/service/domain/dao),并且可以维护bean与bean的关系,当我们需要使用某个bean的时候,我们可以getBean(id),使用即可。
把Applicationcontext做成一个单例的。

五 ioc是什么
ioc,即控制反转,inverseofcontroll,所谓控制反转就是把创建对象(bean),和维护对象(bean)的关系的权利从程序中转移到spring的容器(applicationContext.xml),而程序本身不再维护。
DI是什么?
di,依赖注入,dependency injection,实际上di和ioc是同一个概念,spring设计者认为di更准确表示spring核心技术
学习框架,最重要的就是学习各个配置.
原创粉丝点击