SpringAOP简述 SpringAOP的设计思想,就是通过动态代理,在运行期对需要使用的业务逻辑方法进行增强。 使用场景如:日志打印、权限、事务控制等。 默认情况下,Spring会根据被代理的对象是否实现接口来选择使用JDK还是CGLIB。当被代理对象没有实现接口时,Spring会选择CGLIB。当实现了接口,Spring会选择JDK官方的代理技术,不过我们也可以通过配置的方式,让Spring强制使用CGLIB。 配置方式有两种:使aop:config标签配置使aop:aspectjautoproxy标签配置aop:aspectjautoproxySpring中AOP的实现 2。1XML模式引入依赖(如果项目里没有的话)dependencygroupIdorg。springframeworkgroupIdspringaopartifactIdversion5。1。12。RELEASEversiondependencydependencygroupIdorg。aspectjgroupIdaspectjweaverartifactIdversion1。9。4versiondependencyxml配置 主要看下面的aop部分?xmlversion1。0encodingUTF8?beansxmlnshttp:www。springframework。orgschemabeansxmlns:xsihttp:www。w3。org2001XMLSchemainstancexmlns:contexthttp:www。springframework。orgschemacontextxmlns:aophttp:www。springframework。orgschemaaopxmlns:txhttp:www。springframework。orgschematxxsi:schemaLocationhttp:www。springframework。orgschemabeanshttps:www。springframework。orgschemabeansspringbeans。xsdhttp:www。springframework。orgschemacontexthttps:www。springframework。orgschemacontextspringcontext。xsdhttp:www。springframework。orgschematxhttps:www。springframework。orgschematxspringtx。xsdhttp:www。springframework。orgschemaaophttps:www。springframework。orgschemaaopspringaop。xsd xml相关切面配置beanidlogUtilclasscom。mmc。ioc。utils。LogUtilbean!aop的配置!aop的配置!配置切面aop:pointcut!前置通知aop:before!后置通知,无论业务是否正常执行aop:after!正常执行aop:afterreturning!异常执行aop:afterthrowing!环绕通知!aop:aroundaop:aspectaop:config 环绕通知可以实现上面的4种通知,并且可以控制业务方法是否执行。通过如下代码控制:proceedingJoinPoint。proceed(proceedingJoinPoint。getArgs());publicclassLogUtil{publicvoidprintLog(){System。out。println(打印日志);}publicvoidafter(){System。out。println(后日志打印,不管业务是否正常);}publicvoidafterReturn(){System。out。println(正常执行完毕打印日志);}publicvoidafterException(){System。out。println(异常执行打印日志);}publicvoidaround(ProceedingJoinPointproceedingJoinPoint){System。out。println(环绕前置);try{ObjectresultproceedingJoinPoint。proceed(proceedingJoinPoint。getArgs());System。out。println(环绕正常执行);}catch(Throwablethrowable){throwable。printStackTrace();System。out。println(环绕异常执行);}}}切入点表达式 举例:publicvoidcom。lagou。service。impl。TransferServiceImpl。updateAccountByCardNo(com。lagou。pojo。Account)访问修饰符可以省略,也就是public可以不用写voidcom。mmc。ioc。service。impl。TransferServiceImpl。transfer(String,String,int)返回值可以用代替,表示返回任意值com。mmc。ioc。service。impl。TransferServiceImpl。transfer(String,String,int)包名可以使用。。表示当前包及其子包com。。TransferServiceImpl。transfer(String,String,int)类名和方法名,都可以使用表示任意类,任意方法com。。(String,String,int))参数列表,如果是基本类型可以直接写名称,如int。引用类型必须用全限定名称参数列表可以使用代替任意参数类型,但必须有参数com。。()参数列表可以使用。。代替任意参数类型,有无参数均可com。。()全通配方式:。。。(。。) 2。2XML注解模式XML中开启Spring对注解AOP的支持!开启spring对注解aop的持注解配置ComponentAspectpublicclassLogUtil{Pointcut(execution(com。mmc。ioc。service。impl。TransferServiceImpl。transfer(。。)))publicvoidpointcut(){}Before(pointcut())publicvoidprintLog(){System。out。println(打印日志);}After(pointcut())publicvoidafter(){System。out。println(后日志打印,不管业务是否正常);}AfterReturning(pointcut())publicvoidafterReturn(){System。out。println(正常执行完毕打印日志);}AfterThrowing(pointcut())publicvoidafterException(){System。out。println(异常执行打印日志);}Around(pointcut())publicvoidaround(ProceedingJoinPointproceedingJoinPoint){System。out。println(环绕前置);try{ObjectresultproceedingJoinPoint。proceed(proceedingJoinPoint。getArgs());System。out。println(环绕正常执行);}catch(Throwablethrowable){throwable。printStackTrace();System。out。println(环绕异常执行);}}} 2。3纯注解模式 只需要用注解EnableAspectJAutoProxy替换掉Spring事务配置 也分为3种模式 3。1XML模式引入pom依赖dependencygroupIdorg。springframeworkgroupIdspringcontextartifactIdversion5。1。12。RELEASEversiondependencydependencygroupIdorg。aspectjgroupIdaspectjweaverartifactIdversion1。9。4versiondependencydependencygroupIdorg。springframeworkgroupIdspringjdbcartifactIdversion5。1。12。RELEASEversiondependencydependencygroupIdorg。springframeworkgroupIdspringtxartifactIdversion5。1。12。RELEASEversiondependencyxml配置beanidtransactionManagerclassorg。springframework。jdbc。datasource。DataSourceTransactionManagerconstructorargnamedataSourcerefdataSourceconstructorargbeantx:adviceidtxAdicetransactionmanagertransactionManager!定制事务细节tx:attributestx:methodnamereadonlyfalsepropagationREQUIREDisolationDEFAULTtx:methodnamequeryreadonlytruepropagationSUPPORTStx:methodtx:attributestx:advice!事务衡器逻辑aop:advisoraop:config 3。2基于XML注解xml配置:!spring声明式事务配置beanidtransactionManagerclassorg。springframework。jdbc。datasource。DataSourceTransactionManagerconstructorargnamedataSourcerefdataSourceconstructorargbeantx:annotationdriventransactionmanagertransactionManagertx:annotationdriven在类或方法上面添加Transactional注解Transactional(readOnlytrue,propagationPropagation。SUPPORTS) 3。3纯注解 用EnableTransactionManagement注解替换掉tx:annotationdriventransactionmanagertransactionManager 即可 书山有路勤为径,学海无涯苦作舟 原文链接:https:www。cnblogs。comjavammcp15569761。html