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的更多相关文章

  1. Spring AOP日志实现(四)--Bean的设计

    日志Bean的设计: 类名及方法名:

  2. Spring AOP日志实现(一)

    前置通知:获取访问的类,访问的方法,带参数和不带参数的 日志表信息描述字段: 获取访问时长:

  3. Spring AOP日志实现(三)--获取访问者用户名

    通过Security获取访问者用户名: 也可以通过session来获取: 整体思路:

  4. Spring AOP日志实现(二)--获取访问者IP及访问路径

    获取类及方法上的@RequestMapping注解: 应该是不等于: 获取访问者的ip地址,首先配置一个监听器: 配置完监听器后,就可以在类中注入一个HttpServletRequest: 获取ip:

  5. Spring AOP 实现写事件日志功能

    什么是AOP?AOP使用场景?AOP相关概念?Spring AOP组件?如何使用Spring AOP?等等这些问题请参考博文:Spring AOP 实现原理 下面重点介绍如何写事件日志功能,把日志保存 ...

  6. spring aop学习记录

    许多AOP框架,比较常用的是Spring AOP 与AspectJ.这里主要学习的Spring AOP. 关于AOP 日志.事务.安全验证这些通用的.散步在系统各处的需要在实现业务逻辑时关注的事情称为 ...

  7. 面试官:连Spring AOP都说不明白,自己走还是我送你?

    前言 因为假期原因,有一段时间没给大家更新了!和大家说个事吧,放假的时候一位粉丝和我说了下自己的被虐经历,在假期前他去某互联网公司面试,结果直接被人家面试官Spring AOP三连问给问的一脸懵逼!其 ...

  8. Spring AOP在函数接口调用性能分析及其日志处理方面的应用

    面向切面编程可以实现在不修改原来代码的情况下,增加我们所需的业务处理逻辑,比如:添加日志.本文AOP实例是基于Aspect Around注解实现的,我们需要在调用API函数的时候,统计函数调用的具体信 ...

  9. TinyFrame再续篇:整合Spring AOP实现日志拦截

    上一篇中主要讲解了如何使用Spring IOC实现依赖注入的.但是操作的时候,有个很明显的问题没有解决,就是日志记录问题.如果手动添加,上百个上千个操作,每个操作都要写一遍WriteLog方法,工作量 ...

  10. Spring AOP 完成日志记录

    Spring AOP 完成日志记录 http://hotstrong.iteye.com/blog/1330046

随机推荐

  1. HBase学习2(HBase shell)

    HBase 常用命令 查看当前用户:whoami 创建表:create '表名', '列族名1','列族名2','列族名N' 查看所有表:list 描述表: describe '表名' 判断表存在: ...

  2. spring mvc 官方下载

    1.进入https://spring.io 2.选择projects选项卡 3.点击spring frawewoek 4.选择右中方的Reference 5.选择2.3章节 6.点击 Distribu ...

  3. [转载]JDBC读写Oracle的CLOB、BLOB

    JDBC读写Oracle10g的CLOB.BLOB http://lavasoft.blog.51cto.com/62575/321882/ 在Oracle中存取BLOB对象实现文件的上传和下载 ht ...

  4. PowerDesigner(一)-PowerDesigner概述(系统分析与建模)

     PowerDesigner概述 PowerDesigner是Sybase公司推出的一个集成了企业架构,UML(统一建模语言)和数据库的CASE(计算机辅助软件工程)工具.它不仅可以用于系统设计和开发 ...

  5. 五一培训 清北学堂 DAY3

    今天是钟皓曦老师的讲授~ 今天的内容:动态规划 1.动态规划 动态规划很难总结出一套规律 例子:斐波那契数列  0,1,1,2,3,5,8,…… F[0]=0 F[1]=1 F[[n]=f[n-1]+ ...

  6. LeetCode OJ:Binary Tree Right Side View(右侧视角下的二叉树)

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  7. static数据成员与const数据成员的定义与初始化

    三种数据类型的初始化 1.static int a 的初始化 const int a 的初始化 static const int a的初始化 三种初始化方式 在类外初始化 在构造函数中通过初始化列表初 ...

  8. Java 利用Gson将json字符串转换为List<Map<String, String>>

    json字符串类似于: [ { "userPhone": "123", "userNo": "123-2", " ...

  9. (转)Linux sort命令

    Linux 的 ‘sort’命令的14个有用的范例(一) 2015-5-2 10:29    评论: 3 收藏: 10 编译自:http://www.tecmint.com/sort-command- ...

  10. angularjs控制输入框只输入数字及最多输入两位小数

    ps:示例中作用在循环中,其它的你可以按实际需求进行修改使用 <input type="text" ng-model="item.productNumber&quo ...