spring学习笔记8--使用spring进行面向切面的(AOP)编程(2)XML配置方式
来源:互联网 发布:java socket 客户端 编辑:程序博客网 时间:2024/06/12 01:07
前言:最近在复习spring特记录于此,欢迎大家交流指正 QQ:767872620
主要是对xml文件中的<aop:config>标签配置,这样原来的代理类(拦截类)所有的注解就个全部干掉了!
例子如下:
继续“万恶”的person相关类
public interface PersonService {public void save(String name);public void update(String name,Integer id);public String getPersonName();public String getPersonName(Integer id);}
person实现类:
package cn.itcast.service.imp;import cn.itcast.service.PersonService;public class PersonServiceBean implements PersonService {public String getPersonName() {System.out.println("我是getPersonName()");return "xxx";}public String getPersonName(Integer id) {System.out.println("我是getPersonName()");return "xxx";}public void save(String name) {throw new RuntimeException("我爱意外"); //用于测试意外通知//System.out.println("我是save()");}public void update(String name, Integer id) {System.out.println("我是update()");}}
拦截类:
这是一个纯粹的类,一个不矫揉造作的类,一个没有认识注解修饰的类!
/** * 通过xml配置aop切面 * @author Mars *是一个纯粹的类,没有一点注解的修饰 */public class MyInterceptorXml {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{//if(){//判断用户是否有权限System.out.println("进入环绕通知方法");Object result = pjp.proceed();System.out.println("退出环绕通知方法");//}return result;}}
最为关键的就是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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- xml配置方式实现aop -->
<!--1.将各类配置入bean,将类交给spring管理 -->
<bean id="PersonService" class="cn.itcast.service.imp.PersonServiceBean"></bean>
<bean id="myInterceptorXML" class="cn.itcast.service.MyInterceptorXml"></bean>
<!--2.设置切入点 -->
<aop:config>
<!--(1).配置切面 -->
<aop:aspect id="asp" ref="myInterceptorXML">
<!--(2).配置切入点 -->
<aop:pointcut id="mycut" expression="execution(* cn.itcast.service.imp.PersonServiceBean.*(..))" ></aop:pointcut>
<!--(3).配置前置通知 -->
<aop:before pointcut-ref="mycut" method="doAccessCheck"></aop:before>
<!--(4).配置后置通知 -->
<aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
<!--(5).配置例外通知 -->
<aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>
<!--(6).配置最终通知 -->
<aop:after pointcut-ref="mycut" method="doAfter"/>
<!--(7).配置环绕通知 -->
<aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
</aop:aspect>
</aop:config>
</beans>
还有简单粗暴的测试类:
package junit.test;import org.junit.BeforeClass;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.itcast.service.PersonService;public class springXmlAopTest {@BeforeClasspublic static void setUpBeforeClass() throws Exception{}@Test public void interceptorTest(){ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");PersonService personservice = (PersonService)cxt.getBean("PersonService");personservice.save("xxx");//personservice.getPersonName(2);}}
- spring学习笔记8--使用spring进行面向切面的(AOP)编程(2)XML配置方式
- spring学习笔记7--使用spring进行面向切面的(AOP)编程(1)注解方式实现
- Spring AOP学习笔记(4):AOP切面优先级&切面表达式重用&xml配置方式
- 使用Spring进行面向切面编程(AOP)
- 使用Spring进行面向切面编程(AOP) .
- 使用Spring进行面向切面编程(AOP)---讲解+代码
- 使用Spring进行面向切面编程(AOP)
- 使用Spring进行面向切面(AOP)编程
- 使用Spring进行面向切面编程(AOP)
- 使用Spring进行面向切面编程(AOP)
- 使用Spring进行面向切面编程(AOP)
- 使用Spring进行面向切面编程(AOP)
- 使用Spring进行面向切面编程(AOP
- 使用Spring进行面向切面(AOP)编程
- 使用Spring进行面向切面(AOP)编程
- 使用Spring进行面向切面编程(AOP
- Spring学习笔记(三)--面向切面编程AoP
- Spring学习笔记 —— AOP(面向切面编程) 之使用ProxyFactoryBean实现AOP
- Apache与Tomcat 区别联系
- 《把时间当朋友》 读书笔记 困境 醒悟
- 从玩扑克到软件开发
- android开发步步为营之2:开发自定义进度条对话框
- GCC生成的汇编代码
- spring学习笔记8--使用spring进行面向切面的(AOP)编程(2)XML配置方式
- 数据库的设计
- openstack Centos6.3 Note
- 读懂IDS10的sqexplain.out
- android开发步步为营之1:Handler让主线程和子线程进行通信
- 用boost在共享内存上创建一个复杂的map
- 输出菱形
- tar 命令使用
- DEBUG_NEW和THIS_FILE