您的位置:首页 > 编程语言 > Java开发

Spring AOP实例

2015-07-22 10:43 459 查看
先写一个接口

<span style="font-size:18px;">package com.shuhui.impl;

public interface StudentService {
public void addStudent(String name);
}
</span>


在继承实现它

<span style="font-size:18px;">package com.shuhui.impl;

public class StudentServiceImpl implements StudentService{
@Override
public void addStudent(String name) {
System.out.println("添加学生"+name);
}
}
</span>
切入层

<span style="font-size:18px;">package com.shuihui.Aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class StudentServiceAspect {

public void doBefore(JoinPoint jp){
System.out.println("doBefore开始添加学生");
System.out.println("类名"+jp.getTarget().getClass().getName());
System.out.println("方法名"+jp.getSignature().getName());
}

public void doAfter(JoinPoint jp){
System.out.println("doAfter添加学生结束");
System.out.println("类名"+jp.getTarget().getClass().getName());
System.out.println("方法名"+jp.getSignature().getName());
}

public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("doAround开始添加学生");
Object obj = pjp.proceed();
//若目标方法有返回值,则obj就是目标方法的返回值
System.out.println("doAround添加学生结束");
return obj;
}

public void doAfterThrowing(JoinPoint jp,Throwable ex){
System.out.println("异常信息"+ex.getMessage());

}

}
</span>
bean
<span style="font-size:18px;"><?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"
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"> <aop:aspectj-autoproxy/>
<bean id="StudentServiceAspect" class="com.shuihui.Aspect.StudentServiceAspect"></bean>
<bean id="studentService" class="com.shuhui.impl.StudentServiceImpl"></bean>
<aop:config>
<aop:aspect id="StudentServiceAspect" ref="StudentServiceAspect">
<aop:pointcut expression="execution(* com.shuhui.impl.*.*(..))" id="businessService" />
<aop:before method="doBefore" pointcut-ref="businessService"/>
<aop:after method="doAfter" pointcut-ref="businessService"/>
<aop:around method="doAround" pointcut-ref="businessService"/>
<aop:after-throwing method="doAfterThrowing" pointcut-ref="businessService" throwing="ex"/>
</aop:aspect>

</aop:config>

</beans></span>
测试
<span style="font-size:18px;">package com.shuhui.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.shuhui.impl.StudentService;

public class Test {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
StudentService s = (StudentService) ac.getBean("studentService");
s.addStudent("张三");
}
}
</span>


注意在bean中,studentservice依赖注入的class一定要写接口的地址。否则会报各种莫名其妙的错。

还有在bean中,抛出异常的通知一定要加上throwing="ex"。否则会报各种莫名其妙的错。
<span style="font-size:18px;">after-throwing</span>
<span style="font-size:18px;">after-throwing</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: