博客
关于我
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/

你可能感兴趣的文章
NLP 项目:维基百科文章爬虫和分类【01】 - 语料库阅读器
查看>>
NLP_什么是统计语言模型_条件概率的链式法则_n元统计语言模型_马尔科夫链_数据稀疏(出现了词库中没有的词)_统计语言模型的平滑策略---人工智能工作笔记0035
查看>>
NLP三大特征抽取器:CNN、RNN与Transformer全面解析
查看>>
NLP学习笔记:使用 Python 进行NLTK
查看>>
NLP度量指标BELU真的完美么?
查看>>
NLP的不同研究领域和最新发展的概述
查看>>
NLP的神经网络训练的新模式
查看>>
NLP采用Bert进行简单文本情感分类
查看>>
NLP问答系统:使用 Deepset SQUAD 和 SQuAD v2 度量评估
查看>>
NLP项目:维基百科文章爬虫和分类【02】 - 语料库转换管道
查看>>
NLP:使用 SciKit Learn 的文本矢量化方法
查看>>
nmap 使用方法详细介绍
查看>>
Nmap扫描教程之Nmap基础知识
查看>>
nmap指纹识别要点以及又快又准之方法
查看>>
Nmap渗透测试指南之指纹识别与探测、伺机而动
查看>>
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>