spring笔记(精简版)+mybatis

Spring的核心是控制反转(IoC)和面向切面(AOP)

IoC

  1. 所谓的IoC:Spring是个大容器,存放着各种各样的bean,以往这些bean是你自己主动去new出来的。有了spring后,所有的bean是通过spring容器来进行生成,因此控制权就由你自己反转到了容器上。

  2. IoC的使用方式:
    ① 将所有的bean都配置xml中:<bean id="" class="">
    ② 将所有的依赖都使用注解:@Autowired
    需要对bean使用@Component,以及在xml中配置扫描器<context:component-scan base-package=" ">

  3. IOC的原理(工厂模式+反射技术)
    3.1 IOC容器的初始化过程(bean的注册):
    (1)xml读取,解析成beanDefinition,最后在beanFactory中进行注册。
    在这里插入图片描述
    在这里插入图片描述
    (2)注册:Spring容器内部有一个HashMap ,存放着bean的信息,以后对 bean 的操作都是围绕这个HashMap 来实现的。在这里插入图片描述
    3.2 依赖注入
    当Spring IoC容器完成了初始化后,IoC容器中已经管理类Bean定义的相关数据,但是此时IoC容器还没有对所管理的Bean进行依赖注入,在spring IOC设计中,bean的注册和依赖注入是两个过程,依赖注入在以下两种情况发生:
    a.用户第一次通过getBean方法向IoC容器索要Bean时,IoC容器触发依赖注入。
    b.当用户在Bean定义资源中为元素配置了lazy-init属性,即让容器在解析注册Bean定义时进行预实例化,触发依赖注入。

beanFactory和factoryBean:

  1. FactoryBean:是一个特殊的bean,具有工厂生成对象能力,只能生成特定的对象。
    bean必须使用 FactoryBean接口,此接口提供方法 getObject() 用于获得特定bean。
    先创建FB实例,使用调用getObject()方法,并返回方法的返回值
    FB fb = new FB();
    return fb.getObject();
  2. BeanFactory 和 FactoryBean 对比?
    BeanFactory:工厂,用于生成任意bean。
    FactoryBean:特殊bean,用于生成另一个特定的bean。例如:ProxyFactoryBean ,此工厂bean用于生产代理。 获得代理对象实例。AOP使用

AOP:

  1. 好处:降低耦合度、代码复用
  2. 采取横向抽取机制,取代了传统纵向继承
  3. 一句话:个人理解,aop就是使用代理机制,,对target类的某些方法进行增强。
  4. 代理机制有jdk动态代理和cglib增强两种
  5. AOP术语【掌握】
    1.target:目标类,需要被代理的类。例如:UserService
    2.Joinpoint(连接点):所谓连接点是指那些可能被拦截到的方法。例如:所有的方法
    3.PointCut 切入点:已经被增强的连接点。例如:addUser()
    4.advice 通知/增强,增强代码。例如:after、before
    5.Weaving(织入):是指把增强advice应用到目标对象target来创建新的代理对象proxy的过程.
    6.proxy 代理类
    7.Aspect(切面): 是切入点pointcut和通知advice的结合
    一个线是一个特殊的面。
    一个切入点和一个通知,组成成一个特殊的面。
    在这里插入图片描述
  6. spring aop编程:全自动【掌握】
    切面类:
public class MyAspect implements MethodInterceptor {
	@Override
	public Object invoke(MethodInvocation mi) throws Throwable {
		
		System.out.println("前3");
		//手动执行目标方法
		Object obj = mi.proceed();
		System.out.println("后3");
		return obj;
	}
}

xml配置(需要添加schema):

<?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: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/aop 
       					   http://www.springframework.org/schema/aop/spring-aop.xsd">
	<!-- 1 创建目标类 -->
	<bean id="userServiceId" class="com.itheima.c_spring_aop.UserServiceImpl"></bean>
	<!-- 2 创建切面类(通知) -->
	<bean id="myAspectId" class="com.itheima.c_spring_aop.MyAspect"></bean>
	<!-- 3 aop编程 
		3.1 导入命名空间
		3.2 使用 <aop:config>进行配置
				proxy-target-class="true" 声明时使用cglib代理
			<aop:pointcut> 切入点 ,从目标对象获得具体方法
			<aop:advisor> 特殊的切面,只有一个通知 和 一个切入点
				advice-ref 通知引用
				pointcut-ref 切入点引用
		3.3 切入点表达式
			execution(* com.itheima.c_spring_aop.*.*(..))
			选择方法         返回值任意   包             类名任意   方法名任意   参数任意
		
	-->
	<aop:config proxy-target-class="true">
		<aop:pointcut expression="execution(* com.itheima.c_spring_aop.*.*(..))" id="myPointCut"/>
		<aop:advisor advice-ref="myAspectId" pointcut-ref="myPointCut"/>
	</aop:config>
</beans>

  1. 通知类型:
    前置通知(Before advice):在某连接点之前执行的通知,但这个通知不能阻止连接点之前的执行流程(除非它抛出一个异常)。
    后置通知(After returning advice):在某连接点正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回。
    环绕通知(Around Advice):包围一个连接点的通知,如方法调用。这是最强大的一种通知类型。环绕通知可以在方法调用前后完成自定义的行为。它也会选择是否继续执行连接点或直接返回它自己的返回值或抛出异常来结束执行。
    异常通知(After throwing advice):在方法抛出异常退出时执行的通知。
    最终通知(After (finally) advice):当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。

AOP之事物管理(了解):

  1. 事务的传播属性(一般选择Required,读操作选择Supports
    在这里插入图片描述
  2. 使用(对service层进行aop增强)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<!-- 事务配置 -->
	<!-- 事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 传播行为 -->
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<!-- 切面 -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice"
			pointcut="execution(* com.YYSchedule.store.service.*.*(..))" />
	</aop:config>
</beans>

Mybatis 动态代理

源码主要为MapperRegistry中包含工厂MapperProxyFactory,工厂中的newInstance()方法会产生MapperProxy ,MapperProxy中实现了jdk动态代理机制。
https://blog.csdn.net/xiaokang123456kao/article/details/76228684

猜你喜欢

转载自blog.csdn.net/bintoYu/article/details/86762840