博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring aop
阅读量:4453 次
发布时间:2019-06-07

本文共 2622 字,大约阅读时间需要 8 分钟。

1 jar:

aopalliance-1.0.0.jar

aspectjweaver-1.6.8.jar
commons-logging-1.2.jar
spring-aop-4.3.3.RELEASE.jar
spring-aspects-4.3.3.RELEASE.jar
spring-beans-4.3.3.RELEASE.jar
spring-context-4.3.3.RELEASE.jar
spring-core-4.3.3.RELEASE.jar
spring-expression-4.3.3.RELEASE.jar

2 在配置文件中加入aop命名空间 基于注解的方式

 

package com.sgcc.aop.impl;public interface ArithmeticCalculator {    public int add(int i,int j);        public int sub(int i,int j);        public int mul(int i,int j);        public int div(int i,int j);}
package com.sgcc.aop.impl;import org.springframework.stereotype.Component;@Componentpublic class ArithmeticCalculatorImpl implements ArithmeticCalculator{    @Override    public int add(int i, int j) {        // TODO Auto-generated method stub        int result = i + j;        return result;    }    @Override    public int sub(int i, int j) {        // TODO Auto-generated method stub        int result = i - j;        return result;    }    @Override    public int mul(int i, int j) {        // TODO Auto-generated method stub        int result = i * j;        return result;    }    @Override    public int div(int i, int j) {        // TODO Auto-generated method stub        int result = i / j;        return result;    }}
package com.sgcc.aop.impl;import java.util.Arrays;import java.util.List;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.stereotype.Component;//把这个类声明为一个切面,把该类放入到IOC容器中,再声明一个切面@Component@Aspectpublic class LoggingAspect {    //声明该方法是一个前置通知:在目标方法开始之前执行    @Before("execution( * com.sgcc.aop.impl.*.*(int , int ))")    public void beforeMethod(JoinPoint joinPoint){        String methodName = joinPoint.getSignature().getName();        List args = Arrays.asList(joinPoint.getArgs());        System.out.println("the method"+methodName+" begins"+args);    }    }
package com.sgcc.aop.impl;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {    public static void main(String[] args) {        ApplicationContext ctx = new ClassPathXmlApplicationContext("appliicationContext.xml");                ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class);                int result = arithmeticCalculator.add(3, 6);        System.out.println("result:"+result);        result = arithmeticCalculator.div(5, 5);        System.out.println("result:"+result);    }}

 

转载于:https://www.cnblogs.com/alloevil/p/6092757.html

你可能感兴趣的文章
.Net MVC 前台验证跟后台验证
查看>>
scrollerview中使用viewpager嵌套fragmentment 滚动事件冲突解决
查看>>
maven 和 Maven的配置
查看>>
Jenkins配置备份恢复插件ThinBackup
查看>>
Dockerfile 构建前端node应用cnpm命令启动nodejs服务
查看>>
OpenWRT中的按键和灯的GPIO控制实现_转
查看>>
进度管理中的常见问题
查看>>
POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE
查看>>
linux运行级别
查看>>
工资调整
查看>>
记:Android 安装apk的代码实现
查看>>
xml弹出框js备份
查看>>
省份二级联动
查看>>
使用Vue时localhost:8080中localhost换成ip地址后无法显示页面的问题
查看>>
PHP 【五】
查看>>
HDU 1241 Oil Deposits
查看>>
POJ 2392 Space Elevator
查看>>
2981:大整数加法-poj
查看>>
hdu Piggy-Bank
查看>>
括号配对nyoj2(疑问)
查看>>