zoukankan      html  css  js  c++  java
  • spring

    介绍

    spring一般是指spring framework或者spring全家桶。
    spring的核心技术为ioc和aop。
    参考
    官方文档

    IOC

    IOC,全称Inversion of Control,控制反转。经典的,我们用关键字new去主动创建对象,而将创建对象的工作交给容器再根据需要请求获取的方式称之为控制反转,该容器称为IOC容器。
    1、简单使用
    (1)jar包:spring-context
    (2)配置

    <?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
          <bean id="..." class="...">
    </beans>
    

    (3)代码
    User.java类:id、username和password
    Test.java

    自spring5起,需要jdk8+的支持

    2、容器
    (1)BeanFactory:基本的bean对象管理

    • XmlBeanFactory:BeanFactory的重要实现类
      (2)ApplicationContext:BeanFactory的子接口,提供了企业级的支持
    • WebXmlApplicationContext:用于网络加载
    • FileSystemXmlApplicationContext:参数为配置文件的系统路径
    • ClassPathXmlApplicationContext:参数为项目的相对路径

    实例化容器(可以一次性加载多个配置文件)

    ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
    

    也可以在配置文件中加载其它配置文件

    <beans>
        <import resource="services.xml"/>
        <import resource="resources/messageSource.xml"/>
        <import resource="/resources/themeSource.xml"/>
    
        <bean id="bean1" class="..."/>
        <bean id="bean2" class="..."/>
    </beans>
    

    3、生命周期与作用域范围
    配置scope来控制对象的作用域范围,默认为singleton

    • singleton:单例,在容器加载时创建。当应用结束,容器销毁时被销毁。
    • prototype:多例,每次请求时创建,对象使用结束时销毁。
    • request
    • session

    4、创建bean

    5、注入

    6、常用注解
    基于注解的容器配置

    <?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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:annotation-config/>
    
    </beans>
    
    • @Required
    • @Autowired
    • @Qualifier
    • @Primary
    • @Resource

    自动检测类并注册bean定义

    <?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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            https://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:component-scan base-package="org.example"/>
    
    </beans>
    
    • @Component
  • 相关阅读:
    web版仿微信聊天界面|h5仿微信电脑端案例开发
    h5仿微信聊天(高仿版)、微信聊天表情|对话框|编辑器
    原生wcPop.js消息提示框(移动端)、内含仿微信弹窗效果
    旅行app(游记、攻略、私人定制) | 顺便游旅行H5移动端实例
    【h5+c3】web前端实战项目、快装webapp手机案例源码
    h5区块链项目实战
    css3波纹特效、H5实现动态波浪
    H5移动端项目案例、web手机微商城实战开发
    HTML5仿微信聊天界面、微信朋友圈实例
    Android 给服务器发送网络请求
  • 原文地址:https://www.cnblogs.com/heibaimao123/p/13787268.html
Copyright © 2011-2022 走看看