通知执行使用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为好。

文章结束给大家分享下程序员的一些笑话语录: 问路
有一个驾驶热气球的人发现他迷路了。他降低了飞行的高度,并认出了地面 上的一个人。他继续下降高度并对着那个人大叫,“打扰一下,你能告诉我我 在哪吗?”
下面那个人说:“是的。你在热气球里啊,盘旋在 30 英尺的空中”。
热气球上的人说:“你一定是在 IT 部门做技术工作”。
“没错”,地面上的人说到,“你是怎么知道的?”
“呵呵”,热气球上的人说,“你告诉我的每件事在技术上都是对的,但对都没 有用”。
地面上的人说,“你一定是管理层的人”。
“没错”,热气球上的人说,“可是你是怎么知道的?”
“呵呵”,地面上的那人说到,“你不知道你在哪里,你也不知道你要去哪,你 总希望我能帮你。你现在和我们刚见面时还在原来那个地方,但现在却是我 错了”。

原文地址:https://www.cnblogs.com/xinyuyuanm/p/3065722.html