对方法进行日志输出是一种很常见的功能。传统的做法是把输出语句写在方法体的内部,在调用该方法时,用输入语句输出信息来记录方法的执行!

1.先写一个普通类:

package com.importnew;

public class Common {

    public void execute(String username,String password){
System.out.println("------------------执行 execute()方法----------------");
}
}

2.写一个切面类,用于合法性校验和日志添加:

package com.importnew;
import org.aspectj.lang.JoinPoint;
public class Check {
public void checkValidity(){
System.out.println("------------------验证合法性----------------");
} public void addLog(JoinPoint j){
System.out.println("------------------添加日志----------------");
Object obj[] = j.getArgs();
for(Object o :obj){
System.out.println(o);
}
System.out.println("========checkSecurity=="+j.getSignature().getName());//这个是获得方法名
}
}

3.配置AOP,使用XML方式:(注意红色标志的内容)

<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="common" class="com.importnew.Common"/>
<bean id="check" class="com.importnew.Check"/> <aop:config>
<aop:aspect id="myAop" ref="check">
<aop:pointcut id="target" expression="execution(* com.importnew.Common.execute(..))"/>
<aop:before method="checkValidity" pointcut-ref="target"/>
<aop:after method="addLog" pointcut-ref="target"/>
</aop:aspect>
</aop:config>
</beans>

注意:aop pointcut表达式(*)
execution(方法修饰符+返回值 完整类名+方法名(方法参数))
例如:
A、execution(public void *(..)):所有返回值是public void的方法都会被拦截到
B、execution(public void day6.com.beans.PersonService.*(..)):表示day6.com.beans.PersonService下所有返回值是public void的方法都会被拦截到
C、
execution(public void
day6.com.beans.PersonService.save(java.lang.String...)):表示
day6.com.beans.PersonService类中的第一个形参类型是String的save方法会被拦截到
D、execution(public void save(..)):表示所有类中的save方法会被拦截到
E、execution(public void day6.com.service..*(..)):表示day6.com.service包下的类以及子包的类所有public void方法都会被拦截到
F、execution(public !void day6.com.service..*(..)):表示day6.com.service包下的类以及子包的类所有public 不是void返回类型的方法都会被拦截到

4.最后写一个测试:

package test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.importnew.Common; public class Client { public static void main(String[] args) {
BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
Common c=(Common) factory.getBean("common");
c.execute("fuckyou","fuckme");
}
}

注意:

需要添加三个包:spring-aop.jar , aspectjrt.jar ,aspectjweaver.jar,否则会报错。

输出结果:

------------------验证合法性----------------
------------------执行 execute()方法----------------
------------------添加日志----------------
fuckyou
fuckme
========checkSecurity==execute

////end

spring:利用Spring AOP 使日志输入与方法分离的更多相关文章

  1. Spring Boot 使用 Aop 实现日志全局拦截

    前面的章节我们学习到 Spring Boot Log 日志使用教程 和 Spring Boot 异常处理与全局异常处理,本章我们结合 Aop 面向切面编程来实现全局拦截异常并记录日志. 在 Sprin ...

  2. spring boot集成aop实现日志记录

    1.pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  3. spring boot的gradle整合日志

    1.引入包configurations { providedRuntime // remove default logger all*.exclude group: 'org.springframew ...

  4. 利用Spring AOP自定义注解解决日志和签名校验

    转载:http://www.cnblogs.com/shipengzhi/articles/2716004.html 一.需解决的问题 部分API有签名参数(signature),Passport首先 ...

  5. (转)利用Spring AOP自定义注解解决日志和签名校验

    一.需解决的问题 部分API有签名参数(signature),Passport首先对签名进行校验,校验通过才会执行实现方法. 第一种实现方式(Origin):在需要签名校验的接口里写校验的代码,例如: ...

  6. 利用Spring AOP和自定义注解实现日志功能

    Spring AOP的主要功能相信大家都知道,日志记录.权限校验等等. 用法就是定义一个切入点(Pointcut),定义一个通知(Advice),然后设置通知在该切入点上执行的方式(前置.后置.环绕等 ...

  7. 利用spring AOP 和注解实现方法中查cache-我们到底能走多远系列(46)

    主题:这份代码是开发中常见的代码,查询数据库某个主表的数据,为了提高性能,做一次缓存,每次调用时先拿缓存数据,有则直接返回,没有才向数据库查数据,降低数据库压力. public Merchant lo ...

  8. Spring Boot 入门(五):集成 AOP 进行日志管理

    本篇文章是接着 Spring boot 入门(四):集成 Shiro 实现登陆认证和权限管理写的,按照前面几篇博客的教程,可以搭建一个简单的项目,主要包含了 Pagehelper+MyBatis 分页 ...

  9. spring aop实现日志收集

    概述 使用spring aop 来实现日志的统一收集功能 详细 代码下载:http://www.demodashi.com/demo/10185.html 使用spring aop 来实现日志的统一收 ...

随机推荐

  1. Element type "LinearLayout" must be followed by either attribute specifications, ">" or "/>"的解决办法

    看老师的word文档开始学习.复制了一段代码,在layout中新建了一个Android XML file,发现有提示错误. 代码如下: <?xml version="1.0" ...

  2. javascript部分知识点

    1:script放置位置: a:<title></title>之后 b:<body>之后 c:<body>中的<div></div&g ...

  3. Rsync 传输不需要输入密码

    1.背景 1)        一个作为服务器端:VM3(IP: 3.9.8.151) 2)        一个作为客户端:VM2(IP: 3.9.8.157) 3)        服务器端和客户端网络 ...

  4. Hive扩展功能(七)--Hive On Spark

    软件环境: linux系统: CentOS6.7 Hadoop版本: 2.6.5 zookeeper版本: 3.4.8 主机配置: 一共m1, m2, m3这五部机, 每部主机的用户名都为centos ...

  5. JS——属性绑定

    1.普通形式 <script> var stu = new Object(); stu.name = "ww"; console.log(stu);//{name: & ...

  6. zabbix配置邮件报警(第四篇)

    zabbix配置邮件报警(第四篇) 邮件报警可采用本地邮件服务,也可以自定义脚本,这里我采用本地邮件服务报警 添加收件人

  7. C++编写谷歌日历

    #include<iostream> #include<fstream> using namespace std; void main() //程序从这里开始运行 { int ...

  8. 一、Scrapy入门教程

    本文转载自以下链接:https://scrapy-chs.readthedocs.io/zh_CN/latest/intro/tutorial.html 在本篇教程中,我们假定您已经安装好Scrapy ...

  9. 关于MySQL中自增的理解和设置

    show create table t10;--查看表的创建结果 show create table t10\G;--竖列查看 --设置自增为20 ); insert into t2(name) va ...

  10. JavaScript学习笔记之DOM介绍

    目录 1.简介 2.方法 3.属性 4.访问节点 5.修改节点 6.添加节点 7.删除节点 8.替换节点 9.改变 CSS 1.简介 文档对象模型(Document Object Model,DOM) ...