环境
UserDAO
package com.spring.dao;
import org.springframework.stereotype.Component;
@Component("userDAO")
public class UserDao {
public void say() {
System.out.println("say method is called");
}
public void smile() {
System.out.println("smile method is called");
}
public void cry() {
System.out.println("cry method is called");
}
public void jump() {
System.out.println("jump method is called");
}
}
不做过多解释,不清楚的可参考上篇Spring AOP之HelloWorld xml配置
UserService
package com.spring.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.spring.dao.UserDao;
@Component("userService")
public class UserService {
@Resource(name="userDAO")
private UserDao dao;
public UserDao getDao() {
return dao;
}
public void setDao(UserDao dao) {
this.dao = dao;
}
public void say() {
dao.say();
}
public void smile() {
dao.smile();
}
public void cry() {
dao.cry();
}
public void jump() {
dao.jump();
}
}
Aop拦截类
package com.spring.aop;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Component("logIntercepter")
@Aspect
public class LogIntercepter {
/*public void before(){
System.out.println("----------before-------------");
}
public void after(){
System.out.println("----------after-------------");
}
public void exception(){
System.out.println("----------exception-------------");
}
public void around(){
System.out.println("----------exception-------------");
}*/
@Before("execution(* com.spring.service..*.*(..))")
public void before(){
System.out.println("----------before-------------");
}
@After("execution(* com.spring.service..*.*(..))")
public void after(){
System.out.println("----------after-------------");
}
@AfterThrowing("execution(* com.spring.service..*.*(..))")
public void exception(){
System.out.println("----------exception-------------");
}
/*@Around("execution(* com.spring.service..*.*(..))")
public void around(){
System.out.println("----------exception-------------");
}*/
}
注意观察
此类使用了@Aspect注解,你需要在spring配置文件中使用<aop:aspectj-autoproxy/>标签开启注解功能
接下来依次定义了一系列方法before、after、exception、around依次标注注解为@Before("execution(* com.spring.service..*.*(..))") 、@After("execution(* com.spring.service..*.*(..))")、@AfterThrowing("execution(* com.spring.service..*.*(..))")、@Around("execution(* com.spring.service..*.*(..))") ,分别为spring aop 的前置通知、后置通知、异常通知、环绕通知,当进入com.spring.service包或子包下的所有方法时他们都会起作用,其中异常通知,只有在该方法出现异常时才会调用
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:p="http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.spring.*"/>
<aop:aspectj-autoproxy/>
<!--
<aop:config>
<aop:aspect id="aspect" ref="logIntercepter">
<aop:pointcut expression="execution(* com.spring.service..*(..))" id="pointCut"/>
<aop:before method="before" pointcut-ref="pointCut"/>
<aop:after method="after" pointcut-ref="pointCut"/>
<aop:after-throwing method="exception" pointcut-ref="pointCut"/>
<aop:around method="around" pointcut-ref="pointCut"/>
</aop:aspect>
</aop:config>
-->
</beans>
内容很简单
就这三行
<context:annotation-config/>
<context:component-scan base-package="com.spring.*"/>
<aop:aspectj-autoproxy/>
其中注视部分为xml配置部分的代码
单元测试
package com.spring.test;
import javax.annotation.Resource;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import com.spring.service.UserService;
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class SpringTest extends AbstractJUnit4SpringContextTests {
@Resource(name="userService")
private UserService userService;
@Test
public void test1(){
userService.say();
System.out.println();
userService.smile();
System.out.println();
userService.cry();
}
}
运行结果
----------before-------------
say method is called
----------after-------------
----------before-------------
smile method is called
----------after-------------
----------before-------------
cry method is called
----------after-------------
点我下载工程代码