一.区别
注解方式实现AOP编程没有xml效率高,但是代码要简单
二.代码
1.目标类接口
package com.jp.aop;
import com.jp.domain.Person;
public interface PersonDao {
void save(Person person);
}
2.目标类
package com.jp.aop;
import org.springframework.stereotype.Component;
import com.jp.domain.Person;
@Component("personDao")
public class PersonDaoImpl implements PersonDao{
@Override
public void save(Person person) {
int i=1/0;
System.out.println("保存Person成功...");
}
}
3.切面类
package com.jp.transcation;
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;
import org.springframework.stereotype.Component;
@Component("m")
//代表这个类是一个切面类的注解
@Aspect()
public class MyTranscation {
//<aop:pointcut expression="execution (* com.jp.aop.PersonDaoImpl.*(..))" id="point"/>
//与上面一个方法一样,都是配置切入点的,public void point(){}相当于xml中配置的id
@Pointcut("execution (* com.jp.aop.PersonDaoImpl.*(..))")
public void point(){}
//代表前置通知的注解
@Before("execution (* com.jp.aop.PersonDaoImpl.*(..))")//@Before("point()")
public void begin(){
System.out.println("开启事务");
}
//代表后置通知的注解
@AfterReturning("execution (* com.jp.aop.PersonDaoImpl.*(..))")//@AfterReturning("point()")
public void commit(){
System.out.println("提交事务");
}
/*
@AfterReturning(value="execution (* com.jp.aop.PersonDaoImpl.*(..))",returning="ret")
public void commit(Object ret){
//获取目标函数返回值
System.out.println(ret);
System.out.println("提交事务");
}
*/
//代表异常通知的注解
@AfterThrowing("execution (* com.jp.aop.PersonDaoImpl.*(..))")//@AfterThrowing("point()")
public void ex(){
System.out.println("有异常了");
}
/*
@AfterThrowing(value="execution (* com.jp.aop.PersonDaoImpl.*(..))",throwing="xx")
public void ex(Throwable xx){
//获取异常
System.out.println(xx.getMessage());
System.out.println("有异常了");
}
*/
//代表最终通知的注解
@After("execution (* com.jp.aop.PersonDaoImpl.*(..))")//@After("point()")
public void finall(){
System.out.println("最终通知");
}
//代表环绕通知的注解
//@Around("execution (* com.jp.aop.PersonDaoImpl.*(..))")//@Around("point()")
public void around(ProceedingJoinPoint p) throws Throwable{
System.out.println("环绕通知前");
//放行,执行目标方法
p.proceed();
System.out.println("环绕通知后");
}
}
4.配置文件
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 扫描包 -->
<context:component-scan base-package="com.jp.aop,com.jp.transcation"></context:component-scan>
<!-- 将切面用到的注解被spring识别 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
5.测试类
package com.jp.aop;
import java.lang.reflect.Method;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jp.domain.Person;
public class SpringXmlTest {
@Test
public void test1(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext_annotation.xml");
PersonDao personDao = (PersonDao)ac.getBean("personDao");
Person person=new Person();
personDao.save(person);
}
@Test
public void test2(){
Object o=new Object();
Class class1=o.getClass();
Method[] methods = class1.getMethods();
for(Method m:methods){
System.out.println(m);
}
}
}