AOP中的注解自动装配通知

 

AOP中的注解自动装配通知

 

 

 

 一、创建一个注解类:

 

注意:aop的引入

package cn.csdn.util;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class ServiceImpl implements Service {

	/**
	 * @Before("execution(* work())")方法中的第一个参数“*”是返回值,第二个参数“work()”是方法的名
	 * */
	@Before("execution(* goWork())")
	public void eat(JoinPoint jp) {
		// TODO Auto-generated method stub
		System.out.println("I'm eating!!!");
		/*
		 * 输出的结果是: I'm eating!!! 我正在工作!
		 */
	}

	/**
	 * After(execution(*
	 * cn.csdn.service.Emp*.*(..)))方法的第一个参数(*)是返回值,第二个参数是位于cn.csdn.service
	 * 包下的前缀是Emp的类的所有方法,((..))代表的是方法的参数可以是可变的参数
	 * */
	@After("execution(* cn.csdn.service.Emp*.*(..))")
	public void goCompany() {
		// TODO Auto-generated method stub
		System.out.println("I want to go home");
	}
/**
 * 总结返回值有void和*两种,方法的名字的表示方式有直接写方法的名字、写出那个类的哪个方法或类和方法的名字模糊匹配*/
	@AfterThrowing(pointcut = "execution(* *..EmpService*.*(..))", throwing = "ex")
	public void leave(Exception ex) {
		// TODO Auto-generated method stub
		System.out.println("I want leav");
	}

	@Before("execution(* cn.csdn.service.EmpServiceImpl.*(..))")
	public void goHome() {
		// TODO Auto-generated method stub
		System.out.println("I want go Home");

	}

	@Around("execution(* *..Emp*.goWork(..))")
	public void signIn() {
		// TODO Auto-generated method stub
		System.out.println("I around before");
		System.out.println("I around after");

	}

}

 

xml中的部分代码如下:

<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: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/aop 
	http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
	 >

	<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl"/>
	<bean id="serviceImpl" class="cn.csdn.util.ServiceImpl"></bean>	
	<aop:aspectj-autoproxy/>

</beans>
  源代码如下:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值