接口类PersonService类与6相同。
1)实现类 PersonServiceBean:
@Service
// 使用自动扫描的方式自动装配
public class PersonServiceBean implements PersonService {
@Override
public void save(String name) {
// throw new RuntimeException("异常");
System.out.println("调用save()方法");
}
@Override
public void update(String name, Integer id) {
System.out.println("调用update()方法");
}
@Override
public String getPersonName(Integer id) {
System.out.println("调用getPersonName()方法");
return "xoxo";
}
}
2)拦截类MyInterceptor:
/**
* 当使用XML文件配置实现AOP时,拦截类是没有注解的普通类
* @author A_shun
*/
@Component
// 使用自动扫描的方式自动装配
public class MyInterceptor {
public void doAccessCheck() {
System.out.println("前置通知");
}
public void doAfterReturning() {
System.out.println("后置通知");
}
public void doAfter() {
System.out.println("最后通知");
}
public void doAfterThrowing() {
System.out.println("例外通知");
}
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("进入方法");
Object result = pjp.proceed();
System.out.println("退出方法");
return result;
}
}
3)beans.xml文件的配置:
<?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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 添加的自动扫描组件 --> <context:component-scan base-package="springDaoBean" /> <!-- 添加拦截器 --> <aop:aspectj-autoproxy /> <!-- 注解配置 --> <!-- <bean id="personService" class="springDaoBean.PersonServiceBean" /> --> <!-- 将MyInterceptor拦截类添加到Spring容器中管理 --> <bean id="aspetbean" class="springDaoBean.MyInterceptor" /> <!-- 切面配置 --> <aop:config> <aop:aspect id="asp" ref="aspetbean"> <!-- 设置切点,并设置拦截条件 --> <span style="white-space:pre"> </span> <aop:pointcut id="mycut" expression="execution(* springDaoBean.PersonServiceBean.*(..))" /> <!-- 有参数的拦截:(参数可以设置可以不设置)此处第一个参数为String类型,<span style="white-space:pre"> </span> 第二个为Integer类型,后面的参数可以不设置 <aop:pointcut id="mycut"expression="execution<span style="white-space:pre"> </span>(* springDaoBean.PersonServiceBean.*(java.lang.String,java.lang.Integer,..))"/>--> <!-- 关于方法返回类型的拦截 :持方法只拦截返回值类型不为void的类;<span style="white-space:pre"> </span> 若 !void改为 void,则只拦截返回类型为void的方法 <aop:pointcut id="mycut" expression=" execution(!void springDaoBean.PersonServiceBean.*(..))" /> --> <!-- 设置拦截后具体执行的方法:pointcut-ref为关联到的切点;method为拦截类中具体执行的方法 --> <aop:before pointcut-ref="mycut method="doAccessCheck" /> <aop:after-returning pointcut-ref="mycut" method="doAfterReturning" /> <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing" /> <aop:after pointcut-ref="mycut" method="doAfter" /> <aop:around pointcut-ref="mycut" method="doBasicProfiling" /> </aop:aspect> </aop:config> </beans>
4)主测试类springAopTest:
public class springAopTest {
@Test
public void instanceSpring() {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("springXml/beans.xml");
PersonService personServiceBean = (PersonService)
ctx.getBean("personServiceBean");
personServiceBean.save("oooo");
System.out.println("==========================");
personServiceBean.getPersonName(0);
System.out.println("==========================");
personServiceBean.update("xxxx", 2);
}
}Spring_7_使用XML文件配置的方式实现AOP,布布扣,bubuko.com
原文地址:http://blog.csdn.net/u012963457/article/details/38517079