博客
关于我
spring 基于注解实现Aop
阅读量:396 次
发布时间:2019-03-05

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

??Spring?AOP??????????

??????????????Spring AOP??????????????????????????AOP???

1. ??????POM??

???????????POM??????????????????Spring?AspectJ?

junit
junit
4.11
test
org.springframework
spring-context
5.0.2.RELEASE
org.aspectj
aspectjweaver
1.8.7

2. ??????????

??????????????????????

package com.ljf.spring.aop.service;public interface IAccountService {    void saveAccount();    void updateAccount(int i);    int deleteAccount();}
package com.ljf.spring.aop.service.impl;import org.springframework.stereotype.Service;@Service("accountService")public class AccountService implements IAccountService {    @Override    public void saveAccount() {        System.out.println("?????");        //int i = 1 / 0; // ??????????    }    @Override    public void updateAccount(int i) {        System.out.println("?????" + i);    }    @Override    public int deleteAccount() {        System.out.println("?????");        return 0;    }}

3. ???????????

??????AOP??????????????????????

package com.ljf.spring.aop.util;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;import org.springframework.stereotype.Component;@Component("logger")@Aspectpublic class Logger {    @Pointcut("execution(* com.ljf.spring.aop.service.impl.*.*(..))")    private void pt1() {}    @Before("pt1()")    public void beforePrintLog() {        System.out.println("?????Logger???beforePrintLog????????????");    }    @AfterReturning("pt1()")    public void afterReturningPrintLog() {        System.out.println("?????Logger???afterReturningPrintLog????????????");    }    @After("pt1()")    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??????????????");        }    }}

4. ??Spring XML?????AOP

?Spring?XML????????????AOP??????????

5. ????

???????????????????

package com.ljf.spring.aop;import com.ljf.spring.aop.service.IAccountService;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {    public static void main(String[] args) {        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");        IAccountService as = (IAccountService) ac.getBean("accountService");        as.saveAccount();    }}

??????????????????Spring AOP?????????????????????????????

转载地址:http://lruzz.baihongyu.com/

你可能感兴趣的文章
nodejs npm常用命令
查看>>
nodejs npm常用命令
查看>>
Nodejs process.nextTick() 使用详解
查看>>
NodeJS yarn 或 npm如何切换淘宝或国外镜像源
查看>>
nodejs 中间件理解
查看>>
nodejs 创建HTTP服务器详解
查看>>
nodejs 发起 GET 请求示例和 POST 请求示例
查看>>
NodeJS 导入导出模块的方法( 代码演示 )
查看>>
nodejs 开发websocket 笔记
查看>>
nodejs 的 Buffer 详解
查看>>
NodeJS 的环境变量: 开发环境vs生产环境
查看>>
nodejs 读取xlsx文件内容
查看>>
nodejs 运行CMD命令
查看>>
Nodejs+Express+Mysql实现简单用户管理增删改查
查看>>
nodejs+nginx获取真实ip
查看>>
nodejs-mime类型
查看>>
NodeJs——(11)控制权转移next
查看>>
NodeJS、NPM安装配置步骤(windows版本)
查看>>
NodeJS、NPM安装配置步骤(windows版本)
查看>>
nodejs下的express安装
查看>>