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 cn.ehoo.bean.Person;
import cn.ehoo.service.PersonService;
/**
* @author whp
* @Email [email protected]
* @Jan 4, 2011
*
*/
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;
/**
* @author whp
* @Email [email protected]
* @Jan 4, 2011
*
*/
public class MyInterceptor {
private void anyMethod() {
}
public void doAccessCheck() {
System.out.println("执行前置通知");
}
public void doReturnCheck() {
System.out.println("执行后置通知");
}
public void doReleaseAction() {
System.out.println("执行最终通知");
}
public void doExceptionAction() {
System.out.println("执行例外通知");
}
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">
<bean name="personService" class="cn.ehoo.service.impl.PersonServiceBean" />
<bean name="myInterceptor" class="cn.ehoo.service.impl.MyInterceptor" />
<aop:config>
<aop:aspect id="myaop" ref="myInterceptor">
<aop:pointcut id="mycut" expression="execution(* cn.ehoo.service.impl.PersonServiceBean.*(..))" /><!--切入点表达式 -->
<aop:before pointcut-ref="mycut" method="doAccessCheck" /><!--前置通知 -->
<aop:after-returning pointcut-ref="mycut" method="doReturnCheck" /><!--后置通知 -->
<aop:after-throwing pointcut-ref="mycut" method="doExceptionAction" /><!--例外通知 -->
<aop:after pointcut-ref="mycut" method="doReleaseAction" /><!--执行最终通知 -->
<aop:around pointcut-ref="mycut" method="doBasicProfiling" /><!--执行环绕通知-->
</aop:aspect>
</aop:config>
</beans>
5、测试类
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 {
}
}