SpringAOP示例(纯注解)

9 篇文章 0 订阅
4 篇文章 0 订阅

1.编写UserService的接口(目标类)

package com.zslaa.service;

public interface UserService {

	public void save(String name);
	
	public void update();
}

2.编写UserServiceImpl的实现

//实现类要记得添加@Service,让该类作为Spring的IOC容器对象。

package com.zslaa.service.impl;

import org.springframework.stereotype.Service;

import com.zslaa.service.UserService;
/**
 * 这个类在AOP属于目标对象(Target)
 */
@Service
public class UserServiceImpl implements UserService {

	@Override
	public void save(String name) {
		System.out.println("执行save方法,name为:"+name);
	}

	@Override
	public void update() {
		System.out.println("执行update方法");
	}

}

3.编写纯注解的切面类

//@Aspect: 让该类成为切面类
//@Component: 让该类成为Spring的IOC容器类
//@Before等注解:  注解方式的通知
package com.zslaa.aspect;
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.springframework.stereotype.Component;

/**
 * Spring的AOP的切面类
 */
@Aspect
@Component
public class MyAspect {

	// 前置通知
	@Before(value = "execution(public * com.zslaa.service.impl.UserServiceImpl.*(..))")
	public void before() {
		System.out.println("前置通知");
	}

	// 最终通知
	@After(value = "execution(public * com.zslaa.service.impl.UserServiceImpl.*(..))")
	public void after() {
		System.out.println("最终通知");
	}

	// 后置通知
	@AfterReturning(value = "execution(public * com.zslaa.service.impl.UserServiceImpl.*(..))")
	public void afterReturning() {
		System.out.println("后置通知");
	}

	// 异常通知
	@AfterThrowing(value = "execution(public * com.zslaa.service.impl.UserServiceImpl.*(..))")
	public void afterThrowing() {
		System.out.println("异常通知");
	}

	// 环绕通知
	@Around(value = "execution(public * com.zslaa.service.impl.UserServiceImpl.*(..))")
	public void around(ProceedingJoinPoint pjp) {
		System.out.println("前置通知--前面代码");
		//执行目标对象方法
		try {
			pjp.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		System.out.println("前置通知--后面代码");
	}

}

4.编写Spring配置类

//该类的目标是代替之前的applicationContext.xml
package com.zslaa.test;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * Spring配置类
 * @author http://www.yiidian.com
 *
 */
@Configurable
@ComponentScan(basePackages="com.yiidian")
@EnableAspectJAutoProxy  // 开启AOP注解功能
public class SpringConfig {

}

5编写测试类

package com.zslaa.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.zslaa.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=SpringConfig.class)
public class Demo1 {

	@Resource
	private UserService userService;
	
	@Test
	public void test1(){
		customerService.update();
	}
}

6.运行结果

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值