JS 与 trick 代码的运用及解析全攻略
779
2022-12-29
带你了解Spring AOP的使用详解
目录springmvc.xmlBankDaoAdminCheckBankDaoImplLogInfoTransmactionAdminCheckInterceptorLogInfoInceptorTransmactionInterceptorTest总结
springmvc.xml
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:context="http://springframework.org/schema/context" xmlns:aop="http://springframework.org/schema/aop" xmlns:tx="http://springframework.org/schema/tx" xmlns:mvc="http://springframework.org/schema/mvc" xsi:schemaLocation="http://springframework.org/schema/beans https://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/aop https://springframework.org/schema/aop/spring-aop.xsd http://springframework.org/schema/tx https://springframework.org/schema/tx/spring-tx.xsd http://springframework.org/schema/mvc https://springframework.org/schema/mvc/spring-mvc.xsd">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance"
xmlns:context="http://springframework.org/schema/context"
xmlns:aop="http://springframework.org/schema/aop"
xmlns:tx="http://springframework.org/schema/tx"
xmlns:mvc="http://springframework.org/schema/mvc"
xsi:schemaLocation="http://springframework.org/schema/beans
https://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/context
https://springframework.org/schema/context/spring-context.xsd
http://springframework.org/schema/aop
https://springframework.org/schema/aop/spring-aop.xsd
http://springframework.org/schema/tx
https://springframework.org/schema/tx/spring-tx.xsd
http://springframework.org/schema/mvc
https://springframework.org/schema/mvc/spring-mvc.xsd">
BankDao
package cn.hp.dao;
public interface BankDao {
/**
* 存钱
*/
public void remit();
/**
* 取钱
*/
public void withdrawMoney();
/**
* 转账
*/
public void transferAccounts();
}
AdminCheck
package cn.hp.impl;
public class AdminCheck {
public void check(){
System.out.println("正在验证账号信息");
}
}
BankDaoImpl
package cn.hp.impl;
import cn.hp.dao.BankDao;
public class BankDaoImpl implements BankDao {
@Override
public void remit() {
// adminCheck.check();
// transmaction.beginTransmaction();
System.out.println("存钱的业务逻辑");
// transmaction.closeTransmaction();
}
@Override
public void withdrawMoney() {
// adminCheck.check();
// transmaction.beginTransmaction();
System.out.println("取钱的业务逻辑");
// transmaction.closeTransmaction();
}
@Override
public void transferAccounts() {
System.out.println("转账的业务逻辑");
}
}
LogInfo
package cn.hp.impl;
public class LogInfo {
public void write(){
System.out.println("写日志中");
}
}
Transmaction
package cn.hp.impl;
import org.aspectj.lang.ProceedingJoinPoint;
public class Transmaction {
public void beginTransmaction(){
System.out.println("开始事务");
}
public void closeTransmaction(){
System.out.println("结束事务");
}
public void dointcepter(ProceedingJoinPoint point) throws Throwable {
beginTransmaction();
point.proceed();
closeTransmaction();
}
}
AdminCheckInterceptor
package cn.hp.interceptor;
import cn.hp.impl.AdminCheck;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
import java.util.Set;
public class AdminCheckInterceptor implements MethodBeforeAdvice {
private AdminCheck adminCheck;
public AdminCheck getAdminCheck() {
return adminCheck;
}
public void setAdminCheck(AdminCheck adminCheck) {
this.adminCheck = adminCheck;
}
@Override
public voidhttp:// before(Method method, Object[] objects, Object o) throws Throwable {
adminCheck.check();
}
}
LogInfoInceptor
package cn.hp.interceptor;
import cn.hp.impl.LogInfo;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class LogInfoInceptor implements AfterReturningAdvice {
private LogInfo logInfo;
public LogInfo getLogInfo() {
return logInfo;
}
public void setLogInfo(LogInfo logInfo) {
this.logInfo = logInfo;
}
@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
logInfo.write();
}
}
TransmactionInterceptor
package cn.hp.interceptor;
import cn.hp.impl.Transmaction;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.ibatis.transaction.Transaction;
public class TransmactionInterceptor implements MethodInterceptor {
private Transmaction transmaction;
public Transmaction getTransmaction() {
return transmaction;
}
public void setTransmaction(Transmaction transmaction) {
this.transmaction = transmaction;
}
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
transmaction.beginTransmaction();
Object obj = invocation.proceed();
transmaction.closeTransmaction();
return obj;
}
}
Test
package cn.hp.test;
import cn.hp.dao.BankDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
//test1();
// test2();
test3();
}
private static void test3() {
ApplicationContext ac= new ClassPathXmlApplicationContext("springmvc.xml");
BankDao proxyFactory = ac.getBean("proxyFactory", BankDao.class);
proxyFactory.remit();
}
private static void test2() {
ApplicationContext ac = new ClassPathXmlApplicationContext("springmvc.xml");
BankDao bd = ac.getBean("bankDao",BankDao.class);
bd.transferAccounts();
}
private static void test1() {
ApplicationContext ac = new ClassPathXmlApplicationContext("springmvc.xml");
BankDao bd = ac.getBean("bankDao",BankDao.class);
bd.withdrawMoney();
}
}
总结
本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注我们的更多内容!
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~