注解方式实现Spring的aop

一、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>








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值