Spring 주해 방식 AOP demo 구현

1.자바 프로젝트 를 새로 만 들 고 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 {
	}
}

좋은 웹페이지 즐겨찾기