文章目录

  • 前言
  • 一、Spring概述
    • 1.1 Spring 框架的组成
    • 1.2 Spring 框架优点
    • 1.3 Spring 框架缺点
  • 二、Spring 的控制反转(IOC)容器
    • 2.1 代码实现步骤
    • 2.2 IOC 创建对象的方式
    • 2.3 spring 中的 IOC 容器有哪些?有什么区别?
    • 2.4 那么 DI 又是什么?
    • 2.5 DI 代码实现
    • 2.6 p c命名空间及 Bean 的作用域
    • 2.7 Bean自动装配
  • 三、Aop 面向切面编程
  • 四、Spring整合Mybatis
  • 五,事务管理
  • 总结

前言

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/beanshttp://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/beanshttp://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=kobeProcess 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/beanshttp://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/beanshttp://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.测试

@Testpublic 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)
falseProcess finished with exit code 0

2.7 Bean自动装配

  • List item

三、Aop 面向切面编程

  • List item

四、Spring整合Mybatis

五,事务管理

  • List item

总结

  • List item

Spring5学习总结这一篇就够了相关推荐

  1. 学习javascript这一篇就够了超详细笔记(建议收藏)上

    学习javascript这一篇就够了超详细笔记(建议收藏)上 1.初识 计算机基础导读 编程语言 计算机基础 初识js 浏览器执行 js组成 js初体验-三种书写位置 js注释 js输入输出语句 2. ...

  2. 学习MyBatis3这一篇就够了

    目录 第一章 MyBatis3概述 1.1.概述 1.2.特点 1.3.对比 1.4.官网 1.5.下载 第二章 MyBatis3的增删改查 2.1.环境准备 2.2.创建工程 2.3.导入依赖 2. ...

  3. 强化学习入门这一篇就够了!!!万字长文

    强化学习 强化学习入门这一篇就够了万字长文带你明明白白学习强化学习... 强化学习入门这一篇就够了 强化学习 前言 一.概率统计知识回顾 1.1 随机变量和观测值 1.2 概率密度函数 1.3 期望 ...

  4. 学习FastDFS这一篇就够了

    目录 第一章 FastDFS简介 1.1.FastDFS的简介 1.2.FastDFS的发展历史 1.3.FastDFS的整体架构 1.4.FastDFS的使用用户 1.5.FastDFS的官方网址 ...

  5. caffe-源码学习——只看一篇就够了

    caffe-源码学习--只看一篇就够了 网络模型 说caffe代码难懂,其实关键点在于caffe中有很多基础的数学运算代码,如果能够对掌握这些数学运算,剩下的就是推公式了. 激活函数 sigmoid ...

  6. 干货!学习 Python 看这篇管够!!!

    文 | 潮汐 来源:Python 技术「ID: pythonall」 写在前面 各位朋友们大家好,时间飞逝,转眼咱们公众号运营 2 年了,这两年感谢各位忠实粉丝的陪伴,让我们能更有动力继续前行,也希望 ...

  7. 学习Redis这一篇就够了

    这里写目录标题 本文脑图 redis基本数据结构 本文脑图 前言 Redis核心对象 String类型 int SDS SDS与c语言字符串对比 String类型应用 Hash类型 字典 rehash ...

  8. Numpy 学习看这一篇就够了(整理+理解+精简)

    文章目录 一.Numpy的介绍 1.NumPy简介 2.NumPy的优点 3.NumPy的缺陷 二.Numpy 安装 1.安装 三.Ndarry对象 1.构造函数 np.array (默认深拷贝) n ...

  9. 学习SpringSecurity这一篇就够了

    目录 一.SpringSecurity 框架简介 1.1.概要 1.2.Spring Security到底能干什么? 1.3.常用术语 1.4.历史 1.5.同款产品对比 1.6.模块划分 二.Spr ...

最新文章

  1. 软件开发向大数据开发过渡_如果您是过渡到数据科学的开发人员,那么这里是您的最佳资源...
  2. 一个操作系统的实现(1):分析linux下如何运行一个执行文件
  3. Hi3516A开发--GV7601 硬件设计
  4. 高效能人士的七个习惯_《高效能人士的七个习惯》导图和读后感
  5. JUC多线程:JMM内存模型与volatile内存语义
  6. mac php-frm xampp_XAMPP for Mac(Apache服务器环境套件)
  7. springCloud - 第9篇 - 同步配置文件(消息总线方式)
  8. 任女尔(1990-),女,北京卡达克数据技术中心软件业务本部助理工程师,主要研究方向为大数据、云计算。...
  9. 时间序列趋势判断(一)——斜率阈值判断
  10. Codeforces 1103 E. Radix sum
  11. 求两字符串最长公共连续子串---C++编程
  12. Algs4-1.2.14实现Transaction中的equals()方法
  13. Docker系列(一)什么是Docker
  14. swift5 修改Accessibility order读取的顺序
  15. win10计算机等应用不能能用,Win10电脑无法安装应用软件的解决办法
  16. Java项目 yaml明文密码加密
  17. 对.Net 垃圾回收的C#编程相关方面(Finalize 和Dispose(bool disposing)和 Dispose())的一些理解体会
  18. 中国本土八大会计师事务所简介
  19. SHIO世硕科技马新云携全体员工:2021,在此感谢所有的一切!
  20. 关于BeanUtils.populate

热门文章

  1. such as, for example, etc., and so on, i.e., 和e.g.的使用
  2. 移动硬盘无法识别,参数错误的问题## 标题
  3. ZT- 100年前的人蚁大战
  4. SpringMVC学习-IDEA创建maven项目添加webapp怎么配置
  5. AMIS:这是让前端失业的一个框架
  6. Android硬件加速原理与实现
  7. Just move in!
  8. vue-skeleton-webpack-plugin 骨架屏插件使用
  9. 《layui宇宙版教程》:轮播组件carousel
  10. roaming文件夹是什么文件夹,roaming文件夹能删除么?