Spring框架 - AOP配置文件形式
目录AOP什么是AOP什么是横切面关注点AOP的优势AOP底层原理AOP实现形式AOP核心术语AOP - 配置文件形式切入点的表达式通知类型转账案例操作AOP什么是AOPAOPAspect Oriented Programming的缩写)意为面向切面编程是一种编程范式用于解决横切面关注点的问题。什么是横切面关注点横切关注点是指那些跨域应用程序多个模块的功能例如日志记录事务管理安全控制性能监控异常处理AOP的优势运行期间在不修改代码的情况下对已有的方法进行增强。减少重复代码提高开发效率方便维护AOP底层原理JDK的动态代理技术Cglib的动态代理技术AOP实现形式配置文件形式半注解形式全注解形式AOP核心术语Joinpoint(连接点)是指那些被拦截到的点在spring中,这些点指的是方法Pointcut(切入点)是指要对哪些Joinpoint进行拦截的定义Advice(通知/增强)是指拦截到Joinpoint之后所要做的事情就是通知。通知分为前置通知后置通知异常通知最终通知环绕通知(四个通知的总体)Target(目标对象)代理的目标对象Weaving(织入)是指把增强应用到目标对象来创建新的代理对象的过程Proxy(代理) 一个类被AOP织入增强后就产生一个结果代理类由Spring提供Aspect(切面)是切入点和通知的结合自己编写和配置AOP - 配置文件形式切入点的表达式execution([修饰符] 返回值类型 包名.类名.方法名(参数))修饰符可以省略不写不是必须要出现的返回值类型是不能省略不写的根据你的方法来编写返回值。可以使用 * 代替。示例精准表达式execution(public void com.qcbyjy.demo2.UserServiceImpl.save())通用表达式execution(* com.qcby.*.*ServiceImpl.*(..))save开头的所有方法execution(* com.qcby.*.*ServiceImpl.save*(..))通知类型类型含义AOP切面声明标签实例前置通知目标方法执行前进行增强aop:before后置通知目标方法执行成功或失败进行增强aop:after-returning异常通知目标方法执行成功后进行增强aop:after-throwing最终通知目标方法执行失败后进行增强aop:after环绕通知(四个通知的总体)目标方法执行前后都可以进行增强。目标对象的方法需要手动执行。aop:around示例!--配置AOP的增强--aop:config!--配置切面 切入点 通知组成--aop:aspectrefmyAspect!--前置通知--aop:beforemethodlogpointcutexecution(* com.qcby.*.*ServiceImpl.save*(..))/aop:before!--后置通知--aop:after-returningmethodlogpointcutexecution(* com.qcby.*.*ServiceImpl.save*(..))/aop:after-returning!--异常通知--aop:after-throwingmethodlogpointcutexecution(* com.qcby.*.*ServiceImpl.save*(..))/aop:after-throwing!--最终通知--aop:aftermethodlogpointcutexecution(* com.qcby.*.*ServiceImpl.save*(..))/aop:after!--环绕通知--aop:aroundmethodaroundLogpointcutexecution(* com.qcbyjy.*.*ServiceImpl.save*(..))//aop:aspect/aop:config转账案例操作创建maven Java项目编写pom.xml文件引入依赖新加入了AOP联盟、Spring Aspects、aspectj这三个依赖dependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.0.2.RELEASE/version/dependencydependencygroupIdcommons-logging/groupIdartifactIdcommons-logging/artifactIdversion1.2/version/dependencydependencygroupIdlog4j/groupIdartifactIdlog4j/artifactIdversion1.2.12/version/dependency!--有单元测试的环境Spring5版本Junit4.12版本--dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.12/versionscopetest/scope/dependency!-- 连接池 --dependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion1.1.10/version/dependency!-- mysql驱动包 --dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion5.1.6/version/dependency!-- Spring整合Junit测试的jar包 --dependencygroupIdorg.springframework/groupIdartifactIdspring-test/artifactIdversion5.0.2.RELEASE/versionscopetest/scope/dependency!-- AOP联盟 --dependencygroupIdaopalliance/groupIdartifactIdaopalliance/artifactIdversion1.0/version/dependency!-- Spring Aspects --dependencygroupIdorg.springframework/groupIdartifactIdspring-aspects/artifactIdversion5.0.2.RELEASE/version/dependency!-- aspectj --dependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactIdversion1.8.3/version/dependency/dependencies创建Spring配置文件applicationContext.xml引入具体的AOP的schema约束?xml version1.0encodingUTF-8?beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://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/beans编写实体类Account类编写持久层的接口和实现类AccountDao类和AccountDaoImpl类packagecom.qcby.dao;publicinterfaceAccountDao{/** * 转账 * param name 转账人 * param money 金额 */publicvoidoutMoney(Stringname,doublemoney);/** * 收账 * param name 收帐人 * param money 金额 */publicvoidinMoney(Stringname,doublemoney);}packagecom.qcby.dao.impl;importcom.qcby.dao.AccountDao;importcom.qcby.tools.TxUtils;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.SQLException;publicclassAccountDaoImplimplementsAccountDao{OverridepublicvoidoutMoney(Stringname,doublemoney){try{//把数据存储到数据库中//先获取到连接ConnectionconnTxUtils.getConnection();//编写sql语句Stringsqlupdate account set money money - ? where name ?;//预编译sql语句PreparedStatementstmtconn.prepareStatement(sql);//设置值stmt.setDouble(1,money);stmt.setString(2,name);//执行操作stmt.execute();//关闭资源conn不能关闭stmt.close();}catch(SQLExceptione){e.printStackTrace();}}OverridepublicvoidinMoney(Stringname,doublemoney){try{ConnectionconnTxUtils.getConnection();Stringsqlupdate account set money money ? where name ?;PreparedStatementstmtconn.prepareStatement(sql);stmt.setDouble(1,money);stmt.setString(2,name);stmt.execute();stmt.close();}catch(SQLExceptione){e.printStackTrace();}}}编写业务层的接口和实现类AccountService类和Account ServiceImpl类packagecom.qcby.service;publicinterfaceAccountService{/** * * param out 转账 * param in 收账 * param money 金额 */publicvoidpay(Stringout,Stringin,doublemoney);}packagecom.qcby.service.impl;importcom.qcby.dao.AccountDao;importcom.qcby.service.AccountService;publicclassAccountServiceImplimplementsAccountService{privateAccountDaoaccountDao;publicvoidsetAccountDao(AccountDaoaccountDao){this.accountDaoaccountDao;}/** * * param out 转账人 * param in 收帐人 * param money 金额 */Overridepublicvoidpay(Stringout,Stringin,doublemoney){// 调用dao中方法accountDao.outMoney(out,money);accountDao.inMoney(in,money);}}编写切片类MyAspect类packagecom.qcby.tools;//切面类publicclassMyAspect{//开启事务publicvoidstart(){TxUtils.startTransaction();}//提交事务publicvoidcommit(){TxUtils.commit();}//回滚事务publicvoidrollback(){TxUtils.rollback();}//关闭事务publicvoidclose(){TxUtils.close();}}将目标类配置到Spring中beanidaccountServiceclasscom.qcby.service.impl.AccountServiceImplpropertynameaccountDaorefaccountDao/property/beanbeanidaccountDaoclasscom.qcby.dao.impl.AccountDaoImpl/bean在配置文件中定义切面类beanidmyAspectclasscom.qcby.tools.MyAspect/bean在配置文件中完成aop的配置aop:config!--配置切面 切入点 通知组成--aop:aspectrefmyAspect!--前置通知开启事务--aop:beforemethodstartpointcutexecution(* com.qcby.service.impl.AccountServiceImpl.*(..))/aop:before!--后置通知提交事务--aop:after-returningmethodcommitpointcutexecution(* com.qcby.service.impl.AccountServiceImpl.*(..))/aop:after-returning!--异常通知回滚事务--aop:after-throwingmethodrollbackpointcutexecution(* com.qcby.service.impl.AccountServiceImpl.*(..))/aop:after-throwing!--最终通知关闭连接--aop:aftermethodclosepointcutexecution(* com.qcby.service.impl.AccountServiceImpl.*(..))/aop:after/aop:aspect/aop:config编程测试类AccountTest类packagecom.qcby.test;importcom.qcby.service.AccountService;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.test.context.ContextConfiguration;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;RunWith(SpringJUnit4ClassRunner.class)ContextConfiguration(classpath:applicationContext.xml)publicclassAccountTest{AutowiredpublicAccountServiceaccountService;TestpublicvoidtestPay(){accountService.pay(喜羊羊,懒羊羊,100);}}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2558564.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!