Spring注解方式实现AOP demo

1、新建一个java的工程,导入spring需要的jar包与开发切面需要的jar包。

dist\spring.jar
lib\jakarta-commons\commons-logging.jar
如果使用了切面编程(AOP),还需要下列jar文件
lib/aspectj/aspectjweaver.jar和aspectjrt.jar
lib/cglib/cglib-nodep-2.1_3.jar
如果使用了JSR-250中的注解,如@Resource/@PostConstruct/@PreDestroy,还需要下列jar文件
lib\j2ee\common-annotations.jar

2、Person.java

package cn.ehoo.bean;

/**
 * @author whp
 * @Email [email protected]
 * @Jan 4, 2011
 * 
 */
public class Person {
	private Long id;
	private String name;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}



2、PersonService.java 与PersonServiceBean.java

PersonService.java
package cn.ehoo.service;

import cn.ehoo.bean.Person;

/**
 *@author whp
 *@Email [email protected]
 *@Jan 4, 2011
 *
 */
public interface PersonService {

	public void save(Person person);

}


PersonServiceBean.java
package cn.ehoo.service.impl;

import org.springframework.stereotype.Service;

import cn.ehoo.bean.Person;
import cn.ehoo.service.PersonService;

/**
 * @author whp
 * @Email [email protected]
 * @Jan 4, 2011
 * 
 */
@Service("personService")
public class PersonServiceBean implements PersonService {
	private String user;

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public PersonServiceBean() {
	}

	public PersonServiceBean(String user) {
		super();
		this.user = user;
	}

	public void save(Person person) {
		System.out.println("执行PerServiceBean的save方法");
		//throw new RuntimeException("======");
	}
}


3、MyInterceptor.java

package cn.ehoo.service.impl;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Controller;

import cn.ehoo.bean.Person;

/**
 * @author whp
 * @Email [email protected]
 * @Jan 4, 2011
 * 
 */
@Aspect//声明切面
@Controller//把类交给spring管理
public class MyInterceptor {
	@Pointcut("execution(* cn.ehoo.service.impl.PersonServiceBean.*(..))")// 切入点表达式
	private void anyMethod() {
	}// 声明一个切入点
	@Before("anyMethod()&& args(userName)")//定义前置通知 执行业务方法前执行 args(userName) 表示要执行的方式必须为一个参数并为Person类型.这样就给他再加了限制条件
	public void doAccessCheck(Person userName) {
		System.out.println(userName);//得到输入的参数
		System.out.println("执行前置通知"); 
	}
	@AfterReturning(pointcut="anyMethod()")//定义后置通知 执行完业务方法后执行 如果例外通知执行,那么它不会执行
	public void doReturnCheck() {
		System.out.println("执行后置通知"); 
	}
	
	@After("anyMethod()")//定义最终通知 finally里执行的通知。
	public void doReleaseAction() {
		System.out.println("执行最终通知"); 
	}
	
	@AfterThrowing(pointcut="anyMethod()", throwing="ex")//定义例外通知
    public void doExceptionAction(Exception ex) {
		System.out.println("执行例外通知"); 
	}
	 
	@Around("anyMethod()")//环绕通知 doBasicProfiling    pjp可以修改  用于权限
	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("执行环绕通知"); 
		Object retule =pjp.proceed();
		System.out.println("退出环绕通知"); 
		return retule;
	}




}


4 在src下对beans.xml进行配置
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	<context:component-scan base-package="cn.ehoo" />
	<aop:aspectj-autoproxy />
</beans>


4、测试类
package junit.test;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.ehoo.bean.Person;
import cn.ehoo.service.PersonService;

/**
 * @author whp
 * @Email [email protected]
 * @Jan 4, 2011
 * 
 */
public class AOPTest {
	static PersonService personService;
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		AbstractApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
		personService = (PersonService) cxt.getBean("personService");
	}

	/**
	 * @author whp
	 * @Email [email protected]
	 * @Jan 4, 2011
	 * 
	 */
	
	public static void main(String[] args) {
		try {
			setUpBeforeClass();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Test
	public void saveTest() {
		personService.save(new Person());
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}
}

你可能感兴趣的:(java,spring,AOP,bean,JUnit)