Spring5学习总结这一篇就够了

本文详细介绍了Spring框架的核心组件,包括SpringCore、SpringContext和SpringWeb等,并深入讲解了控制反转(IOC)的概念,通过代码示例展示了如何创建和管理Bean。此外,还探讨了依赖注入(DI)的实现方式,以及Bean的作用域。最后,提到了Spring的AOP编程和Spring与Mybatis的整合,以及事务管理在Spring中的应用。
摘要由CSDN通过智能技术生成

前言

Spring 是 Java EE 编程领域的一款轻量级的开源框架,由被称为“Spring 之父”的 Rod Johnson 于 2002 年提出并创立,它的目标就是要简化 Java 企业级应用程序的开发难度和周期。

一、Spring概述

1、Spring 是轻量级的开源的 JavaEE 框架
2、Spring 可以解决企业应用开发的复杂性
3、Spring 有两个核心部分:IOC 和 AOP
(1)IOC:控制反转,把创建对象过程交给 Spring 进行管理
(2)Aop:面向切面,不修改源代码进行功能增强

1.1 Spring 框架的组成

在这里插入图片描述

1.「Spring Core」:Spring核心,它是框架最基础的部分,提供IOC和依赖注入DI特性。

2.「Spring Context」:Spring上下文容器,它是 BeanFactory 功能加强的一个子接口。

3.「Spring Web」:它提供Web应用开发的支持。

4.「Spring MVC」:它针对Web应用中MVC思想的实现。

5.「Spring DAO」:提供对JDBC抽象层,简化了JDBC编码,同时,编码更具有健壮性。

6.「Spring ORM」:它支持用于流行的ORM框架的整合,比如:Spring + Hibernate、Spring + iBatis、Spring + JDO的整合等

7.「Spring AOP」:即面向切面编程,它提供了与AOP联盟兼容的编程实现。

1.2 Spring 框架优点

方便解耦,简化开发: Spring 就是一个大工厂,可以将所有对象的创建和依赖关系的维护交给 Spring 管理。

方便集成各种优秀框架: Spring 不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如 Struts2、Hibernate、MyBatis 等)的直接支持。

降低 Java EE API 的使用难度: Spring 对 Java EE 开发中非常难用的一些 API(JDBC、JavaMail、远程调用等)都提供了封装,使这些 API 应用的难度大大降低。

方便程序的测试: Spring 支持 JUnit4,可以通过注解方便地测试 Spring 程序。

AOP 编程的支持: Spring 提供面向切面编程,可以方便地实现对程序进行权限拦截和运行监控等功能。

声明式事务的支持: 只需要通过配置就可以完成对事务的管理,而无须手动编程。

1.3 Spring 框架缺点

整合其他框架配置众多且烦琐,人称 " 配置地狱 “ 这为 SpringBoot 框架进一步简化开发埋下伏笔。

二、Spring 的控制反转(IOC)容器

IOC(底层原理:xml解析 反射技术 工厂模式)
IOC意思是控制反转,它是一种思想而不是一种实现,他为什么叫控制反转呢,在传统代码中,我们某些类内部主动创建对象,从而导致类与类之间高耦合,使用IOC将类与类的依赖关系写在配置文件中,程序在运行时根据配置文件动态加载依赖的类由IOC容器去实例化对象,这些在IOC容器中的对象叫做Bean,然后由IOC容器控制这些Bean的创建,销毁,降低类与类之间的耦合度。简而言之,就是我们传统开发过程中创建对象的权力掌握在程序员手中,现在这个权力赋给ioc容器创建,管理,销毁bean对象。个人认为:所谓的控制反转就是获得依赖对象的方式反转了,由传统的面向对象编程,转变为面向bean编程。具体是实现可以使用xml配置,也可以是使用注解
在这里插入图片描述
DI:依赖注入,将对应的属性注入到具体的对象中@Autowired @Resource set方法 populateBean方法来完成属性的注入。

获取spring上下文对象:
ApplicationContext context = new ClassPathXmlApplicationContext(“beans.xml”);
我们的对象现在都在spring管理了 我们要使用的话直接在里面取就可以 Hello hello = (Hello) context.getBean(“hello”);

2.1 代码实现步骤

1.引入依赖

 <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>
    </dependencies>

2.编写类

@NoArgsConstructor
@AllArgsConstructor
@Data
public class Hello {
    private String str;
    private int id;
}

3.编写配置文件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">

<!--
  使用spring来创建对象,在spring这些都是bean
  类型 变量名 = new 类型;
  Hello hello = new Hello;
  bean=id  new Hello;
  bean= 对象 new Hello();
  id=变量名 class =new的对象
-->
   <!--hello相当于变量名 class全路径名称相当于new的对象-->
    <bean id="hello" class="com.kuang.pojo.Hello">
        <!--给对象属性赋值-->
        <property name="str" value="spring"/>
        <!--给对象的属性赋值-->
        <property name="id" value="18"/>
    </bean>

    <bean id="hello2" class="com.kuang.pojo.Hello">
        <!--给对象属性赋值-->
        <property name="str" value="springs"/>
        <!--给对象的属性赋值-->
        <property name="id" value="19"/>
    </bean>
    
</beans>

4.测试

public class MyTest {
    public static void main(String[] args) {
        //获取spring的上下文对象    固定的用xml加载必须写这个
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
       //我们的对象现在都在spring管理了 我们要使用的话直接在里面取就可以
        Hello hello = (Hello) context.getBean("hello");
        Hello hello2 = (Hello) context.getBean("hello2");

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

    }
}

5.输出结果

Hello(str=spring, id=18)
Hello(str=springs, id=19)

Process finished with exit code 0

2.2 IOC 创建对象的方式

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
     private String name;

     public void show(){
         System.out.println("name="+name);
     }
}
@Data
public class UserT {
    private String name;

    public UserT() {
        System.out.println("UserT被创建!");
    }

    public void show(){
        System.out.println("name="+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
       http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
   ioc创建对象的方式
   1使用无参构造 默认
   2假设要使用有参构造

<!- 第一种下标赋值-->
<!--    <bean id="user" class="com.kuang.pojo.User">-->
<!--    <property name="name" value="haha"/>-->

<!--        <constructor-arg index="0" value="java"/>-->
<!--    </bean>-->
<!-- 第二种通过类型创建 不建议使用 -->
<!--<bean id="user" class="com.kuang.pojo.User">-->
<!--    <constructor-arg type="java.lang.String" value="qinjiang"/>-->
<!--</bean>-->

<!--    第三种直接通过参数名-->
<bean id="user" class="com.kuang.pojo.User">
    <constructor-arg name="name" value="java"/>
</bean>
<!--    <bean id="userT" class="com.kuang.pojo.UserT">-->

<!--    </bean>-->
<!--    别名,如果添加了别名我们也可以通过他来获取对象-->
<!--    <alias name="user" alias="askId"/>-->
<!--
  id:bean的唯一标识符,相当于对象名
  class:bean对象所对应的类型
  name:也是起别名 而且name可以同时取多个别名
-->
    <bean id="userT" class="com.kuang.pojo.UserT" name="user2 u2,u3">
       <property name="name" value="kobe"/>
    </bean>
</beans>

输出结果:

UserT被创建!
name=java
name=kobe

Process finished with exit code 0

2.3 spring 中的 IOC 容器有哪些?有什么区别?

spring 主要提供了两种 IOC 容器,一种是 BeanFactory,还有一种是 ApplicationContext它们的区别就在于,BeanFactory 只提供了最基本的实例化对象和拿对象的功能,ApplicationContext 是继承了 BeanFactory 所派生出来的产物,是其子类,它的作用更加的强大,比如支持注解注入、国际化等功能。
在这里插入图片描述

2.4 那么 DI 又是什么?

DI 就是依赖注入,其实和 IOC 大致相同,只不过是同一个概念使用了不同的角度去阐述。DI 所描述的重点是在于依赖,我们说了IOC 的核心功能就是在于在程序运行时动态的向某个对象提供其他的依赖对象」,而这个功能就是依靠 DI 去完成的,比如我们需要注入一个对象 A,而这个对象 A 依赖一个对象 B,那么我们就需要把这个对象 B 注入到对象 A 中,这就是依赖注入。

spring 中有三种注入方式: 接口注入,构造器注入,set注入

2.5 DI 代码实现

1.编写实体类

@AllArgsConstructor
@NoArgsConstructor
@Data
public class Address {
    private String address;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobby;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

}

2.编写配置文件ApplicationContext.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.kuang.pojo.Address">
        <property name="address" value="西安"/>
    </bean>

    <bean id="student" class="com.kuang.pojo.Student">
        <!-- 第一种普通注入 value-->
        <property name="name" value="TCL"/>

        <!-- 第二种注入 ref-->
        <property name="address" ref="address"/>

        <!-- 数组注入 ref-->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>三国演义</value>
                <value>西游记</value>
                <value>水浒传</value>
            </array>
        </property>

        <!-- list注入-->
        <property name="hobby">
            <list>
                <value>听过</value>
                <value>看电影</value>
                <value>敲代码</value>
            </list>
        </property>

        <!-- map注入 -->
        <property name="card">
                <map>
                    <entry key="身份证" value="123456"/>
                    <entry key="银行卡" value="222222"/>
                </map>
        </property>
        
        <!-- set-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>DNF</value>
                <value>CF</value>
            </set>
        </property>
        <!-- null -->
        <property name="wife">
            <null/>
        </property>

        <!-- properties注入 -->
        <property name="info">
            <props>
                <prop key="学号">123456</prop>
                <prop key="性别"></prop>
                <prop key="姓名">小米</prop>
            </props>
        </property>
    </bean>
</beans>

3.测试

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
    }
}    

4.测试结果

Student(name=TCL, address=Address(address=西安), books=[红楼梦, 三国演义, 西游记, 水浒传], hobby=[听过, 看电影, 敲代码], card={身份证=123456, 银行卡=222222}, games=[LOL, DNF, CF], wife=null, info={学号=123456, 性别=, 姓名=小米})

Process finished with exit code 0

2.6 p c命名空间及 Bean 的作用域

1.编写实体类

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private String name;
    private int age;
}

2.编写配置文件userbeans.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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--p命名空间注入property  可以直接注入属性的值-->
    <bean id="user" class="com.kuang.pojo.User" p:name="科比" p:age="38"/>

    <!--p命名空间注入property -->
    <bean id="user2" class="com.kuang.pojo.User" c:age="18" c:name="Hello" scope="prototype"/>

</beans>

3.测试

@Test
    public void test2(){
       ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");

        User user = (User) context.getBean("user2");
        User user1 = (User) context.getBean("user2");
        System.out.println(user.toString());
        System.out.println(user==user1);

    }

4.测试结果

User(name=Hello, age=18)
false

Process finished with exit code 0

2.7 Bean自动装配

  • List item

三、Aop 面向切面编程

  • List item

四、Spring整合Mybatis

五,事务管理

  • List item

总结

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值