一.Spring框架概述
1,Spring是一个 开源的 轻量级的 JavaEE框架。
轻量级:体积小,jar独立使用不需要依赖其他jar包,
开源:免费,可以提供源代码
框架:解决开发的复杂性,简化企业开发。
2,spring的两个核心部分:IOC,Aop
IOC:控制反转,把创建对象的过程交给spring进行管理。
Aop:面向切面,在不修改源代码的情况下,进行功能的添加或增强。
3,spring框架的特点:
1),方便解耦,简化开发:对象与对象之间的关系依赖spring。
2),Aop编程支持:不改变源代码,增强功能
3),方便测试;
4),方便集成各种优秀框架。
5),方便进行食物管理,降低API的使用难度
6),java源码经典学习范例。
4,入门案例:
文章图片
1),下载Spring5:
spring网址 : spring.io
直接下载地址:https://repo.spring.io/artifa...
还要下载一个日志jar包,spring5依赖于这个jar包commons-login这个jar
下载链接:commons-logging-1.1.1.jar下载及Maven、Gradle引入代码,pom文件及包内class -时代Java (nowjava.com)
我个人下载5.2.6
文章图片
文章图片
2),下载完成以后,参加一个普通的java项目,将jar导入项目
文章图片
3),使用spring
(1),创建普通类,在类中创建普通方法
public class User {
public void add(){
System.out.println("add.......");
}
}
(2),创建spring配置文件,在配置文件中配置文件配置创建的对象
a,Spring配置文件使用xml文件配置

文章图片
b,创建测试类Test
public class Tset {
@Test
public void testAdd(){
//1.读入上下文配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//2.获取配置对象
User user = context.getBean("user", User.class);
System.out.println(user);
user.add();
}
}
c,测试结果,成功创建对象,并调用方法

文章图片
二,IOC容器 1.IOC底层原理
1),什么是IOC: 控制反转,将对象的创建和对象之间的交互作用,都交给spring进行管理。
2),使用IOC的目的 为了降低耦合
3)IOC入门案例 2,IOC底层原理
1),xml解析,工厂模式,反射 2)图解IOC底层原理 传统方式:

文章图片
工厂模式:

文章图片
IOC模式:

文章图片
3.IOC接口(BeanFactory)
IOC基于容器完成,IOC容器的底层就是对象工厂
1),BeanFactory接口: IOC容器的最基本实现,是spring内部的使用接口,不提倡给开发人员使用。
2),ApplicationContext接口: 是BeanFactory的子接口,提供比BeanFactory更强大的功能,一般为开发人员使用
3),两接口的区别

文章图片

文章图片
因为我们在使用Spring框架时,一般都是配合网页使用,使用BeanFactory创建对象时,不会创建对象,而加载配置文件的时候,是在服务器启动的时候,使用tomcat,都是将系统加载文件等费事非空间的事放在tomcat启动时完成,以提供更好的用户体验,所以采用ApplicationContext接口
4),applicationContext的实现类

文章图片
3.IOC操作Bean管理(基于xml)
1),什么是bean管理: A,bean管理包括两个步骤:Spring创建对象和Spring属性注入
2),bean管理的实现方式: a,基于xml配置文件的实现方式
1.基于XML方式创建对象

文章图片
id属性:给class类路径取个别名
class属性:创建对象类的全路径
XML方式创建默认对象默认使用空参构造器
2.基于XML方式的属性注入
(1),DI:依赖注入,就是注入属性。
DI与IOC的区别:DI是IOC的一种实现。
方式一:使用set方式注入
(a),创建类的对象,创建set方法

文章图片
(b),在配置文件中配置对象创建,配置属性注入

文章图片
方式二:使用有参构造方法注入

文章图片

文章图片
方式三:p名称空间注入:
第一步:

文章图片
第二步:

文章图片
3.注入空值和特殊字符
一,注入空值

文章图片
二,注入特殊符号

文章图片
4,注入bean
1),注入外部bean
引入外部bean,用service调用Dao层,就是引入外部bean的过程。

文章图片
2)注入内部bean 和 级联赋值

文章图片
级联赋值方式1:不需要dept的get方法。

文章图片
级联赋值方式2:第二种方法需要创建dept的get方法。

文章图片
5.注入集合属性
0),创建Stu类,User类
package com.yuge;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Stu {
//数组类型
private String course[];
//List类型
private List name;
//Map类型
private Map map;
//Set类型
private Set set;
//List类型中存入多个对象
private List userList;
public void setUserList(List userList) {
this.userList = userList;
}
public void setCourse(String[] course) {
this.course = course;
}
public void setName(List name) {
this.name = name;
}
public void setMap(Map map) {
this.map = map;
}
public void setSet(Set set) {
this.set = set;
}
public void show(){
System.out.println(Arrays.toString(course));
System.out.println(name);
System.out.println(map);
System.out.println(set);
System.out.println(userList);
}
}
package com.yuge;
public class User {
private String name;
public void setName(String name) {
this.name = name;
}
public void add(){
System.out.println("add.......");
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
1).XML注入数组类型属性
2),XML注入List集合属性
3),XML注入Map集合属性
4),XML注入Map属性
5),在集合中注入对象属性:
javaSe
Mysql
武巴
巴巴
张三
小三
6),创建测试类
package com.yuge.test;
import com.yuge.Stu;
import com.yuge.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.applet.AppletContext;
public class Tset {
@Test
public void testAdd(){
//加载配置文件
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("bean2.xml");
//创建对象
Stu stu = (Stu) applicationContext.getBean("stu");
stu.show();
}
}
7),输出结果

文章图片
8).将集合向上抽取为所有bean的公共集合
第一步:引入新的名称空间:

文章图片
第二步:使用util标签完成list集合注入的向上抽取
创建新的Book类测试向上抽取注入list
package com.yuge;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import java.util.List;
public class Book {
private List bookList;
public void setBookList(List bookList) {
this.bookList = bookList;
}
public void test(){
System.out.println(bookList);
}
}
配置XML文件抽取注入list的方法:
java从入门到入土
python从入门到入狱
运行结果:抽取成功

文章图片
抽取之前的样子:

文章图片
抽取之后的样子:

文章图片
还可以抽取更多的对象。

文章图片

文章图片
6,Spring中的两种bean 1)普通bean:XML中定义什么类型就返回什么类型
2),工厂bean:XML中定义一个类型,可以返回其他类型
第一步:创建类作为工厂bean,实现FactoryBean的接口
第二步:实现接口里的方法,在实现的方法中定义返回的bean类型
package com.yuge.factoryBean;
import com.yuge.Stu;
import org.springframework.beans.factory.FactoryBean;
public class Mybean implements FactoryBean {
@Override
public Stu getObject() throws Exception {
Stu stu = new Stu();
return stu;
}
@Override
public Class> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return false;
}
}
第三步:配置XML文件
测试:@Test
public void testMyBean(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
Stu stu = context.getBean("myBean",Stu.class);
System.out.println(stu);
}
结果:

文章图片
7,bean的作用域:

文章图片
在XML中配置bean时单实例还是多实例:

文章图片

文章图片

文章图片
8,XML的自动装配

文章图片
自动装配:根据指定的装配规则,(属性名称或者属性类型),Spring自动将匹配的属性值填入。
演示自动自动装配:
1,根据名称装配

文章图片
2,根据属性类型装配

文章图片
9,引入外部属性管理

文章图片
4.IOC操作Bean(基于注解)
1,spring针对创建对象提供的注解 第一步引入依赖:

文章图片
第二步:开启组件扫描

文章图片
第三步:创建类,在类上添加上注解。
1,@Component,都可以使用改注解创建对象

文章图片
2,@Service,一般用于业务逻辑层,或者service层
3,@Controller,一般用于Web层
4,@Repository,一般用于Dao层
上面的资格注解,功能都一样,只是将每个注解用于不同层便于开发人员区别。2,开启组件扫面配置的细节配置

文章图片

文章图片

文章图片
3,使用注解出入属性 1),@Autowired:根据属性类型自动注入
第一步:使用注解在各个类中创建对象。
第二步:定义对象属性。在属性上面添加注解。不需要set方法。

文章图片
2),@Qualifier:根据属性名注入

文章图片
3),@Resource:可以根据属性名和属性类型注入

文章图片
以上三种是注入对象,不是普通类型*
4),@Value:注入普通类型

文章图片
4,完全注解开发 1),创建配置类,替代XML配置文件

文章图片

文章图片
2),编写测试类

文章图片
三,Aop 面向切面,不修改源码对功能进行加强。
1,什么是AOP
对业务的各个逻辑进行隔离,从而使业务之间的逻辑耦合性降低,提高代码的可重用性,提高开发效率。

文章图片
2,AOP的底层原理
1,AOP底层使用动态代理 1,有接口的动态代理,使用JDK的动态代理
创建接口的实现类的代理对象,增强类的方法

文章图片
2,无接口的动态代理,使用CGLIB动态代理
创建子类的代理对象,增强类的方法

文章图片
2,使用JDK的动态代理 使用proxy类实现动态代理

文章图片

文章图片
代码实现:
1,创建接口:
package com.JDK动态代理;
public interface UserDao {
public int add(int a,int b);
public String update(String id);
}
2,创建实现类
package com.JDK动态代理;
public class UserDaoImpl implements UserDao{
@Override
public int add(int a, int b) {
return a+b;
}
@Override
public String update(String id) {
return id;
}
}
3,使用proxy类创建接口的动态代理对象
package com.JDK动态代理;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.security.PublicKey;
import java.security.UnresolvedPermission;
import java.util.Arrays;
public class JDKProxy{
public static void main(String[] args) {
Class[] interfaces = {UserDao.class};
UserDao dao = (UserDao)Proxy.newProxyInstance(UserDaoProxy.class.getClassLoader(), interfaces, new UserDaoProxy(new UserDaoImpl()));
int res = dao.add(1, 2);
System.out.println(res);
}
}
class UserDaoProxy implements InvocationHandler{
//需要将待增强功能的类的对象传递到代理类中,并通过构造方法,代理类的构造方法将其实例化
//通过UserDaoProxy创建UserDaoImpl的代理对象
private Object obj;
public UserDaoProxy(Object obj){
this.obj = obj;
}
@Override
/**
*增加逻辑写在这个方法内
* @ proxy:代理对象
* @ method:需要增强的方法
* @ args:要增强功能的方法需要的参数
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//方法之前
System.out.println("方法之前执行。。。"+method.getName()+"传递的参数:"+ Arrays.toString(args));
//被增强方法执行
Object res = method.invoke(obj, args);
//方法之后执行
System.out.println("方法之后执行。。。"+obj);
return res;
}
}

文章图片
3,AOP中的相关术语
1,连接点:那些方法可以被增强,那些方法就叫连接点。
2,切入点:世界真正被增强的方法就叫切入点。
3,通知(增强):实际被增强的逻辑部分就叫通知或者增强。
通知有多种类型:

文章图片
4,切面:把通知应用到切入点的动作就叫做切面
## 4,AOP的操作准备
1,Spring框架一般都是基于AspectJ实现AOP操作
AspectJ:不是Spring框架的一部分,独立于AOP的框架,一般将Spring和AOP框架一起使用进行AOP操作。
2,基于AspectJ实现AOP操作
(1),基于XML配置文件的AOP操作
(2),基于注解方式实现(使用)
3,在项目的过程中,引入AOP相关的依赖。

文章图片
4,切入点表达式
(1),切入点表达式作用:知道对哪个类里面的那个方法进行增强。
(2),语法结构:
execution:([权限修饰符][返回类型][类全路径][方法名称](参数列表))
举例1:execution(* (返回值可以省略)com.yuge.UserDaoImpl.add(..));
加强com.yuge.UserDaoImpl的add()方法,传入的参数用..表示,权限修饰符用*,返回值类型省略。
举例2:execution( (返回值可以省略)com.yuge.UserDaoImpl.(..)); 对类中的所有方法加强。
举例3:execution( (返回值可以省略)com.yuge..*(..)); 对包中所有类的所有方法加强
5,AOP操作(AspectJ注解) 1,创建一个类,在类中定义方法,使用注解在类中增强该方法。
package com.AOP注解方式;
public class User {
public void add(){
System.out.println("add...................");
}
}
2,创建一个增强类,编写增强逻辑
package com.AOP注解方式;
//增强类
public class UserProxy {
//前置通知
public void before(){
System.out.println("before.............");
}
}
3,进行通知的配置
(0)、引入名称空间

文章图片
(1),在spring的配置文件中,开启注解扫描
(2),使用注解创建User对象和UserProxy对象。

文章图片
(3),在增强类上面添加@Aspect注解

文章图片
(4),在spring配置文件中开启生成代理对象。
(5),配置不同类型 的通知
a,在增强类方法上面,添加通知类型。使用切入点表达式配置package com.AOP注解方式;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
//增强类
@Component
@Aspect //生成代理对象
public class UserProxy {
//前置通知,添加了Before注解,则就会在add()方法之前执行before方法。
@Before("execution(* com.AOP注解方式.User.add(..))")
public void before(){
System.out.println("before.............");
}
//在方法执行之后执行
@After("execution(* com.AOP注解方式.User.add(..))")
public void after(){
System.out.println("after.............");
}
//在方法存在异常时执行
@AfterThrowing("execution(* com.AOP注解方式.User.add(..))")
public void afterThrowing(){
System.out.println("afterThrowing.............");
}
//在方法返回之后执行
@AfterReturning("execution(* com.AOP注解方式.User.add(..))")
public void afterReturning(){
System.out.println("afterReturning.............");
}
//添加环绕方法,在方法执行前后都执行
@Around("execution(* com.AOP注解方式.User.add(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕之前.............");
//被增强的方法执行
proceedingJoinPoint.proceed();
System.out.println("环绕之后..............");
}
}
package com.AOP注解方式;
import org.springframework.stereotype.Component;
@Component
public class User {
public void add(){
System.out.println("add...................");
}
}
package com.AOP注解方式;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
@org.junit.Test
public void testAdd(){
//加载上下文配置,读取xml配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
//获取对象
User user = (User)context.getBean("user");
user.add();
}
}
运行结果:

文章图片
b,总结:
after无论是否存在异常都会执行,afterReturning存在异常时不会执行。
6,AOP操作(AspextJ注解)优化 1,提取相同的切入点
//抽取相同的切入点
@Pointcut(value = "https://www.it610.com/article/execution(* com.AOP注解方式.User.add(..))")
public void pointCut(){
}
//前置通知,添加了Before注解,则就会在add()方法之前执行before方法。
@Before("pointCut()")
public void before(){
System.out.println("before.............");
}
//在方法执行之后执行
@After("execution(* com.AOP注解方式.User.add(..))")
public void after(){
System.out.println("after.............");
}
2,当有多个增强类对同一个方法进行增强时,设置增强类优先级
在多个增强类上面设置优先级使用@Order(整型值)这个注解,整型值越小,优先级越高

文章图片
//增强类
@Component
@Aspect //生成代理对象
@Order(3)
public class UserProxy {
//抽取相同的切入点
@Pointcut(value = "https://www.it610.com/article/execution(* com.AOP注解方式.User.add(..))")
public void pointCut(){
}
@Component
@Aspect
@Order(0)
public class UserProxy2 {
@Before("execution(* com.AOP注解方式.User.add(..))")
public void before(){
System.out.println("UserProxy2增强类先执行。。。。。");
}
}
7,AOP操作(XML配置文件) 前提在xml中创建增强类和被增强类的对象

文章图片
8,完全注解开发

文章图片
四,JdbcTemplate 1,JdbcTempalte的概念
Spring对JDBC进行封装,使用JdbcTemplate可以方便的对数据库的操作。
准备工作:
引入依赖:

文章图片
配置XML创建类注入属性

文章图片
2,使用JdbcTemplate模板对数据库的增删改查
【万字长文详解Spring5架构教程,还不懂你打我!!】
public interface BookDao {
void add(Book book);
}
@Repository
public class BookDaoImpl implements BookDao {
//注入JdbcTemplate对象
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void add(Book book) {
String sql = "insert into book values(?,?,?)";
int update = jdbcTemplate.update(sql, book.getId(), book.getName(), book.getPrice());
System.out.println(update);
}
}
@Service
public class BookService {
//注入BookDao属性
@Autowired
private BookDao bookDao;
public void insert(Book book){
bookDao.add(book);
}
}
package com.druid;
public class DruidDataSource {
String url;
String password;
String username;
String driverClassName;
public void setUrl(String url) {
this.url = url;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
private void close() {
}
}
package com.test;
import com.bean.Book;
import com.service.BookService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestJdbcTemplate {
@Test
public void test(){
ApplicationContext context =
new ClassPathXmlApplicationContext("bean4.xml");
BookService bookService = context.getBean("bookService", BookService.class);
Book book = new Book();
book.setId(1);
book.setName("一阳指");
book.setPrice(250);
bookService.insert(book);
}
}
查询返回某一个值

文章图片
查询返回某一个对象

文章图片

文章图片
查询返回一个集合

文章图片

文章图片
## 3,使用JdbcTemplate模板对数据库的批量操作

文章图片

文章图片

文章图片
五,事务操作 1,事务的概念:
回顾:事务是指一组基本的数据操作单元,要么全部完成操作,要么全部都不完成操作。
典型事务场景:银行转账
事务的四大特性(ACID):原子性,一致性,隔离性,持久性
2,事务环境的搭建

文章图片
3,spring事务管理的介绍
1,事务添加到JavaEE的三层体系结构的Service层(业务逻辑层)
2,在Spring事务操作:
1),有两种方式:编程式(在方法中添加代码)和声明式(基于XML或者基于注解方式)
2),声明式事务管理:底层使用到AOP
3),Spring事务管理相关的API

文章图片

文章图片

文章图片

文章图片
4,多事务之间事务的传播行为:

文章图片

文章图片
5, ioslation关于事务的隔离级别:
事务的隔离性:多事务的操作之间不会相互影响
如果不考虑隔离:会导致脏读,幻读,不可重复读的问题
解决隔离级别:

文章图片
配置隔离级别:

文章图片

文章图片
6,关于事务的超时限制:

文章图片

文章图片
7,readOnly是否只读:

文章图片

文章图片
8,rollbackFor:回滚

文章图片
9,noRollbackFor:不回滚

文章图片
10,事务完全注解开发

文章图片

文章图片

文章图片

文章图片