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)
{ }
}
}
======================================================================

  

上一篇:***文件上传控件bootstrap-fileinput的使用和参数配置说明


下一篇:Codeforces 191 C Fools and Roads (树链拆分)