spring--AOP--日志---demo1---bai
AOP日志DEMO1: 实体类: package com.etc.entity; import org.aspectj.lang.annotation.Pointcut; public class User implements IUser
{
public static int NORMAL = 1;//普通用户角色
public static int ADMIN = 2; //管理员角色
private int role; //所属的角色 public int getRole() {
return role;
} public void setRole(int role) {
this.role = role;
} public void login() {
System.out.println("执行登录了!"); } public void reg() {
System.out.println("执行注册了!");
//throw new RuntimeException("注册过程发生异常!");
} }
================================================================
实体类需实现的接口: package com.etc.entity; //定义用户接口
public interface IUser
{
void login(); //登录
void reg(); //注册 }
=================================================================
通知类: package com.etc.advice; import org.aspectj.lang.ProceedingJoinPoint; //自定义通知类
public class MyAdvice
{
public void beforelog()
{
System.out.println("这是前置通知的日志!");
} public void afterlog()
{
System.out.println("这是后置通知的日志!");
} public void aroundlog(ProceedingJoinPoint point) throws Throwable
{
System.out.println("这是环绕前通知的日志!");
point.proceed();//原来代码的位置
System.out.println("这是环绕后通知的日志!");
} public void throwlog()
{
System.out.println("这是抛出异常后通知的日志");
} }
=================================================================
配置文件: <?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 定义1个业务类对象 -->
<bean id="user" class="com.etc.entity.User">
<property name="role" value="2"></property>
</bean>
<!-- 定义1个通知类对象 -->
<bean id="myadv" class="com.etc.advice.MyAdvice">
</bean>
<aop:config>
<!-- 配置切点的集合、即切线 -->
<aop:pointcut expression="execution(* com.etc.entity.User.*(..))" id="mypc"/>
<!-- 配置切入的方向 ,即切面-->
<aop:aspect ref="myadv">
<!-- 前置通知
<aop:before method="beforelog" pointcut-ref="mypc"/>
-->
<!-- 后置通知
<aop:after method="afterlog" pointcut-ref="mypc"/>
-->
<!-- 环绕通知
<aop:around method="aroundlog" pointcut-ref="mypc"/>
-->
<!-- 异常通知
<aop:after-throwing method="throwlog" pointcut-ref="mypc"/>
-->
</aop:aspect>
</aop:config> </beans>
===============================================
测试类:
package com.etc.test; import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.etc.entity.IUser; public class Test {
public static void main(String[] args)
{
BeanFactory fac = new ClassPathXmlApplicationContext("applicationContext.xml");
IUser u = (IUser) fac.getBean("user"); //执行业务方法
u.login();
System.out.println("====="); try
{
u.reg();
} catch (Exception e)
{ }
}
}
======================================================================
spring--AOP--日志---demo1---bai的更多相关文章
- Spring AOP日志实现(四)--Bean的设计
日志Bean的设计: 类名及方法名:
- Spring AOP日志实现(一)
前置通知:获取访问的类,访问的方法,带参数和不带参数的 日志表信息描述字段: 获取访问时长:
- Spring AOP日志实现(三)--获取访问者用户名
通过Security获取访问者用户名: 也可以通过session来获取: 整体思路:
- Spring AOP日志实现(二)--获取访问者IP及访问路径
获取类及方法上的@RequestMapping注解: 应该是不等于: 获取访问者的ip地址,首先配置一个监听器: 配置完监听器后,就可以在类中注入一个HttpServletRequest: 获取ip:
- Spring AOP 实现写事件日志功能
什么是AOP?AOP使用场景?AOP相关概念?Spring AOP组件?如何使用Spring AOP?等等这些问题请参考博文:Spring AOP 实现原理 下面重点介绍如何写事件日志功能,把日志保存 ...
- spring aop学习记录
许多AOP框架,比较常用的是Spring AOP 与AspectJ.这里主要学习的Spring AOP. 关于AOP 日志.事务.安全验证这些通用的.散步在系统各处的需要在实现业务逻辑时关注的事情称为 ...
- 面试官:连Spring AOP都说不明白,自己走还是我送你?
前言 因为假期原因,有一段时间没给大家更新了!和大家说个事吧,放假的时候一位粉丝和我说了下自己的被虐经历,在假期前他去某互联网公司面试,结果直接被人家面试官Spring AOP三连问给问的一脸懵逼!其 ...
- Spring AOP在函数接口调用性能分析及其日志处理方面的应用
面向切面编程可以实现在不修改原来代码的情况下,增加我们所需的业务处理逻辑,比如:添加日志.本文AOP实例是基于Aspect Around注解实现的,我们需要在调用API函数的时候,统计函数调用的具体信 ...
- TinyFrame再续篇:整合Spring AOP实现日志拦截
上一篇中主要讲解了如何使用Spring IOC实现依赖注入的.但是操作的时候,有个很明显的问题没有解决,就是日志记录问题.如果手动添加,上百个上千个操作,每个操作都要写一遍WriteLog方法,工作量 ...
- Spring AOP 完成日志记录
Spring AOP 完成日志记录 http://hotstrong.iteye.com/blog/1330046
随机推荐
- 【P2629】好消息,坏消息(前缀和+单调队列优化DP)
一激动一颓就会忘了总结... 前面的大黄题就不总结了. 这个题我只想说一声艹,一开始的思路就是正确的,然后计算的时候有了一个瑕疵,不过很快也就改过来了.然后却一直连样例都过不了.仔仔细细看了老半天,经 ...
- Apache Phoenix基本操作-1
本篇我们将介绍phoenix的一些基本操作. 1. 如何使用Phoenix输出Hello World? 1.1 使用sqlline终端命令 sqlline.py SZB-L0023780:2181:/ ...
- poj 1061 青蛙的约会 扩展欧几里德
青蛙的约会 Time Limit: 1000MS Memory Limit: 10000K Description 两 只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们 ...
- Lightoj 1370 素数打表 +二分
1370 - Bi-shoe and Phi-shoe PDF (English) Statistics Time Limit: 2 second(s) Memory Limit: 32 MB ...
- Functions should do one thing一个函数应该只做一件事
if you take nothing else away from this guide other than this, you'll be ahead of many developers. 如 ...
- js dom操作选择器,dom操作复习
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Windows环境下CGAL的安装
1 准备工作 下载cmake 下载CGAL安装包 学习如何设置环境变量 安装Qt运行demos. libQGLViewer用来运行 3D CGAL demos. 确定Visual Studio 相应的 ...
- 如何在阿里云上部署war包到tomcat服务器
一. 准备工作:xshell和xftp 首先我们得确保,xshell能够远程连接阿里云ECS,xftp能够保证windows和linux之间的文件传输(当然也可以选择FileZilla,但xftp感觉 ...
- 51nod 1215 单调栈/迭代
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1215 1215 数组的宽度 题目来源: Javaman 基准时间限制:1 ...
- 缓存淘汰算法--LRU算法(转)
(转自:http://flychao88.iteye.com/blog/1977653) 1. LRU1.1. 原理 LRU(Least recently used,最近最少使用)算法根据数据的历史访 ...