Spring 学习笔记(二) Spring AOP 1.定义
在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程 ,通过预编译 方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP 的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程 的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度 降低,提高程序的可重用性,同时提高了开发的效率。
简单的说就是把我们程序重复的代码抽取出来,在需要执行的时候,使用动态代理 的技术,在不修改源码的基础上,对我们的已有方法进行增强。
2.AOP相关术语
Joinpoint(连接点): 所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点。
Pointcut(切入点): 所谓切入点是指我们要对哪些Joinpoint进行拦截的定义。
Advice(通知/增强): 所谓通知是指拦截到Joinpoint之后所要做的事情就是通知。
通知的类型:
前置通知
后置通知
异常通知
最终通知
环绕通知。
Introduction(引介): 引介是一种特殊的通知在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field。
Target(目标对象): 代理的目标对象。
Weaving(织入): 是指把增强应用到目标对象来创建新的代理对象的过程。 spring采用动态代理织入,而AspectJ采用编译期织入和类装载期织入。
Proxy(代理): 一个类被AOP织入增强后,就产生一个结果代理类。
Aspect(切面): 是切入点和通知(引介)的结合。
3.Spring AOP的使用 1.基于bean.xml的使用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 <?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd" > <bean id ="accountService" class ="com.wht.service.Impl.AccountServiceImpl" > </bean > <bean id ="logger" class ="com.wht.utils.Loogger" > </bean > <aop:config > <aop:aspect id ="logAdvice" ref ="logger" > <aop:before method ="printLog" pointcut-ref ="pt1" > </aop:before > <aop:pointcut id ="pt1" expression ="execution(* com.wht.service.impl.*.*(..))" /> </aop:aspect > </aop:config > </beans >
2.基于注解的使用 xml的配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <?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 https://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.wht" > </context:component-scan > <aop:aspectj-autoproxy > </aop:aspectj-autoproxy > </beans >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 @Component("logger") @Aspect public class Loogger { @Pointcut("execution(* com.wht.service.impl.*.*(..))") private void pt1 () {} public void beforePrintLog () { System.out.println("前置通知Logger类中的beforePrintLog方法开始记录日志了。。。" ); } public void afterReturningPrintLog () { System.out.println("后置通知Logger类中的afterReturningPrintLog方法开始记录日志了。。。" ); } public void afterThrowingPrintLog () { System.out.println("异常通知Logger类中的afterThrowingPrintLog方法开始记录日志了。。。" ); } public void afterPrintLog () { System.out.println("最终通知Logger类中的afterPrintLog方法开始记录日志了。。。" ); } @Around("pt1()") public Object aroundPringLog (ProceedingJoinPoint pjp) { Object rtValue = null ; try { Object[] args = pjp.getArgs(); System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。前置" ); rtValue = pjp.proceed(args); System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。后置" ); return rtValue; }catch (Throwable t){ System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。异常" ); throw new RuntimeException(t); }finally { System.out.println("Logger类中的aroundPringLog方法开始记录日志了。。。最终" ); } } }
Spring JdbcTemplate 1.JdbcTemplate的作用 它就是用于和数据库交互,实现对表的CRUD操作
2.JdbcTemplate的使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <?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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd" > <bean id ="jdbcTemplate" class ="org.springframework.jdbc.core.JdbcTemplate" > <property name ="dataSource" ref ="dataSource" > </property > </bean > <bean id ="dataSource" class ="org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name ="driverClassName" value ="com.mysql.cj.jdbc.Driver" > </property > <property name ="url" value ="jdbc:mysql:///mybatis?useUnicode=true& characterEncoding=utf-8& useSSL=false& serverTimezone = GMT" > </property > <property name ="username" value ="root" > </property > <property name ="password" value ="123456" > </property > </bean > </beans >
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public class JdbcTemplateDemo02 { public static void main (String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml" ); JdbcTemplate jdbcTemplate = ac.getBean("jdbcTemplate" , JdbcTemplate.class); jdbcTemplate.update("insert into account(name,money) values(?,?)" ,"fff" ,1000f ); jdbcTemplate.update("update account set name =?,money=? where id = ?" ,"fff" ,2000f ,7 ); jdbcTemplate.update("delete from account where id = ?" ,7 ); List<Account> query = jdbcTemplate.query("select * from account where money > ?" , new BeanPropertyRowMapper<Account>(Account.class), 100 ); Integer integer = jdbcTemplate.queryForObject("select count(*) from account" , new BeanPropertyRowMapper<Integer>(Integer.class)); } }
Spring中的事务控制 1.基于xml的事务 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 <?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:aop ="http://www.springframework.org/schema/aop" xmlns:tx ="http://www.springframework.org/schema/tx" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd" > <bean id ="jdbcTemplate" class ="org.springframework.jdbc.core.JdbcTemplate" > <property name ="dataSource" ref ="dataSource" > </property > </bean > <bean id ="dataSource" class ="org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name ="driverClassName" value ="com.mysql.cj.jdbc.Driver" > </property > <property name ="url" value ="jdbc:mysql:///mybatis?useUnicode=true& characterEncoding=utf-8& useSSL=false& serverTimezone = GMT" > </property > <property name ="username" value ="root" > </property > <property name ="password" value ="123456" > </property > </bean > <bean id ="transactionManager" class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name ="dataSource" ref ="dataSource" > </property > </bean > <tx:advice id ="txAdvice" transaction-manager ="transactionManager" > <tx:attributes > <tx:method name ="*" propagation ="REQUIRED" read-only ="false" /> <tx:method name ="find*" propagation ="SUPPORTS" read-only ="true" /> </tx:attributes > </tx:advice > <aop:config > <aop:pointcut id ="pt1" expression ="execution(* com.wht.*.*(..))" /> <aop:advisor advice-ref ="txAdvice" pointcut-ref ="pt1" > </aop:advisor > </aop:config > </beans >
2.基于注解的事务 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 <?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:aop ="http://www.springframework.org/schema/aop" xmlns:tx ="http://www.springframework.org/schema/tx" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd" > <context:component-scan base-package ="com.wht" > </context:component-scan > <bean id ="jdbcTemplate" class ="org.springframework.jdbc.core.JdbcTemplate" > <property name ="dataSource" ref ="dataSource" > </property > </bean > <bean id ="dataSource" class ="org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name ="driverClassName" value ="com.mysql.cj.jdbc.Driver" > </property > <property name ="url" value ="jdbc:mysql:///mybatis?useUnicode=true& characterEncoding=utf-8& useSSL=false& serverTimezone = GMT" > </property > <property name ="username" value ="root" > </property > <property name ="password" value ="123456" > </property > </bean > <bean id ="transactionManager" class ="org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name ="dataSource" ref ="dataSource" > </property > </bean > <tx:annotation-driven transaction-manager ="transactionManager" > </tx:annotation-driven > </beans >