Spring---->采用Annotation的方式实现AOP

一、采用Annotation的方式实现AOP

1、spring依赖库
          SPRING_HOME/dist/spring.jar
          SPRING_HOME/lib/jakarta-commons/commons-logging.jar
          SPRING_HOME/lib/log4j/log4j-1.2.14.jar
          SPRING_HOME/lib/aspectj/*.jar
2、采用Aspect定义切面
@Aspect public class SecurityHandler

3、在Aspect定义Pointcut和Advice
@Pointcut("execution(* com.ncepu.spring.*.add*(..))")
@Before("allAddMethod()")

4、启用AspectJ对Annotation的支持并且将Aspect类和目标对象配置到Ioc容器中
<aop:aspectj-autoproxy/>
<bean id="usermanager" class="com.ncepu.spring.UserManagerImpl"></bean>
<bean id="securityHandler" class="com.ncepu.spring.SecurityHandler"></bean></beans>
注意:在这种方法定义中,切入点的方法是不被执行的,它存在的目的仅仅是为了重用切入点即Advice中通过方法名引用这个切人点


二、例子代码

UserManager.java

package com.ncepu.spring;


public interface UserManager {
	public void add(String name,String password);
	public void delete(int id);
	public void modify(String name,String password);
	public void find (int id);
}

UserManagerImpl.java

package com.ncepu.spring;

public class UserManagerImpl implements UserManager {

	@Override
	public void add(String name, String password) {
	System.out.print("添加用户");

	}

	@Override
	public void delete(int id) {
		System.out.print("删除用户");

	}

	@Override
	public void find(int id) {
		System.out.print("查找用户");
	}

	@Override
	public void modify(String name, String password) {
		System.out.print("修改用户");

	}

}

SecurityHandler.java

package com.ncepu.spring;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class SecurityHandler {

	/**
	 * 定义Pointcut,Pointcut的名称就是allAddMethod,此方法不能有返回值和参数,该方法只是一个标识
	 * 
	 * Pointcut的内容是一个表达式,描述那些对象的那些方法(订阅Joinpoint)
	 */
	@Pointcut("execution(* com.ncepu.spring.*.add*(..))")
	private void allAddMethod() {};

	/**
	 * 定义Advice,标识在那个切入点何处织入此方法
	 */

	@Before("allAddMethod()")
	private void checkSecurity() {
		System.out.println("安全性检查");
	}

}

Client.java

package com.ncepu.client;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ncepu.spring.UserManager;

public class Client {
	
	public static void main(String args[])
	{
		BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
		UserManager userManager=(UserManager)factory.getBean("usermanager");
		userManager.add("wsz","ncepu");
	}

}

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:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<aop:aspectj-autoproxy/>
	<bean id="usermanager" class="com.ncepu.spring.UserManagerImpl"></bean>
	<bean id="securityHandler" class="com.ncepu.spring.SecurityHandler"></bean>
</beans>



 

 

posted on 2012-09-08 15:34  小-强-斋-太  阅读(201)  评论(0编辑  收藏  举报

导航