渚漪Day31——SSM【Spring01】

1、Spring

1.1、简介

目的:解决企业级开发的复杂性

SSH:Struct2 + Spring + Hibernate

SSM:SpringMVC + Spring + Mybatis

官方文档地址:https://docs.spring.io/spring/docs/5.2.6.RELEASE/spring-framework-reference/

官方下载地址:https://repo.spring.io/release/org/springframework/spring/

maven包

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.6.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.6.RELEASE</version>
</dependency>


1.2、优点

  • 开源免费的容器(框架)!
  • 轻量级的、非入侵式框架!
  • 控制反转(IOC),面向切面编程(AOP)!(重点)
  • 支持事务的处理

1.3、组成

1.4、拓展

  • Spring Boot
    • 基于Spring Boot可以快速开发单个微服务
    • 快速开发
  • Spring Cloud
    • 基于SpringBOot实现的

学习SpringBoot的前提是学会Spring

2、IOC理论推导

IOC思想:【控制反转】

利用set方法,动态实现值(对象)的注入,而其它层的代码只管业务的完成实现,不管对象的创建。

【对象由test(用户)创建】而service层只负责set注入

使用面向对象编程,对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建有程序自己控制,控制反转后,将对象的创建转交给第三方。

IoC也称为依赖注入(DI)。它是一个过程,对象仅通过构造函数、工厂方法的参数 或 在从工厂方法构造或返回对象实例后,在对象实例上设置的属性来定义它们的依赖关系(即与它们一起工作的其他对象)。

3、HelloSpring

hello基础类

package com.ijuy.pojo;

public class hello {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Hello "+name;
    }
}

配置

<?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="hello" class="com.ijuy.pojo.hello">
        <property name="name" value="Spring"/>
    </bean>

</beans>

实现

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        //获取spring的上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        Object hello = context.getBean("hello");

        System.out.println(hello.toString());
    }
}

4、IOC创建对象的方式

  • 下标赋值 index=“0” value=“渚漪”
  • 类型 type=“java.lang.String” value=“渚漪”
  • 直接通过参数名 name=“name” value=“渚漪” 【最简单】

5、Spring配置

5.1、别名

<alias name = "user" alias="newUser"/>

5.2、Bean

id 唯一标识符 class 对象 name 别名

<bean id="user" class="com.ijuy.pojo.user" name="name2,name3"/>

5.3、import

合并多个配置,导入为一个

6、DI依赖注入

6.1、构造器注入

6.2、Set方式注入【重点】

  • 依赖注入:set注入!
    • 依赖:bean对象的创建依赖于容器
    • 注入:bean对象中的所有属性,由容器注入

pojo类

public class Student {
    private String name;
    private Address address;//自定义类
    private String[] books;
    private List<String> hobbies;
    private Map<String,String> card;
    private Set<String> games;
    private Properties info;
    private String girlFriend;
}

测试类

@Test
public void test1() {
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    Student student = (Student) context.getBean("student");
    System.out.println(student.toString());
}

beans.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">

    <bean id="address" class="com.ijuy.pojo.Address">
        <property name="address" value="家"/>
    </bean>
    <bean id="student" class="com.ijuy.pojo.Student">
        
        <property name="name" value="渚漪"/>
        
        <property name="address" ref="address"/>
        
        <property name="books">
            <array>
                <value>book1</value>
                <value>book2</value>
                <value>book3</value>
                <value>book4</value>
            </array>
        </property>

        <property name="hobbies" >
            <list>
                <value>兴趣1</value>
                <value>兴趣2</value>
                <value>兴趣3</value>
                <value>兴趣4</value>
            </list>
        </property>
        
        <property name="card">
            <map>
                <entry key="身份证" value="123123123"/>
                <entry key="银行卡" value="123123"/>
            </map>
        </property>
        
        <property name="games" >
            <set>
                <value>csgo</value>
                <value>dota</value>
            </set>
        </property>
        
        <property name="girlFriend">
            <null/>
        </property>
        
        <property name="info">
            <props>
                <prop key="学号">1841600000</prop>
            </props>
        </property>
        
    </bean>
</beans>

结果

Student{
    name='渚漪',
    address=Address{address='家'},
    books=[book1, book2, book3, book4],
    hobbies=[兴趣1, 兴趣2, 兴趣3, 兴趣4],
    card={身份证=123123123, 银行卡=123123},
    games=[csgo, dota],
    info={学号=1841600000},
    girlFriend='null'
}

6.3、拓展方式注入

原文地址:https://www.cnblogs.com/ijuysama/p/12926698.html