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
随机推荐
- python 操作mongoDB数据库
网上关于python 操作mongoDB的相关文章相对不是很多,并且质量也不是很高!下面给出一个完整的 增删改查示例程序! #!/usr/bin/python # -*- coding: utf-8 ...
- Difference between stem and lemma
lemma与stem的区别 Difference between stem and lemma 先从wikipedia上看看什么是stem,什么是lemma? Lemma(morphology):In ...
- Linux ls命令参数详解
-a -- 全部(all).列举目录中的全部文件,包括隐藏文件(.filename).位于这个列表的起首处的 .. 和 . 依次是指父目录和你的当前目录. -l -- 长(long).列举目 ...
- cplusplus.com
1/ http://www.cplusplus.com/reference/map/multimap/find/ 2. C
- windows7下cmd窗口使用ssh命令
http://jingyan.baidu.com/article/36d6ed1f931eb31bcf4883dc.html
- JSON 在JavaScript 中的应用及自己的理解
[对象和json]JSON:JavaScript 对象表示法(JavaScript Object Notation).JSON 是存储和交换文本信息的语法.类似 XML.JSON是一种数据格式,不是一 ...
- Python爬虫之利用BeautifulSoup爬取豆瓣小说(二)——回车分段打印小说信息
在上一篇文章中,我主要是设置了代理IP,虽然得到了相关的信息,但是打印出来的信息量有点多,要知道每打印一页,15个小说的信息全部会显示而过,有时因为屏幕太小,无法显示全所有的小说信息,那么,在这篇文章 ...
- hdu 5237 Base64(模拟)
Base64 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- mysql查询哪张表数据最大
转载:https://blog.csdn.net/qq13650793239/article/details/81142134 mysql数据库中information_schema 数据库存储了数据 ...
- SDK中常用的工具
Android SDK包含了各种各样的定制工具,简介如下: 一.Android模拟器(Android Emulator )它是在你的计算机上运行的一个虚拟移动设备.你可以使用模拟器来在一个实际的And ...