• Spring


    Spring

    一、Spring框架概述

    在这里插入图片描述

    1.spring是什么

    1. spring是一个轻量级开源的JavaEE框架

    2. spring可以解决企业应用开发的复杂性

    3. spring有两个核心部分:IOC和AOP

      IOC:控制反转,把创建对象过程交给spring进行管理。

      AOP:面向切面,不修改源代码进行功能增强。

    2.spring的特点

    1. 方便解耦,简化开发

    2. AOP编程的支持

    3. 声明式事务的支持

    4. 方便程序的测试

    5. 方便集成各种优秀框架

    6. 降低Java EE API的使用难度

    7. Java 源码是经典学习范例

    3.spring入门案例

    1. 创建maven工程
    2. 导入spring的依赖
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>springartifactId>
      <version>2.5.6version>
    dependency>
    
    1. 创建实体类对象并且编写方法
    
    public class User {
       
      public  void sout(){
       
          System.out.println ("你好,spring5");
      }
    }
    
    1. 创建spring配置文件 bean1.xml
    
    <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">
    
             
      <bean id="user" class="com.spring.User">bean>
    
     beans>
    
    1. 进行测试代码编写
    public class UserTest {
       
      @Test
      public  void testSout(){
       
          //1,加载spring配置文件
          ApplicationContext context=
              new ClassPathXmlApplicationContext ("bean1.xml");
          //2.获得配置创建的对象
          User user = (User) context.getBean ("user", User.class);
          System.out.println (user);
    
         user.sout ();
    
     }
    }
    

    二、IOC容器

    1. IOC底层原理

    1.1 什么是IOC

    1. IOC就是控制反转,把对象创建和对象之间的调用过程交给spring进行管理
    2. 使用IOC的目的:为了耦合度降低
    3. 做入门案例就是IOC的实现

    1.2 IOC底层原理

    IOC底层原理主要用到了:xml解析、工厂模式、反射

    工厂模式

    在这里插入图片描述

    IOC过程

    在这里插入图片描述

    2. IOC接口

    IOC思想基于IOC容器完成,IOC容器底层就是对象工厂

    BeanFactory

    Spring提供IOC容器实现两个方式:(两个接口)

    • BeanFactory :IOC容器基本实现,是spring内部使用的接口,不提供开发人员进行使用。
      • 加载配置文件的时候不会创建对象,在获取(使用)才去创建对象
    • ApplicationContext :BeanFactory 接口的子接口,提供更多更强大的功能,一般由开发人员进行使用。
      • 加载配置文件的时候就会把配置文件对象进行创建

    ApplicationContext

    ApplicationContext 接口的主要实现类:
    在这里插入图片描述

    3. IOC操作Bean管理

    3.1 什么是bean管理?

    bean管理指的是两个操作:

    1. spring 创建对象
    2. spring 注入属性

    3.2 Bean管理的两种方式

    • 基于XML
    • 基于注解

    3.3 IOC操作Bean管理(基于XML方式)

    3.3.1 基于XML方式创建对象bean标签

    在spring配置文件中,使用bean标签,标签里面添加对应的属性,就可以实现对象创建。

    注意:创建对象时,默认也是执行无参构造方法完成对象的创建

    bean标签的属性介绍:

    • id 属性: 唯一标识
    • class 属性:类的全路径(包类路径)
    
    <bean id="user" class="com.spring.User">bean>
    
    3.3.2 基于XML方式注入属性

    DI:依赖注入,就是注入属性

    ①方法一:使用set注入
    1. 创建实体类的属性,设置set方法
        package com.spring;
    
        public class Book {
       
            private  String bookname;
            private  int id;
    
            //方式一:set注入
            public void setBookname(String bookname) {
       
                this.bookname = bookname;
            }
    
            public void setId(int id) {
       
                this.id = id;
            }
    
            @Override
            public String toString() {
       
                return "Book{" + "bookName='" + bookname + '\'' + ", id=" + id + '}';
            }
        }
    
    
    1. 在spring配置下的 bean 标签下使用 property 完成属性注解
    
    <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">
    
        
    
        <bean id="book" class="com.spring.Book">
            
            <property name="bookname" value="活着">property>
            <property name="id" value="1">property>
    
        bean>
    
    beans>
    
    1. 测试
    @Test
    public  void BookTest(){
       
        //1,加载spring配置文件
        ApplicationContext context=
            new ClassPathXmlApplicationContext ("bean1.xml");
        //2.获得配置创建的对象
        Book book = context.getBean ("book", Book.class);
        System.out.println (book.toString ());
    }
    
    ②方法二:使用有参数的构造器进行注入
    1. 创建实体类对象,并设置有参构造器
    package com.spring;
    
    public class Person {
       
        private  String pname;
        private  int age;
    
        //有参构造器
        public Person(String pname, int age) {
       
            this.pname = pname;
            this.age = age;
        }
    
        @Override
        public String toString() {
       
            return "Person{" + "pname='" + pname + '\'' + ", age=" + age + '}';
        }
    }
    
    
    1. 在spring的配置中的bean标签下,使用constructor-arg 属性进行注入
    
    <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">
        
    
    
        <bean id="person" class="com.spring.Person">
            
            <constructor-arg index="0" value="霸王花">constructor-arg>
            <constructor-arg index="1" value="18">constructor-arg>
        bean>
    
    beans>
    
    1. 测试
    @Test
    public  void personTest(){
       
        //1,加载spring配置文件
        ApplicationContext context=
            new ClassPathXmlApplicationContext ("bean1.xml");
        //2.获得配置创建的对象
        Person person = context.getBean ("person", Person.class);
        System.out.println (person.toString ());
    }
    
    ③p名称空间注入(了解)

    使用p名称空间注入,可以简化基于XML配置方式

    1. 添加p名称空间在配置文件中
    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    
           xmlns:p="http://www.springframework.org/schema/p"
    
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        
        <bean id="book" class="com.spring.Book" p:bookname="海绵宝宝" p:id="9">
    
        bean>
    beans>
    
    1. 进行属性注入,在bean标签里面进行操作

    2. 测试同①

    3.3.3XML注入其他类型属性
    1. 字面值
    2. 注入属性——外部bean
    3. 注入属性——内部bean和级联赋值
    ①字面量

    字面量:

    • 空值null
    <property name="bookname">
        <null/>
    property>
    
    • 属性值包含特殊符号

      特殊符号:
      1. 把<>进行转义
      2. 把特殊符号内容写进CDATA

            <property name="bookname" >
                   <value>
                     >]]>
                   value>
                  property>
    
    ②注入属性——外部bean

    注入属性——外部bean

    1. 创建两个类service类和dao类
    package dao;
    public class User implements UserDao {
       
        @Override
        public void show() {
       
            System.out.println ("我是user");
        }
    }
    
    
    package dao;
    
    public interface UserDao {
       
        public  void show();
    }
    
    
    1. 在service调用里面的方法
    package service;
    
    import dao.UserDao;
    
    public class UserService {
       
    
        private UserDao userdao;
    
        public void setUserdao(UserDao userdao) {
       
            this.userdao = userdao;
        }
    
        public  void test(){
       
            System.out.println ("我是userservice");
            userdao.show ();
    
        }
    }
    
    1. 在spring的配置文件中进行配置
        
        <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">
         <bean name="userService" class="service.UserService">
        
             <property name="userdao" ref="userDao">property>
         bean>
         <bean name="userDao" class="dao.User">bean>
    
        beans>
    
    1. 测试
        @Test
        public  void testSout(){
       
            //1,加载spring配置文件
            ApplicationContext context=
                new ClassPathXmlApplicationContext ("bean1.xml");
            //2.获得配置创建的对象
            UserService userService = context.getBean ("userService",UserService.class);
            userService.test ();
        }
    
    ③注入属性——内部bean
    1. 创建两个类Emp类和Dept类
        package bean;
    
        public class Emp {
       
            private  String name;
            private  int age;
    
            private  Dept dept;
    
            public void setDept(Dept dept) {
       
                this.dept = dept;
            }
    
            public void setName(String name) {
       
                this.name = name;
            }
    
            public void setAge(int age) {
       
                this.age = age;
            }
    
            @Override
            public String toString() {
       
                return "Emp{" + "name='" + name + '\'' + ", age=" + age + ", dept=" + dept + '}';
            }
        }
    
    
    package bean;
    
    public class Dept {
       
        private  int did;
    
        public void setDid(int did) {
       
            this.did = did;
        }
    
        @Override
        public String toString() {
       
            return "Dept{" + "did=" + did + '}';
        }
    }
    
    
    1. 在spring的配置文件中进行配置
        
        <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">
            <bean id="emp" class="bean.Emp">
                <property name="name" value="霸王花">property>
                <property name="age" value="18">property>
    
                
                <property name="dept">
                    <bean id="dept" class="bean.Dept">
                        <property name="did" value="9">property>
                    bean>
                property>
                 bean>
    
    1. 测试
    @Test
    public  void test(){
       
        //1,加载spring配置文件
        ApplicationContext context=
            new ClassPathXmlApplicationContext ("bean2.xml");
        //2.获得配置创建的对象
        Emp emp = context.getBean ("emp", Emp.class);
        System.out.println (emp.toString ());
    }
    
    ④注入属性——级联赋值

    方法一:

    
    <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">
    
        <bean id="emp" class="bean.Emp">
            <property name="name" value="霸王花">property>
            <property name="age" value="18">property>
    
    
            
            <property name="dept" ref="dept">property>
        bean>
    
        <bean id="dept" class="bean.Dept">
            <property name="did" value="0">property>
        bean>
    beans>
    

    方法二:

    需要提前在emp类中生成dept的get方法

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLoc
  • 相关阅读:
    Related to the third param of function “sort“ & Lambda of Cpp
    技能大赛训练题:登录安全加固
    Pytorch基础知识与GPU并行简介
    2023数维杯数学建模竞赛D题思路+模型+代码+论文
    AI生成内容(AIGC)技术:革新创作与挑战未来
    ubuntu20添加一个硬盘
    0915练习
    使用jconsole监控SpringbootJVM(JDK11)
    Django 中admin页面的css样式无法加载的终极解决方案
    别对蔚来手机抱太大希望
  • 原文地址:https://blog.csdn.net/A_Pluto_i/article/details/126964095