您的位置:首页 > 编程语言 > Java开发

注解方式实现Spring的aop

2015-11-23 20:04 357 查看
一、UserService接口

<span style="font-size:18px;">package com.seven.spring.o_aop_annotation;

import java.util.List;

public interface UserService {
//为了后置通知返回一个结果
List<Object> queryUsers();

void saveUser();

void deleteUser();
}</span>
二、UserServiceImpl实现类

<span style="font-size:18px;">package com.seven.spring.o_aop_annotation;

import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService{

public List<Object> queryUsers() {
System.out.println("查询一个User");
return new ArrayList<Object>();

}
public void saveUser() {
System.out.println("保存一个User");

}
public void deleteUser() {
int i= 1/0;
System.out.println("删除一个User");

}

}</span>
三、通知对象LogAdvice

<span style="font-size:18px;">package com.seven.spring.o_aop_annotation;

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;

/**
* 通知对象
* @author Administrator
*
*/
@Aspect//定义一个切面
public class LogAdvice{
@Pointcut("execution(* *(..))")
private void MyPointCut(){}

//前置通知
@Before("MyPointCut()")
public void before(){
System.out.println("==before==");
}

//最终通知
@After("MyPointCut()")
public void after(){
System.out.println("==after==");
}

//后置通知
//returning="returnValue"接受原方法执行后的返回值
@AfterReturning(pointcut="MyPointCut()",returning="returnValue")
public void afterReturning(Object returnValue){
System.out.println(returnValue);
System.out.println("==afterReturning==");
}

//异常通知
@AfterThrowing(pointcut="MyPointCut()",throwing="ex")
public void afterThrows(Exception ex){
System.out.println(ex);
System.out.println("==afterThrows==");
}

//环绕通知
//一定要在环绕通知中,执行完本方法后,要有返回结果
@Around("MyPointCut()")
public Object around(ProceedingJoinPoint joinPoint){

try {
System.out.println("环绕通知(前)");
Object object=joinPoint.proceed();//执行原方法
System.out.println("环绕通知(后)");
return object;
} catch (Throwable e) {
throw new RuntimeException(e);
}
}

}
</span>
四、applicationContext.xml

<span style="font-size:18px;"><?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> 
<!-- 配置UserService对象 -->
<bean id="userService" class="com.seven.spring.o_aop_annotation.UserServiceImpl">
</bean>

<!-- 配置一个通知对象 -->
<bean id="logAdvice" class="com.seven.spring.o_aop_annotation.LogAdvice"></bean>

<!-- 使用注解的方式配置切面,必须要有这个配置-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>
</span>
五、测试类MainTest

<span style="font-size:18px;">package com.seven.spring.o_aop_annotation;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.seven.oa.service.crm.RoleService;
/**
* 使用编程方式实现aop
* @author Administrator
*/
public class MainTest {

@Test
public void testUserService(){

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml", getClass());
//这里获取的是代理对象
UserService userService = (UserService) ac.getBean("userService");

System.out.println(userService.getClass());
//=========================
//使用的是代理对象
userService.saveUser();
System.out.println("--------------");

userService.queryUsers();
System.out.println("--------------");

userService.deleteUser();
System.out.println("--------------");
}

}
</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: