spring5

spring5

1、概述

1)spring是轻量级的开源的JavaEE框架


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


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

IOC:控制反转,把创建对象的过程交给spring来完成

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


4)spring的特点:

①方便解耦,简化开发

②AOP编程支持

③方便程序测试

④方便和其他框架进行整合

⑤方便进行事务操作

⑥降低API开发难度

2、IOC(概念和原理)

1)什么是IOC : 控制反转,把对象的创建和对象之间的调用过程交给Spring进行管理


2)使用IOC的目的 : 降低耦合度


3)IOC底层原理 :xml解析,工厂模式,反射


原理图:

39236d01f4f7fe5f4ff8b9d8b1ee718e.png

130d677a01d13a45ad48266f890a02ee.png稍微降低了耦合度

fb34a7d48525a0039c954e8d7987935b.png进一步降低了耦合度

3、IOC(接口)


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


2) Spring提供了IOC容器的基本实现两种方式(两个接口)

①BeanFactory : IOC容器的基本实现,是Spring内部的使用接口

*加载配置文件(xml)时不会创建对象,而是在获取对象(使用)时才会创建

②ApplicationContext : BeanFactory的子接口。提供了更多更强大的共能,一般由开发人员进行使用

*加载配置文件时把配置文件中的对象进行创建

3) ApplicationContext接口的实现类

fb0ffc7dd8aa53d34dc5273fb542585d.png

4、基于XML创建对象

6c1ada1b7ff053e8f28a84c82eca6aaa.png

5、Bean的作用域

在Spring中可以通过scope属性设置创建的bean实例仕多实例还是单实例

scope属性值:

![652922fea5cec49dadb0e499d8c051d6.png][652922fea5cec49dadb0e499d8c051d6]

单实例(默认)

![0b3bb2d1a773e0c0e5ece6de8540c49e.png][0b3bb2d1a773e0c0e5ece6de8540c49e]

![12be8ff0b2a697ed98f77e46d4fb223b.png][12be8ff0b2a697ed98f77e46d4fb223b]


运行结果:

![7c29563c80cb71e646365d541ec22e96.png][7c29563c80cb71e646365d541ec22e96]

多实例:

![a0f6ecd15bbe063eb69503ab3fb77a52.png][a0f6ecd15bbe063eb69503ab3fb77a52]

![ffbf64c8d8c051a8c0465a2fa32edbac.png][ffbf64c8d8c051a8c0465a2fa32edbac]

运行结果:

![dffd41a571d8b54b2100e150f89d80c0.png][dffd41a571d8b54b2100e150f89d80c0]

注:单实例对象是在容器注册时创建对象,而多实例对象的床在是在getBean()方法被调用时创建

6、Bean的生命周期、

bean生命周期

1)通过构造器创建bean实例(无参构造)

2)为bean的属性设置值和对其他bean的引用(调用set方法)

2.5)把bean实例传递给bean后置处理器方法

3)调用bean的初始化方法(需要进行配置)

3.5)把bean实例传递给bean后置处理器方法

4)bean可以使用了(已经获取到对象)

5)当容器关闭时调用bean的销毁方法(需进行配置)注:只有单实例对象会在容器关闭时被销毁

7、基于注解创建对象

目的:简化XML配置

eg:

![81954e0d8dd53925c45459793a4bdfdb.png][81954e0d8dd53925c45459793a4bdfdb]

![2f3d83dc1c78504faed4ec4b09f73a92.png][2f3d83dc1c78504faed4ec4b09f73a92]

运行结果

![91b69f32a3e1f1919d072d0d1a5a1615.png][91b69f32a3e1f1919d072d0d1a5a1615]


8、Spring中针对Bean管理而提供的注解


1)@Component

2)@Service

3)@Controller

4)@Repository

这四个注解只是在使用的场景上不同 功能上并没有区别(并且可以在不同场景中使用)



9、组件扫描: 在XML文件中配置要扫描(即需要创建对象)的包

创建名称空间

xmlns:context=“http://www.springframework.org/schema/context

xsi:schemaLocation="http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd"

![c4da542380699889fec8a08471c32ab5.png][c4da542380699889fec8a08471c32ab5]


![0d257b15c2bd7370becaaaf9a21cac44.png][0d257b15c2bd7370becaaaf9a21cac44]


10、属性的注入


1) @Autowired :根据属性类型进行自动装配

![040440823a0b359d2e63e39917fafdc9.png][040440823a0b359d2e63e39917fafdc9] 《----------自动注入--------- ![645381d0396f82527ff94b5d1b944647.png][645381d0396f82527ff94b5d1b944647]

2)@Qualifier:根据属性名进行注入(需要和Autowired一起使用)

![e677e1b9dcc3805483cf5aaf469aeb02.png][e677e1b9dcc3805483cf5aaf469aeb02]《-------根据名称注入-------![177813db9fe00c319bd560369cc5f148.png][177813db9fe00c319bd560369cc5f148]

3)@Resource:可以根据类型注入也可以根据名称注入

![4c47467157654844c0753396f46c9371.png][4c47467157654844c0753396f46c9371]《----------根据类型注入---------![52d26d8972ac78e09bfcaaa493d01369.png][52d26d8972ac78e09bfcaaa493d01369]

![54986210977d820bab235d5f62b8ae64.png][54986210977d820bab235d5f62b8ae64]《-------根据名称注入-------![fb18427ef4a3b571232e3a2380e6b660.png][fb18427ef4a3b571232e3a2380e6b660]

注:@Resource注解是属于Javax扩展包中的注解并不是Spring中的注解

4)@value:注入普通类型属性

![a762dfcc9e5764d7a3bcf2472d68ef3e.png][a762dfcc9e5764d7a3bcf2472d68ef3e]                        运行结果:![54d5ef2f73dd2b6f7f2aa0d96554baa8.png][54d5ef2f73dd2b6f7f2aa0d96554baa8]




11、实现完全注解开发


![75dc98bdedfe9fdc6ebcfd917ec160ab.png][75dc98bdedfe9fdc6ebcfd917ec160ab]


![97ebf284c7388da829a04df166536b8d.png][97ebf284c7388da829a04df166536b8d]


运行结果并未改变

![d741edecbde295a17f44601913cde47d.png][d741edecbde295a17f44601913cde47d]

AOP


1、什么是AOP

AOP ( Aspect Oriented Programming)面向切面编程,利用AOP可以对业务逻辑实现各个部分的隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程        序的可重用性,提高开发效率

通俗的讲AOP就是 不通过修改源代码的方式,对主干功能里添加新的功能

2、底层逻辑

第一种:有接口的情况下,使用JDK动态代理

创建接口实现类代理对象,增强类方法

![7545b3457e5913d8ebbd5ab480c76bb0.png][7545b3457e5913d8ebbd5ab480c76bb0]

第二种:没有接口的情况下,使用CGLIB动态代理

创建子类代理类对象增强对象方法

![a79a006a45bfd0a412771730d642a8c6.png][a79a006a45bfd0a412771730d642a8c6]

eg:JDK 动态代理的底层实现(不通过Spring)

创建要实现动态代理的接口和实现类

![a8f58e19efc1f74cfa3feae2839986a9.png][a8f58e19efc1f74cfa3feae2839986a9]

![2e81c7f2a5a8a89da1677769fb1fd6ea.png][2e81c7f2a5a8a89da1677769fb1fd6ea]



实现动态代理

package com;

import java.lang.reflect.Array;

import java.lang.reflect.InvocationHandler;

import java.lang.reflect.Method;

import java.lang.reflect.Proxy;

import java.util.Arrays;

public class JDKP_Main {

public static void main(String[] args) {

Class [] interfaces = {JDKP_INTF.class};

/*

方法:Proxy.newProxyInstance(类加载器,一个存有要实现动态代理的类的接口的数组,动态代理对象(要实现动态代理的对象));

返回:代理对象

*/

JDKP_INTF test =(JDKP_INTF) Proxy.newProxyInstance(JDKP_Main.class.getClassLoader(),interfaces,new inv(new JDK_Proxy_test()));

System.out.println(test.add(1,2));

}

}

class inv implements InvocationHandler {

private Object obj;

public inv(Object obj) {

this.obj = obj;

}

@Override

/*

对方法进行增强的类

方法:public Object invoke(Object proxy(???), Method method(执行的方法名), Object[] args(执行方法的参数)) throws Throwable

*/

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(“方法之后执行的方法。。。。。”);

return res;

}

}


运行结果:

![1d60c277dd7d2812cd327a0e5d0b695b.png][1d60c277dd7d2812cd327a0e5d0b695b]

3、AOP术语

1、连接点

类中可以被增强的方法

2、切入点

实际已经被增强的方法

3、通知(增强)

实现增强的逻辑部分

通知有多种类型:前置通知,后置通知,环绕通知,异常通知,最终通知(类似于finally)

4、切面

把通知应用到切入点的过程

实现

package com.AOP_t;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.EnableAspectJAutoProxy;

//配置类

@Configuration

@ComponentScan(basePackages = {“com.AOP_t”})//包扫描类路径

@EnableAspectJAutoProxy(proxyTargetClass = true)//开启AOP

public class config_class {}

被增强类

package com.AOP_t;

import org.springframework.stereotype.Component;

@Component

public class User {

//切入点\连接点

public void add(){

System.out.println(“add…”);

}

}


增强类(切面类)

package com.AOP_t;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.*;

import org.springframework.context.annotation.EnableAspectJAutoProxy;

import org.springframework.stereotype.Component;

@Component

@Aspect

public class plus_c {

/*

切入点表达式

excution(权限修饰符(可用*代表全部) 返回类型(可略)类全路径.方法名(【参数列表(…代表全部)】))

eg:execution(* com.AOP_t.User.add(…))

*/

@Pointcut(“execution(* com.AOP_t.User.add(…))”)//切入点提取

public void pointcut(){}

//通知

@Before(value = “pointcut()”)//前置通知

public void before(){

System.out.println(“before…”);

}

@After(value = “pointcut()”)//后置通知

public void after(){

System.out.println(“after…”);

}

@AfterReturning(value = “pointcut()”)//最终通知 收到返回值执行

public void afterReturning(){

System.out.println(“AfterReturning…”);

}

@AfterThrowing(value = “pointcut()”)//异常通知

public void afterThrowing(){

System.out.println(“afterThrowing…”);

}

@Around(value = “pointcut()”)//环绕通知

public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

System.out.println(“around before…”);

proceedingJoinPoint.proceed();

System.out.println(“around after…”);

}

}

运行结果

![c2fc43ba7d72668f6e0bbbc386faa428.png][c2fc43ba7d72668f6e0bbbc386faa428]

当有异常抛出

package com.AOP_t;

import org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import org.springframework.stereotype.Component;

@Component

public class User {

public static void main(String[] args) {

ApplicationContext context = new AnnotationConfigApplicationContext(config_class.class);

User user =(User) context.getBean(“user”);

user.add();

}

//切入点\连接点

public void add(){

int a = 10/0;

System.out.println(“add…”);

}

}

运行结果

![3667d9f910bd7ca57369ab26d9c740fb.png][3667d9f910bd7ca57369ab26d9c740fb]

由上面的运行结果可知:后置通知在发生异常后依然执行 

但是最终通知因为没有接受到返回结果所以不执行(环绕通知的后半段也不会执行)

jdbcTemplate


maven依赖注入

org.springframework

spring-context

5.1.5.RELEASE

org.springframework

spring-aspects

5.1.5.RELEASE

junit

junit

4.10

test

com.alibaba

druid

1.0.9

mysql

mysql-connector-java

8.0.22

org.junit.jupiter

junit-jupiter

RELEASE

compile

spring配置文件

<?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:context=“http://www.springframework.org/schema/context

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package=“com.JDBC_Template”></context:component-scan>

DAO类接口

package com.JDBC_Template.Dao;

import com.JDBC_Template.Entity.Book;

import java.util.List;

public interface JDBC_DAO {

public void add(Book book);

public void delete(int id);

public void update(Book book);

public int search();

public Book searchobj(int id);

public List searchList();

public void addm(List<Object[]> books);

}

接口实现类

package com.JDBC_Template.Dao;

import com.JDBC_Template.Entity.Book;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jdbc.core.BeanPropertyRowMapper;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Component;

import java.util.List;

@Component

public class JDBCDAO_Impl implements JDBC_DAO{

@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.getStatus());

System.out.println(update);

}

@Override

public void delete(int id) {//删除一个数据

String sql = “delete from book where id=?”;

int update = jdbcTemplate.update(sql, id);

System.out.println(update);

}

@Override

public void update(Book book) {//修改一个数据

String sql = “update book set name=?,status=? where id=?”;

int update = jdbcTemplate.update(sql, book.getName(), book.getStatus(), book.getId());

System.out.println(update);

}

@Override

public int search() {//查找数据(返回总个数)

String sql = “select count(*)from book”;

Integer query = jdbcTemplate.queryForObject(sql, Integer.class);

return query;

}

@Override

public Book searchobj(int id) {//查找数据(返回所擦护照的实体类)

String sql = “select * from book where id=?”;

Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(Book.class),id);

return book;

}

@Override

public void addm(List<Object[]> books) {//批量添加数据(批量删除类似)

String sql =“insert into book values(?,?,?)”;

jdbcTemplate.batchUpdate(sql,books);

}

@Override

public List searchList() {//查找多行数据并返回对象列表

String sql = “select * from book”;

List query = jdbcTemplate.query(sql, new BeanPropertyRowMapper(Book.class));

return query;

}

}

实体类

package com.JDBC_Template.Entity;

public class Book {

private int id;

private String name;

private String status;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getStatus() {

return status;

}

public void setStatus(String status) {

this.status = status;

}

@Override

public String toString() {

return “Book{”+this.getId()+“,”+this.getName()+“,”+this.getStatus()+“}”;

}

}

service类

package com.JDBC_Template.Service;

import com.JDBC_Template.Dao.JDBCDAO_Impl;

import com.JDBC_Template.Entity.Book;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

@Service

public class BookService {

@Autowired

private JDBCDAO_Impl jDBCDAO_Impl;

public void add(Book book){

jDBCDAO_Impl.add(book);

}

public void delect(int id){

jDBCDAO_Impl.delete(id);

}

public void update(Book book){

jDBCDAO_Impl.update(book);

}

public int search(){

return jDBCDAO_Impl.search();

}

public Book searchobj(int id){

return jDBCDAO_Impl.searchobj(id);

}

public List searchList(){

return jDBCDAO_Impl.searchList();

}

public void addm(List<Object[]> books){

jDBCDAO_Impl.addm(books);

}

}

2、事务

1)什么是事务:事务是数据库操作的最基本单元,逻辑上一组操作要么都成功,如果由一个失败的话那么所有的操作都失败

eg:银行转帐

2)事务的四个特性(ACID)

①原子性

②一致性

③隔离性

④持久性

3、Spring中的事务管理

1)事务的声名会添加到JAVAEE三层结构里面的Service层(业务逻辑层)中

2)分为编程式管理和声明式事务管理(使用)


3)声明式业务管理

①基于XML配置文件方式

②基于注解方式(使用)


4)Spring中的事务管理底层使用的是AOP


5)Spring事务管理API

在Spring中提供了一个接口,代表事务管理器,这个接口针对不同的框架提供了不同的实现类

![c351f9fc04c7018b2dca518cdf264e21.png][c351f9fc04c7018b2dca518cdf264e21]


6)实现


配置文件

<?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:context=“http://www.springframework.org/schema/context” xmlns:tx=“http://www.springframework.org/schema/tx

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

<context:component-scan base-package=“com.Transaction_t”/>

<tx:annotation-driven transaction-manager=“transactionManager”/>


DAO层

package com.Transaction_t;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Component;

@Component

public class TransactionDao {

@Autowired

JdbcTemplate jdbcTemplate;

public void add(){

String sql1 = “select salary from emp where name=‘Mary’”;

String sql2 = “update emp set salary=?+100 where name=‘Mary’”;

Integer s = jdbcTemplate.queryForObject(sql1,Integer.class);

int update = jdbcTemplate.update(sql2,s);

System.out.println(update);

}

public void down(){

String sql1 = “select salary from emp where name=‘Lucy’”;

String sql2 = “update emp set salary=?-100 where name=‘Lucy’”;

Integer s = jdbcTemplate.queryForObject(sql1,Integer.class);

int update = jdbcTemplate.update(sql2,s);

System.out.println(update);

}

}


Service层

package com.Transaction_t;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.PlatformTransactionManager;

import org.springframework.transaction.annotation.Transactional;

@Service

@Transactional//开启事务的注释

public class TransactionService {

@Autowired

TransactionDao transactionDao;

public void transaction1(){

transactionDao.add();

int a = 10/0;//异常

transactionDao.down();

}

}




7)@Transaction的参数

①propagation 设置事物的传播行为

![3fd53bb5a77f9e80a771a40dd4223af6.png][3fd53bb5a77f9e80a771a40dd4223af6]

②isolation 设置事务的隔离级别

@Transactional(isolation = Isolation.READ_UNCOMMITTED):读取未提交数据(会出现脏读, 不可重复读,幻读) 基本不使用

@Transactional(isolation = Isolation.READ_COMMITTED):读取已提交数据(会出现不可重复读和幻读)

@Transactional(isolation = Isolation.REPEATABLE_READ):可重复读(会出现幻读)

@Transactional(isolation = Isolation.SERIALIZABLE):串行化(解决所有问题)

MYSQL: 默认为REPEATABLE_READ级别

脏读 : 一个事务读取到另一事务未提交的更新数据

不可重复读 : 在同一事务中, 多次读取同一数据返回的结果有所不同, 换句话说, 后续读取可以读到另一事务已提交的更新数据. 相反, "可重复读"在同一事务中多次读取数据时, 能够保证所读数据一样, 也就是后续读取不能读到另一事务已提交的更新数据

幻读 : 一个事务读到另一个事务已提交的insert数据

③timeout 设置事务的超时秒数

④ readOnly 设置当前事务是否为只读

⑤ <span style=“letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(247, 247, 247); font-size: 14pt; color: rgb(79, 79, 79); font-family: -apple-system, “SF UI Text”, Arial, “PingFang SC”, “Hiragino Sans GB”, “Microsoft YaHei”, “WenQuanYi Micro Hei”, sans-serif; font-variant-caps: normal; font-variant-ligatures: no-common-ligatures;”>rollbackFor 设置需要进行回滚的异常类数组


⑥ noRollbackFor 设置不需要进行回滚的异常类数组



用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:context=“http://www.springframework.org/schema/context

xmlns:tx=“http://www.springframework.org/schema/tx

xmlns:aop=“http://www.springframework.org/schema/aop

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd

http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

<context:component-scan base-package=“com.Transaction_t”/>

<tx:advice id=“txadvice”>

tx:attributes

<tx:method name=“transaction1”/>

</tx:attributes>

</tx:advice>

aop:config

<aop:pointcut id=“pt” expression=“execution(* com.Transaction_t.TransactionService.transaction1(…))”/>

<aop:advisor advice-ref=“txadvice” pointcut-ref=“pt” />

</aop:config>




使用纯注释实现事务


配置类

package com.Transaction_t;

import com.alibaba.druid.pool.DruidDataSource;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration//设置配置类

@ComponentScan(basePackages = “com.Transaction_t”)//开启扫描组件

@EnableTransactionManagement//开启事务

public class TransactionConfig {

@Bean//创建Druid连接池

public DruidDataSource getDataSource(){

DruidDataSource dataSource = new DruidDataSource();

dataSource.setUrl(“jdbc:mysql://localhost:3306/springjdbc?serverTimezone=UTC”);

dataSource.setUsername(“root”);

dataSource.setPassword(“2464wh666666”);

dataSource.setDriverClassName(“com.mysql.cj.jdbc.Driver”);

return dataSource;

}

@Bean//创建jdbctemplate对象 因为IOC容器中已经有了连接池对象 所以形参会自动注入容器中的连接池对象

public JdbcTemplate getJdbcTemplate(DruidDataSource dataSource){

JdbcTemplate jdbcTemplate = new JdbcTemplate();

jdbcTemplate.setDataSource(dataSource);

return jdbcTemplate;

}

@Bean//创建事务管理器 因为IOC容器中已经有了连接池对象 所以形参会自动注入容器中的连接池对象

public DataSourceTransactionManager getDataSourceTransactionManager(DruidDataSource dataSource){

DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();

dataSourceTransactionManager.setDataSource(dataSource);

return dataSourceTransactionManager;

}

}


Spring5新功能(Spring5最低支持jdk8 以上版本)

1、Spring5整合了log4j2 将不在支持log4j日志框架,如果需要使用log4j版本就需要将Spring版本下调

2、Spring5 新增了@Nullable注释

该注释可以添加在方法,属性,参数上,分别代表着 该方法的返回值可以为空   ,   该属性值可以为空   ,   该参数可以为空


3、函数式风格(eg:使用拉姆达表达式创建对象)

@Test

public void test2(){

GenericApplicationContext context = new GenericApplicationContext();

context.refresh();

context.registerBean(TransactionService.class,()->new TransactionService());

TransactionService transactionService = (TransactionService) context.getBean(“com.Transaction_t.TransactionService”);

System.out.println(transactionService);

}

}


4、整合了Junit5单元测试框架(同时也可以自行导入Junit4)

![ScreenClip.png][b585ec31f03a703821bd62082babbdf8]





5、Webflux


    1)介绍:spring webflux是Spring5添加的新模块,用于web开发,功能和SpringMVC类似,Webflux使用当前一种比较流行的响应式编程出现的框架


    2)传统的Web框架,例如SpringMVC是基于Serclet容器的,而Webflux是一种异步非阻塞的框架,异步非阻塞的框架是在Servlet3.1之后才开始支持的,

而Webflux其核心式基于Reactor的相关API实现的


    3)什么式异步非阻塞

            **异步和同步  :针对的是调用者 异步是在发送请求的过程当中不论有没有得到回应都可以去处理其他的事情,而同步是只有在请求的到回应之后才会去                                        处理其他事情


            **阻塞和非阻塞:针对的是被调用者,如果被调用者在接受到请求之后立即执行并且返回结果就是非阻塞,而当被调用者在接收到请求后将线程挂起并去                                            处理其他未完成的任务,知道未完成的任务完成后才处理请求并返回结果就是阻塞








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值