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

你可能感兴趣的文章
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
node exporter完整版
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
查看>>
Node 裁切图片的方法
查看>>