logger日志类:

package cn.mepu.utils;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 记录日志,提供公共代码
 * @author shkstart
 * @create 2019-11-09 15:25
 */
@Component("logger")
@Aspect //表示当前类是一个切面类
public class logger {

    //表达式
    @Pointcut("execution(* cn.mepu.service.imp.*.*(..))")
    private void pt1(){

    }
    /**
     * 前置日志
     */
    @Before("pt1()")
    public void beforePrintLog(){
        System.out.println(" logger中前置开始记录日志" );
    }

    /**
     * 后置日志
     */
    @AfterReturning("pt1()")
    public void afterreturningPrintLog(){
        System.out.println(" logger中后置开始记录日志" );
    }

    /**
     * 异常日志
     */
    @AfterThrowing("pt1()")
    public void afterThrowingPrintLog(){
        System.out.println(" logger中异常开始记录日志" );
    }

    /**
     * 最终日志
     */
    @After("pt1()")
    public void afterPrintLog(){
        System.out.println(" logger中最终开始记录日志" );
    }

    //@Around("pt1()")
    public Object aroundPringLog(ProceedingJoinPoint pjp){
        Object reValue = null;
        try {
            Object[] args = pjp.getArgs();//得到方法所需的参数
            //前置通知
            System.out.println("logger中环绕通知开始记录日志前置" );
            pjp.proceed(args);//明确调用切入点方法
            //后置通知
            System.out.println("logger中环绕通知开始记录日志后置" );
            return reValue;
        } catch (Throwable throwable) {
            //异常通知
            System.out.println("logger中环绕通知开始记录日志异常" );
            throw new RuntimeException(throwable);
        }finally {
            //最终通知
            System.out.println("logger中环绕通知开始记录日志最终" );
        }

    }

}

service层:

package cn.mepu.service.imp;

import cn.mepu.service.IAccountService;
import org.springframework.stereotype.Service;

/**
 * 账户业务层实现类
 * @author shkstart
 * @create 2019-11-09 15:23
 */
@Service("accountService")
public class AccountServiceImp implements IAccountService {
    @Override
    public void saveAccount() {
        System.out.println("保存");

    }

    @Override
    public void updateAccount(int i) {
        System.out.println("更新"+i);

    }

    @Override
    public int deleteAccount() {
        System.out.println("删除");
        return 0;
    }
}

测试类:

package cn.mepu.Test;

import cn.mepu.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 测试Aop
 * @author shkstart
 * @create 2019-11-09 15:59
 */
public class AopTest {
    public static void main(String[] args) {
        //1.获取容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("Bean.xml");
        //2.获取对象
        IAccountService as = (IAccountService) ac.getBean("accountService");
        //3.执行方法
        as.saveAccount();

    }
}

Bea文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置spring创建容器时要扫描的包-->
    <context:component-scan base-package="cn.mepu"></context:component-scan>

    <!-- 配置spring开启注解AOP的支持 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

POM文件;

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.mepu</groupId>
    <artifactId>day05_spring_aop_annotation</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>
    </dependencies>

</project>

spring_AOP的注解开发的更多相关文章

  1. Spring_AOP基于AspectJ的注解开发&JDBC的模板使用&事务管理(学习笔记3)

    一:AOP基于AspectJ的注解开发 1,简单的实例: 1)引入相应的jar包 ​ 2)在配置文件里引入相关约束 <beans xmlns="http://www.springfra ...

  2. SpringMVC注解开发初步

    一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...

  3. SpringMVC的注解开发入门

    1.Spring MVC框架简介 支持REST风格的URL 添加更多注解,可完全注解驱动 引入HTTP输入输出转换器(HttpMessageConverter) 和数据转换.格式化.验证框架无缝集成 ...

  4. Struts2框架之-注解开发

    Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action类中每个方法的绑定这是重点,在这里先简单看一下配置文件中的简单配置: <span style=" ...

  5. Java自定义注解开发

    一.背景 最近在自己搞一个项目时,遇到可需要开发自定义注解的需求,对于没有怎么关注这些java新特性的来说,比较尴尬,索性就拿出一些时间,来进行研究下自定义注解开发的步骤以及使用方式.今天在这里记下, ...

  6. Annotation(四)——Struts2注解开发

    Hibernate和Spring框架的开发前边总结了,这次看一下流行的MVC流程框架Struts2的注解开发吧.Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action ...

  7. Annotation(三)——Spring注解开发

    Spring框架的核心功能IoC(Inversion of Control),也就是通过Spring容器进行对象的管理,以及对象之间组合关系的映射.通常情况下我们会在xml配置文件中进行action, ...

  8. Annotation(一)——注解开发介绍

    <p>在编程中,一直强调的一点就是注释的编写,注释的规范等等.尤其是越是核心,程序越复杂,逻辑越多的清空下,注释的编写对我们以后的阅读代码,维护软件起着至关重要的作用.一款软件有着好的注释 ...

  9. spring注解开发中常用注解以及简单配置

    一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...

随机推荐

  1. 西里尔字 俄语 - Cyrillic

    https://zh.wikipedia.org/wiki/%E8%A5%BF%E9%87%8C%E5%B0%94%E5%AD%97%E6%AF%8D 其他编码[编辑] 其他适用西里尔字母的字符编码系 ...

  2. kvm搭建

    今日做公司项目时恰好是这个,然后就做一个博客,希望可以帮助到你们 1.把虚拟机zmedu63内存调成6G以上,因为我们要在VMware虚拟中安装KVM,然后在KVM中再安装虚拟机,需要内存大一些.zm ...

  3. Delphi 格式化函数 Format函数

    function Format(const Format: string; const Args: array of const): string; function Format(const For ...

  4. shell 读取文件的每一行

    1.使用while #!/bin/bash while read line do echo $line done < file.txt #!/bin/bash cat file.txt | wh ...

  5. linux文件查找工具——locate,find

    一文件查找介绍 文件的查找就是在文件系统上查找符合条件的文件. 文件查找的方式:locate, find非实时查找也就是基于数据库查找的locate,效率特别高. 实时查找:find 二locate ...

  6. 云数据库POLARDB产品解读之二:如何做到高性价比

    现在做任何事情都要看投入产出比,对应到数据库上其实就是性价比.POLARDB作为一款阿里自研数据库,经常被问的问题是:性能怎么样?能不能支撑我的业务?价格贵不贵?很显然,在早期调研阶段,对稳定性.可靠 ...

  7. 「NOI2017」游戏 解题报告

    「NOI2017」游戏 \(d\)这么小,你考虑直接对\(d\)个东西暴力 枚举\(x\)为\(a\)或\(b\)(\(c\)就不用了,因为\(a,b\)已经包含\(c\))了,剩下的就是个\(2-s ...

  8. SVN server 服务端修改端口号

    SVN server 服务端修改端口号 在实际使用中可能当安装svn server 服务后,发现与后续其他程序端口冲突, 1.打开svn server 管理界面->操作->properti ...

  9. 为什么要用getBaseContext()方法代替this?(转)

    问:this 常常引用当前的 context.但是有些时候,必须使用getBaseContext()来代替this.就是说使用this会引发错误. 如下面的例子: Spinner spinner = ...

  10. python find()函数

    实例(Python 2.0+)  str1 = "this is string example....wow!!!"; str2 = "exam"; print ...