Spring

01.Spring框架的基本理解

关键字

        核心思想IOC\AOP、作用(解耦、简化),简单描述框架组成

Spring框架

        是一款轻量级的开发框架,核心思想是IOC(控制反转)AOP(面向切面编程)
为java应用程序开发提供组件管理服,用于组件之间的解耦以及简化第三方JavaEE中间件技术的使用(JMS、任务调度、缓存、ORM框架),是一个基础架构型的开发框架
Spring框架包括:

        IOC容器、 Validation数据校验、AOP面向切面编程
        Transactions事务管理、Spring JDBC、
        Spring MVC框架、以及各类第三方JavaEE中间件技术集成:

 02.Spring框架由哪些模块组成?

关键字官方文档描述,由7个模块组成
        Spring Framework根据官方文档的描述,主要包括以下常用5个模块:
1.Core:核心模块

        包括:IOC Containe(IOC容器),Events(事件通知机制),Resources(资源加载机制)i18n(国际化),Validation(数据校验),Data Binding(数据绑定),Type Conversion(类型转换),SpEL(Spring表达式),AOP(面向切面编程)


2.Testing:测试模块
        包括:Mock objects(测试模拟对像),TestContext Framework(测试框架),Spring MVC Test(用于测试Spring MVC),WebTestclient(用于测试Webclient、Restful、Webflux等)


3.Data Access:数据访问模块
        包括:Transactions(事务管理),DAO Support:(统一的Data Access object DAO模式封装),JDBC(Spring对于JDBC的操作封装),O/R Mapping(Spring对于对象关系映射框架的封装,例如Hibernate等框架)等;


4.Web Servlet:基于Servlet的Web应用开发
        包括:Spring MVC(Spring基于Mvc模式设计封装的web框架),
WebSocket Spring集成WebSocket,WebSocket是一个服务器与客户端双向通信的技术)等;


5.Integration:企业级系统集成模块(不同系统之间的交互集成)

        包括:Remoting(Spring用于在分布式系统中进行远程服务调用的通讯框架),JMS
(Spring集成各类ava消息中间件、Java消息服务【Java Message Service】,例如ActiveMQ等),Java Email(邮件发送),Tasks Scheduling(任务调度):

 03.Spring IOC的理解

关键字:IOC名词解释,作用是解耦,使用IOC容器管理项目组件之间的耦合关系
 

IOC(Inversion of Control,中文释义:控制反转)是Spring框架的核心思想之一,主要
用于解耦。IOC是指将创建对象的控制权转移给Spring框架进行管理。由Spring框架根据配置
文件或注解
等方式,创建bean对象并管理各个bean对象之间的依赖关系。使对象之间形成松散耦合的关系,实现解耦
        。控制:指的是对象创建(实例化、管理)的权力
        。反转:控制权交给外部环境(Spring框架\、IOC容器)

 04.Spring IOC容器的理解

关键字:IOC容器的作用、存储形式、初始化过程

        IOC通常被理解为IOC Container容器,IOC容器其实就是一个Map,key是每个bean对
象的ID,value是bean对象本身。IOC容器负责创建bean对象并管理bean的生命周期。并且根据配置好配置文件或注解,管理IOC容器中的每个bean,以及根据bean之间的依赖关系,完成bean之间的注入。

        IOC容器属于Spring Core模块,用来创建和管理Bean默认使用单例的方式将bean存储在DefaultListableBeanFactory类的beanDefinitionMap中(一个ConcurrentHashMap类型的Map集合);

        IOC容器使用ConcurrentHashMap集合存储了BeanDefinition对象,该对象封装了Spring对一个Bean所有配置信息,包括:类名,属性,构造方法参数,依赖,是否延迟加载,是否是单例等配置信息:

SpringIOC(控制反转):解耦
高内聚,低耦合编程思想
硬编码实现解耦:
    1.不要直接new对象,通过反射创建
    2.将需要创建的对象独立保存在资源文件中,动态加载

public class Test01 {
    public static void main(String[] args) throws SQLException, ClassNotFoundException, IOException {

        /**************高耦合******************/
//        DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
//        Connection root = DriverManager.getConnection("jdbc:mysql://localhost:3306/myschool?serverTimezone=GMT", "root", "123456");
//        System.out.println(root);

        /**************中耦合******************/

//        Class.forName("com.mysql.cj.jdbc.Driver()");
//        Connection root = DriverManager.getConnection("jdbc:mysql://localhost:3306/myschool?serverTimezone=GMT", "root", "123456");
//        System.out.println(root);

        /**************低耦合******************/
        //1.创建工具类
        Properties properties = new Properties();
        //2.加载文件
        InputStream inputStream=Test01.class.getClassLoader().getResourceAsStream("jdbc.properties");
        properties.load(inputStream);
        //3.通过key获取value
        String driver = properties.getProperty("msg1");
        String url = properties.getProperty("msg2");
        String name = properties.getProperty("msg3");
        String pwd = properties.getProperty("msg4");
        //4.获取连接
        Class.forName(driver);
        Connection root = DriverManager.getConnection(url,name,pwd);
        System.out.println(root);

    }
}
msg1=com.mysql.cj.jdbc.Driver
msg2=jdbc:mysql://localhost:3306/myschool?serverTimezone=GMT
msg3=root
msg4=123456

05. spring两大思想:

一.IOC控制反转 DI依赖注入
    IOC容器:springIOC搭建spring容器管理程序中的各个组件(class)让程序可以实现高内聚,低耦合的编程

二.AOP面向切面编程

spring环境搭建:
    1.坐标
    2.配置文件

springIOC:
    1.找到需要以解耦方式获取实例对象的类
    2.将需要spring管理的类注入spring容器
        <bean id="唯一标识" class="类"></bean>
    3.向spring容器中索取java实例(解耦)
            3.1.加载spring核心配置文件,获取spring容器对象
                ApplicationContext applicationContext =  new ClassPathXmlApplicationContext("beans.xml");
            3.2.获取对象
                Student student = (Student) applicationContext.getBean("student");

Demo02                  Demo3
beans.properties        beans.xml
key=value               id=class
BeansFactory            ApplicationContext
getBean(key)            getBean(id)

SpringIOC:
    ● IOC名词解释,作用是解耦,使用IOC容器管理项目组件之间的耦合关系
    ● IOC( Inversion of Control,中文释义:控制反转  ) 是Spring框架的核心思想之一,
        主要用于解耦。IOC是指将创建对象的控制权转移给Spring框架进行管理。由Spring框架根据配置文件或注解等方式,创建bean对象并管理各个bean对象之间的依赖关系。使对象之间形成松散耦合的关系,实现解耦;
      ○ 控制 :指的是对象创建(实例化、管理)的权力
      ○ 反转 :控制权交给外部环境(Spring框架、IoC容器)

关键字:
    BeanFactory(父)
    ApplicationContext(子)

        ClassPathXmlApplicationContext============>通过加载主配置文件的相对路径,获取spring容器
        FileSystemXmlApplicationContext===========>通过加载主配置文件的绝对路径,获取spring容器
        AnnotationConfigApplicationContext========>加载配置类,获取spring容器

dao=com.UserDaoImp
service=com.UserServiceImp

public interface IUserController {
    public void save();
}



public class UserController implements IUserController{
    //高耦合
//    IUserService service = new UserServiceImp();

    //低耦合
    IUserService service = (IUserService) BeansFactory.getBean("service");

    @Override
    public void save() {
        System.out.println("===controller的新增方法===");
        service.save();
    }

}



public interface IUserDao{
    public void save();

}

public class UserDaoImp implements IUserDao {
    @Override

    public void save() {
        System.out.println("===dao的新增方法===");
    }

}


public interface IUserService {
    public void save();

}

public class UserServiceImp implements IUserService{
//    IUserDao dao = new UserDaoImp();

    //低耦合
    IUserDao dao = (IUserDao) BeansFactory.getBean("dao");

    @Override
    public void save() {
        System.out.println("===service的新增方法===");
        dao.save();
    }

}



public class BeansFactory {
    public static Object getBean(String key){

        try {
            //1.创建工具类
            Properties properties = new Properties();
            //2.加载文件
            InputStream inputStream = Test01.class.getClassLoader().getResourceAsStream("beans.properties");
            properties.load(inputStream);
            //3.通过key获取value
            String classPath = properties.getProperty(key);
            return Class.forName(classPath).newInstance();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException exception) {
            exception.printStackTrace();
        }
        return null;

    }
}


public class Test01 {
    public static void main(String[] args) {
        IUserController controller =  new UserController();
        controller.save();
    }
}

public interface IUserDao{
    public void save();

}

public class UserDaoImp implements IUserDao {
    @Override

    public void save() {
        System.out.println("===dao的新增方法===");
    }

}



public class Test01 {
    public static void main(String[] args) {
        //1.加载spring核心配置文件,获取spring容器对象
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");

        //2.获取对象
        Student student= (Student) applicationContext.getBean("student");
        System.out.println(student);

        IUserDao dao=(IUserDao) applicationContext.getBean("dao");
        dao.save();

        Date date=(Date)applicationContext.getBean("date");
        System.out.println(date);

    }

}



 <!-- 注入类-->
    <bean id="student" class="com.xn.pojo.Student"></bean>
    <bean id="dao" class="com.UserDaoImp"></bean>
    <bean id="date" class="java.util.Date"></bean>

  • 28
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值