对Spring的AOP的浅薄的理解

今天重新看了 动态代理模式,又看了一遍Spring  AOP,把我的项目慢慢的添加Spring,把我了解的AOP和大家分享一下。


下面的例子参考了别人的代码

添加Spring3.0  需要的包


 

定义一个接口 HelloInterface

package rw.hello;

public interface HelloInterface {
    void BeforeHello();
    void ExecuteHello();
    void AfterHello(); 
}

实现这个接口的类 Hello

package rw.hello;

public class Hello implements HelloInterface{

	public void BeforeHello() {
		// TODO Auto-generated method stub
		System.out.println("----------------Hello 方法执行之前");
		
	}

	public void ExecuteHello() {
		// TODO Auto-generated method stub
		System.out.println("----------------Hello 方法执行中......");
	}

	public void AfterHello() {
		// TODO Auto-generated method stub
		System.out.println("----------------Hello 方法执行之后");
	}

}

现在想在 Hello 类的
BeforeHello(),ExecuteHello(),AfterHello()这三个方法执行之前做其它事情,比如权限问题,记录日志之类.....反正我今天是添加的用户权限问题

同样有3个类,风别是BeforeHello(),ExecuteHello(),AfterHello()这三个方法调用之前要调用的
下面只测试一下After.java 和 Before.java

After.java

package rw.method;

import org.aspectj.lang.JoinPoint;


public class After {
  public void invoke(JoinPoint joinpoint){
      System.out.println("After 类"+joinpoint.getSignature().getName());
  }
}

Before.java

package rw.method;

import org.aspectj.lang.JoinPoint;


public class Before {
  public void invoke(JoinPoint joinpoint){
      System.out.println("Before 类"+joinpoint.getSignature().getName());
  }
}


最注意的还是 applicationContext.xml的配置

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:p="http://www.springframework.org/schema/p"
	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.springsource.org/schema/aop/spring-aop-3.0.xsd
	                    http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

<bean id="hello" class="rw.hello.Hello"/>

<bean id="after" class="rw.method.After"/>
<bean id="before" class="rw.method.Before"/>
<bean id="execute" class="rw.method.Execute"/>

<aop:config>
<aop:pointcut expression="execution(* rw.hello.HelloInterface.BeforeHello(..))" id="beforepointcut"/>
<aop:aspect id="beforeaspect" ref="before">
  <aop:before method="invoke" pointcut-ref="beforepointcut"/>
</aop:aspect>
</aop:config>

<aop:config>
 <aop:pointcut expression="execution(* rw.hello.HelloInterface.AfterHello(..))" id="afterpointcut"/>
 <aop:aspect id="afteraspact" ref="after">
    <aop:after method="invoke" pointcut-ref="afterpointcut"/>
 </aop:aspect>
</aop:config>
</beans>
排版不是很好

1,在Hello的BeforeHello()方法执行之前 会调用Before类中的invoke(JoinPoint joinpoint)方法

2,在Hello的AfterHello()方法执行之后 会调用After类中的invoke(JoinPoint joinpoint)方法

测试类

package rw.test;


import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import rw.hello.HelloInterface;


public class AllTest {
    private ApplicationContext context;
	@Before
	public void setUp() throws Exception {
		context=new ClassPathXmlApplicationContext("applicationContext.xml");
	}
     @Test
	public void TestHelloInterface(){
	    HelloInterface hello=(HelloInterface) context.getBean("hello");
	    hello.BeforeHello();
	    hello.AfterHello();
	}
}

输出结果

Hello 类中的 void ExecuteHello()方法在applicationContext.xml中没有加以任何配置

正常输出



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值