zoukankan      html  css  js  c++  java
  • Spring技术详解

    先综述一下,看到的很多资料总结出来的东西,如果有错或者不全,还希望提出宝贵意见。

    1.1  什么是spring

    Spring是一个开源框架。Spring的核心是控制反转(IoC)和面向切面(AOP)。简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架。

    轻量级:与EJB对比,依赖资源少,销毁的资源少。

    分层: 一站式,每一个层都提供的解决方案

               web层:struts,spring-MVC

               service层:spring

               dao层:hibernate,mybatis , jdbcTemplate  --> spring-data

    1.1  spring核心

     Spring的核心是控制反转(IoC面向切面(AOP

    IOC

    目标类

      提供UserService接口和实现类,获得UserService实现类的实例.之前开发中,直接new一个对象即可。

      学习spring之后,将由Spring创建对象实例--> IoC 控制反转(Inverse of  Control)。之后需要实例对象时,从spring工厂(容器)中获得,需要将实现类的全限定名称配置到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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
                                  http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!-- 配置service 
            <bean> 配置需要创建的对象
                id :用于之后从spring容器获得实例时使用的
                class :需要创建实例的全限定类名
        -->
        <bean id="userServiceId" class="com.itheima.a_ioc.UserServiceImpl"></bean>
    </beans>
    public void demo02(){
            //从spring容器获得
            //1 获得容器
            String xmlPath = "com/itheima/a_ioc/beans.xml";
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
            //2获得内容 --不需要自己new,都是从spring容器获得
            UserService userService = (UserService) applicationContext.getBean("userServiceId");
            userService.addUser();
            
        }

    DI

     DI Dependency Injection ,依赖注入

               is a :是一个,继承。

               has a:有一个,成员变量,依赖。

                         class B {

               private A a;   //B类依赖A类

            }

               依赖:一个对象需要使用另一个对象

               注入:通过setter方法进行另一个对象实例设置。

     例如:

               class BookServiceImpl{

            //之前开发:接口 = 实现类  (service和dao耦合)

                         //private BookDao bookDao = new BookDaoImpl();

                        //spring之后 (解耦:service实现类使用dao接口,不知道具体的实现类)

                         private BookDao bookDao;

                         setter方法

       }

               模拟spring执行过程

               创建service实例:BookService bookService = new BookServiceImpl()                     -->IoC  <bean>

               创建dao实例:BookDao bookDao = new BookDaoImple()                                         -->IoC

               将dao设置给service:bookService.setBookDao(bookDao);                                         -->DI   <property>

  • 相关阅读:
    Hibernate unsaved-value 属性
    ResulsetHandler九个实现类
    Introspector内省和反射的区别.
    数据库表、字段命名规范
    Linux 时间同步 ntpdate
    http升级https改造方案
    org.apache.tomcat.util.net.NioEndpoint,打开的文件过多
    kafka常用命令
    elasticsearch.yml 配置说明
    getDate() 各种时间格式
  • 原文地址:https://www.cnblogs.com/Hennessy-Road/p/6539735.html
Copyright © 2011-2022 走看看