使用Aspectj进行AOP开发

添加类库:aspectjrt.jar和aspectjweaver.jar
添加aop schema.
定义xml元素:<aop:aspectj-autoproxy>
编写java类,并用@Aspect注解成通知
     AspectJ 支持 5 种类型的通知注解:
  @Before: 前置通知, 在方法执行之前执行
  @After: 后置通知, 在方法执行之后执行
  @AfterReturning: 返回通知, 在方法返回结果之后执行
  @AfterThrowing: 异常通知, 在方法抛出异常之后
  @Around: 环绕通知, 围绕着方法执行
配置成普通bean元素即可.

 

二、

后置通知:@After
    @After("execution(* *..WelcomeService.*(..))")
    public void applaud(){..}
后置通知在目标方法执行完成之后执行.一个切面aspect包含很多通知.
@After
    后置通知表明目标方法执行完之后,不论是否抛异常,都会织入该通知.
@AfterReturning
    方法返回后通知只在目标方法返回以后执行,若抛异常不执行.

@AfterReturning(pointcut="",returning="res")
public void xxx(Joinput jp,Object res)
在AfterReturning
通知中可接收到返回值.res即是用来接收返回值的对象.

三、

环绕通知:@Around
    @Around("execution(* *..WelcomeService.*(..))")
    public void around(ProceedingPointCut jp){..}
注意:可以控制目标方法是否调用,以及返回完全不同的对象,要慎用.

指定优先级:
@Aspect
@Order(0)
public class xxx{...}
加上@Order注解可以指定加入切面的优先级(先后顺序,值越小,优先级越高)

例子说明:


AdviceImpl类:

package www.csdn.spring.advice.aspetjs;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class AdviceImpl {

	@Before(value = "execution(* UserDaoImpl.*(..))")
	public void doTransAction(){
		System.out.println("----开启事务-----");
	}
	
	@After(value = "execution(* www.csdn..UserDaoImpl.*(..))")
	public void doAfterTransAction(){
		System.out.println("-------提交事务-------");
	}
	
	@Around(value = "execution(* www.csdn..UserDaoImpl.save(..))")
	public void doSec(ProceedingJoinPoint jp){
		System.out.println("----------安全处理之前-------------");
		try {
			Object obj = jp.proceed();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("----------安全处理之后-------------");
	}
}


配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd
           ">

 <!-- 通知 -->
 <bean id="adviceImpl" class="www.csdn.spring.advice.aspetjs.AdviceImpl"/>
 
 <!-- 配置bean -->
 <bean id="userDaoImpl" class="www.csdn.spring.advice.aspetjs.UserDaoImpl"/>
 
 <aop:aspectj-autoproxy/>
</beans>


测试类:

package www.csdn.spring.advice.aspetjs;

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

public class AspetjsTest {

	@Test
	public void test(){
		ApplicationContext context=new ClassPathXmlApplicationContext("spring-asp*.xml");
		UserDao uDao=(UserDao) context.getBean("userDaoImpl");
		uDao.save(null);
		/*uDao.delete(null);
		try{
			uDao.update(null);
		}catch(Exception e){
			
		}
		uDao.getObjects();*/
	}
}


例子说明:在配置文件中出现的UserDaoImpl可以自己在相应的包中自己建,最好是新建UserServiceImpl为好。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值