JavaEE--Spring_IOC01

一、Spring概念

    1、Spring是开源的轻量级框架

      2、Spring的核心主要为两部分:

        (1)aop:面向切面编程;扩展功能不需要修改源代码实现

        (2)ioc:控制反转;对象的创建不通new,而是交给Spring的配置来完成

      3、Spring是一站式框架

        Spring对JavaEE的三层结构中每一层都提供了不同的解决技术

        WEB层:SpringMVC

        service层:Spring的IOC

        DAO层:Spring的jdbcTemplate

二、IOC的底层原理

     1、IOC底层用到的技术

        (1)xml配置文件

        (2)dom4j解析xml

        (3)工厂设计模式

        (4)反射

     2、创建对象时的解耦和

         思想:高内聚低耦合

        (1)最开始使用new关键字创建对象

            public class UserService{

              public void addUser(){}

            }

            public class UserServlet{

              //通过new创建对象并调用方法

               UserService service=new UserService();

               service.add();

            }

          这种方式导致两个类耦合度过高,当Service发生变化时需要改动Servlet的代码不利于维护

        (2)使用工厂模式解耦和

            public class UserService{

              public void addUser(){}

            }

            public class ServiceFactory{

              public static UserService getService(){

                return new UserService();

              }

            }

            public class UserServlet{

              //通过工厂类获取对象

               UserService service=ServiceFactory.getService();

               service.add();

            }

            出现了工厂类和Servlet的新耦合

         (3)使用IOC获取对象原理

            public class UserService{

              public void addUser(){}

            }

            第一步:创建xml配置文件,

                <bean id="userService" class="类的全路径"/>

            第二步:创建工厂类,使用dom4j解析配置文件+反射

            public class ServiceFactory{

              public static UserService getService(){

                //使用dom4j解析配置文件,根据id值获取class属性值

                String classValue=“class属性值”;

                //使用反射创建类对象

                Class clazz=Class.forName(classValue);

                UserService userService=clazz.newInstance();

                return userService;

              }

            }

            public class UserServlet{

              //通过工厂类获取对象

               UserService service=ServiceFactory.getService();

               service.add();

            }

三、入门案例

   第一步:导入相关jar包

        做Spring最基本的功能时导入Beans、Core、Context、SpEL四个核心jar包就可以了

        还需要导入日志相关的jar包

    第二步:创建一个类;在里面建个方法

    第三步、创建Spring的配置文件,配置创建的类

        1、Spring核心配置文件的位置和命名不是固定的

          建议放在src下面,官方建议命名:applicationContext.xml  

        2、引入schema约束  

            <beans xmlns="http://www.springframework.org/schema/beans"

 

              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="
              http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

            </beans>

        3、配置对象的创建

            <bean id="userService" class="com.mystudy.ioc_01.UserService"></bean>

    第四步:案例代码

          public class UserService{

            public void add(){

              ...

            }

          }

          public class Test{

            public void IOCTest(){

              //加载配置文件

              ApplicationContext context=

                    new ClassPathXmlApplicationContext("applicationContext.xml");

              //根据id获取对象

              UserService service=(UserService)context.getBean("userService");

              service.add();

            }

          }

posted @ 2017-07-16 11:09  SugarMonster丶  阅读(137)  评论(0编辑  收藏  举报