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. day01 python初识、数据类型、流程控制

    今日内容大纲:1,计算机基础. cpu,内存,硬盘,操作系统.2,python的发展与应用.3,python的历史. 2008年python同时更新了两个版本 1,python2x python3x ...

  2. Sass-属性嵌套

    Sass 中还提供属性嵌套,CSS 有一些属性前缀相同,只是后缀不一样,比如:border-top/border-right,与这个类似的还有 margin.padding.font 等属性.假设你的 ...

  3. 力扣——candy (分糖果) python实现

    题目描述: 中文: 老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分. 你需要按照以下要求,帮助老师给这些孩子分发糖果: 每个孩子至少分配到 1 个糖果. ...

  4. 对struct typedef *的认识

    typedef struct node { ……… }NODE,*PNODE; 应该等价于 typedef struct node NODE;//struct node = NODE,eg:struc ...

  5. OS---磁盘存储器

    1.概述 1.1 磁盘存储器  不仅  容量大.存取速度快  而且  可以随机存取: 现代计算机都配置了  磁盘存储器,以  它  为主  存放文件: 对文件 的操作,都将涉及对磁盘的访问: 1.2 ...

  6. 数组(R语言)

    myarray = <- array (vector, dimensions, dimnames) 例如,生成一个2*3*4的数组: dim1 <- c("A1",&q ...

  7. 存储过程中的in out in out 三种类型的参数

    in 是参数的默认模式,这种模式就是在程序运行的时候已经具有值,在程序体中值不会改变. out模式定义的参数只能在过程体内部赋值,表示该参数可以将某个值传递回调用他的过程 in out 表示高参数可以 ...

  8. bzoj 2364

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2346 比较裸的最短路(' '     ) 水题又多了一道 #include <iost ...

  9. centos下mysql密码修改与远程连接

    之前的服务器,好久没上去过了,今天上去,密码居然又忘了... 要修改mysql的登陆密码,首先要设置 #vim /etc/my.cnf 在[mysqld] 下面加上一句 skip-grant-tabl ...

  10. Linux 应用程序编程基础

    一个计算机应用程序在内存中可以分成两个部分:存放代码的代码段和存放数据的数据段.代码段存放用户编写的代码;数据段存放栈和堆. 相关内存管理函数 #include <stdlib.h> vo ...