//Spring事物
connection.setAutoCommit(flase)//开启事物,connection是jdbc里面的,关闭事物的自动提交
connection.close//关闭事物
connection.commit//提交事物
connection.rollback//回滚事物,可以写在捕获异常的代码块里面
//把上面的操作都分别封装成方法,再写到工具类里面.写静态方法,这样一来就可以直接用类名来调用
@RunWith(SpringJUnit4ClassRunner.class) //由Junit提供,可以提供运行类的注解(不使用注解的时候不可以使用)
//SpringJUnit4ClassRunner.class 是Spring提供的一个可以运行的类,这个类在运行的时候可以动态获取核心容器对象
@ContextConfiguration(locations = "classpath:xml/beans.xml")//如果是基于xml来就使用这个格式,如果是基于类来创建核心容器就使用class =...
//在上面两个注解之后,可以直接获取对象
@Autowired //直接用这个注解可以获取对象,在下面直接定义就可以
Service service;
//AOP(原生aop配合Spring)
public Proxy.newProxyInstance(object.getClass().getClassLoader(),object.getClass().getInterfaces,new InvocationHandler(){
@Override
public Object invoke(Object proxy,Method method,Object[] args) throws Throwable{
//代理目标前置
Object invoke = method.invoke(object,args);//这里的object就是要增强的目标类,比如说accountService,这个好像是接口
//代理目标后置
return invoke;
} });
//心得体会
//类直接加@Component注解,类里面的参数直接加@Autowired注解
//再代理目标前置的地方就可以用trycatchfinally语句块完成事物相关的操作,把invoke也写进去,到时候调用这个方法传参数就可以直接增强目标类
//test方法中的代码
AccountService accountProxy = (AccountService)this.accountProxy.getAccountProxy();
accountProxy.update();//这样一来,每次调用方法的时候就会增强这个方法
//Spring 中的AOP
Joinpoint(连接点):连接点是指被拦截到的点,在spring中,这些点指的是方法,因为spring只支持方法类型的连接点
Pointcut(切入点):切入点是指我们对哪些Joinpoint进行增强的点
//连接点包含切入点
//灵活运用method.getName()来进行测试和拦截
//通知的种类:前置,后置,异常,结束,环绕
//SpringAOP的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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
//开启扫描包
<context:component-scan base-package="com.ygkj"/>
//在这个区域写配置,为了笔记的方便起见,就写在下面了
</beans>
//创建一个类,包含所有通知(前置+后置+异常+环绕+结束),用方法的格式
//找到需要被代理的类(无接口继承),拥有一个需要被代理的方法
//配置需要代理的对象和配置通知对象(bean标签)
//如果是基于xml的方式来使用AOP,那么被代理类也需要用bean标签在xml文件中配置(不能用注解)
//配置AOP
<aop:config>
//配置切面:切入点和通知
<aop:aspect id="切面标识" ref = "通知类的bean标识">
//配置通知的类型
<aop:before method="前置通知的方法名" pointcut="切入点表达式的属性,这里是execution(public void com.ygkj.Aop.method())"/>
//切入点表达式规则为execution(修饰符+返回值+全类名+方法+参数),public void->*,所有方法->*,所有参数->*,结果为 * com.ygkj.*.*(..),(java.kang.String,java.kang.String)可以保证增强所有的有参方法,或者写int,灵活运用
//后置通知:method改一下,aop:after-returning改一下
//异常通知:aop:after-throwing
//最终通知:aop:after
//注意位置要放好
<aop:pointcut id="123" expression="上文的方法路径"/>//这个要写在最前面
//配置bean标签后,用pointcut-ref"123"来替换pointcut标签
//配置环绕通知,就不能配置上面4个通知
<aop:"aroundAdvice" pointcut-ref="123"/>
//环绕通知:包含了前置后置最终异常通知
</aop:config>
//基于注解的AOP配置
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
//开启扫描包
<context:component-scan base-package="com.ygkj"/>
//开启spring对注解aop的支持
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
//在切面类中添加注解
@Component("logger")
@Aspect //切面类的注解
//编写一个切入点表达式的方法,带有切入点的注解
@Pointcut("切入点的表达式,见上文")
public void pt01(){}
//在前置通知的方法上面添加注解
@Before("pt01()")