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
随机推荐
- 为什么原生的servlet是线程不安全的而Struts2是线程安全的?
因为原生的servlet在整个application生命周期中,只在初次访问的时候实例化一次,以后都不会再实例化,只会调用Server方法进行响应,所以如果在servlet类中定义成员变量,那么就会让 ...
- 泛型学习第四天——List泛型终结:什么是List泛型,泛型筛选,泛型排序
为什么要用泛型集合? 在C# 2.0之前,主要可以通过两种方式实现集合: a.使用ArrayList 直接将对象放入ArrayList,操作直观,但由于集合中的项是Object类型,因此每次使用都必须 ...
- 用js将一个数组合并到另一个数组中
var arr1 = ["one","two","three"]; var arr2 = ["1","2&qu ...
- JavaScript -- 练习 window 流氓广告
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Spring获取bean的一种方式
随便一百度,网上一大把,并且还不止一种.所以这里就只记录目前用的一种好了. 实现ApplicationContextAware接口 即可: import org.springframework.bea ...
- 【R】数据导入读取read.table函数详解,如何读取不规则的数据(fill=T)
函数 read.table 是读取矩形格子状数据最为便利的方式.因为实际可能遇到的情况比较多,所以预设了一些函数.这些函数调用了 read.table 但改变了它的一些默认参数. 注意,read.t ...
- 为mac终端添加tree命令
原文:http://superuser.com/questions/359723/mac-os-x-equivalent-of-the-ubuntu-tree-command/ 整理步骤如下: $ t ...
- hdu 5238 Calculator(线段树,中国剩余定理¥)
Calculator Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
- IIFE 立即执行的函数表达式
介绍IIFE IIFE的性能 使用IIFE的好处 IIFE最佳实践 jQuery优化 在Bootstrap源码(具体请看<Bootstrap源码解析>)和其他jQuery插件经常看到如下的 ...
- 使用range()生成自然数序列