`
skydove
  • 浏览: 18577 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

AOP学习笔记

阅读更多

1、依赖配置方式

测试方法:

public void test1() {
  String filePath = System.getProperty("user.dir") + File.separator + "src/com/dmeo/aop"
   + File.separator + "spring.xml";
  BeanFactory factory = new XmlBeanFactory(new FileSystemResource(filePath));

  Shopping shopping = null;
  System.out.println("不使用任何通知");
  shopping = (Shopping) factory.getBean("shoppingImpl");
  shopping.buySomething("something");
  //    shopping.buyAnything("anything");

 

  System.out.println("使用前置通知");
  shopping = (Shopping) factory.getBean("welcomeAdviceShop");
  shopping.buySomething("type2");
  shopping.buyAnything("anything");
 }

 

spring.xml

<bean id="welcomeAdvice" class="com.dmeo.aop.WelcomeAdvice" />
 <bean id="welcomeAdviceShop"
  class="org.springframework.aop.framework.ProxyFactoryBean">
  <property name="target">
   <ref local="shoppingImpl" />
  </property>
  
  <!-- 以下属性没有的话不会出错,interceptorNames如果没有配置,target的方法就不会被拦截 -->
  <property name="proxyInterfaces">
   <value>com.dmeo.aop.Shopping</value>
  </property>
  <property name="interceptorNames">
   <list>
    <value>welcomeAdvice</value>
   </list>
  </property>
 </bean>

 

2.不干涉原先的bean配置,AOP真正的强悍啊,可以用来测试,添加日志等

1).单个通知

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:jee="http://www.springframework.org/schema/jee"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">

 <bean id="customer" class="com.dmeo.aop.Customer">
  <constructor-arg index="0">
   <value>xh</value>
  </constructor-arg>
  <constructor-arg index="1">
   <value>26</value>
  </constructor-arg>
 </bean>

 <bean id="shoppingImpl" class="com.dmeo.aop.ShoppingImpl">
  <property name="customer">
   <ref local="customer" />
  </property>
 </bean>

 <bean id="welcomeAdvice" class="com.dmeo.aop.WelcomeAdvice" />

 <aop:config>
  <aop:pointcut id="serviceOperation-before"
   expression="execution(* com.dmeo.aop.*.*(..))" />
  <aop:advisor pointcut-ref="serviceOperation-before"
   advice-ref="welcomeAdvice" />
 </aop:config>

注意前面的bean的引用以及AOP的配置。

 

上面的配置只能够对切面(方法)的一种行为进行拦截,比如WelcomeAdvice实现MethodBeforeAdvice,就只能在切点调用之前进行通知(前置通知)。(后置通知 AfterReturningAdvice、环绕通知 MethodInterceptor、异常通知 ThrowsAdvice)

 

下面给出对切面的所有行为进行拦截通知。

 

2).多个通知一起拦截

<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-2.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
 default-autowire="autodetect">

 <aop:config>
  <aop:aspect id="TestAspect" ref="aspectBean">
   <!--配置com.dmeo.aop2包下所有类或接口的所有方法-->
   <aop:pointcut id="businessService"
    expression="execution(* com.dmeo.aop2.*.*(..))" />
    
   <aop:before pointcut-ref="businessService"    method="doBefore" />
   <aop:after pointcut-ref="businessService" method="doAfter" />
   <aop:around pointcut-ref="businessService"    method="doAround" />
   <aop:after-throwing pointcut-ref="businessService"   method="doThrowing" throwing="ex" />
  </aop:aspect>
 </aop:config>

 <bean id="aspectBean" class="com.dmeo.aop2.TestAspect" />
 <bean id="aService" class="com.dmeo.aop2.AServiceImpl"></bean>
 <bean id="bService" class="com.dmeo.aop2.BServiceImpl"></bean>
</beans>

 

测试类:

public static void main(String[] args) {
  String filePath = "com/dmeo/aop2/spring.xml";

 // 必须得用ApplicationContext
  ApplicationContext factory = new ClassPathXmlApplicationContext(filePath);

  aService = (AService) factory.getBean("aService");
  bService = (BServiceImpl) factory.getBean("bService");

  testThrow();
 }

 public static void testCall() {
  System.out.println("Spring AOP test");
  aService.fooA("test fooA");
  aService.barA();
  bService.fooB();
  bService.barB("test barB", 0);
 }

 public static void testThrow() {
  try {
   bService.barB("call barB", 1);
  } catch (IllegalArgumentException e) {
  }
 }

 

详细代码见附件~~~

 

重点说明:

《Spring参考手册》中定义了以下几个AOP的重要概念,结合以上代码分析如下:

  • 切面(Aspect) :官方的抽象定义为“一个关注点的模块化,这个关注点可能会横切多个对象”,在本例中,“切面”就是类TestAspect所关注的具体行为,例如,AServiceImpl.barA()的调用就是切面TestAspect所关注的行为之一。“切面”在ApplicationContext中<aop:aspect>来配置。
  • 连接点(Joinpoint) :程序执行过程中的某一行为,例如,AServiceImpl.barA()的调用或者BServiceImpl.barB(String _msg, int _type)抛出异常等行为。
  • 通知(Advice) :“切面”对于某个“连接点”所产生的动作,例如,TestAspect中对com.spring.service包下所有类的方法进行日志记录的动作就是一个Advice。其中,一个“切面”可以包含多个“Advice”,例如TestAspect
  • 切入点(Pointcut) :匹配连接点的断言,在AOP中通知和一个切入点表达式关联。例如,TestAspect中的所有通知所关注的连接点,都由切入点表达式execution(* com.spring.service.*.*(..))来决定
  • 目标对象(Target Object) :被一个或者多个切面所通知的对象。例如,AServcieImpl和BServiceImpl,当然在实际运行时,Spring AOP采用代理实现,实际AOP操作的是TargetObject的代理对象。
  • AOP代理(AOP Proxy) 在Spring AOP中有两种代理方式,JDK动态代理和CGLIB代理。默认情况下,TargetObject实现了接口时,则采用JDK动态代理,例如,AServiceImpl;反之,采用CGLIB代理,例如,BServiceImpl。强制使用CGLIB代理需要将 <aop:config>proxy-target-class 属性设为true

 execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)

modifiers-pattern:方法的操作权限

ret-type-pattern:返回值

declaring-type-pattern:方法所在的包

name-pattern:方法名

parm-pattern:参数名

throws-pattern:异常

其中,除ret-type-pattern和name-pattern之外,其他都是可选的。上例中,execution(* com.spring.service.*.*(..))表示com.spring.service包下,返回值为任意类型;方法名任意;参数不作限制的所有方法。

分享到:
评论

相关推荐

    spring aop 学习笔记

    NULL 博文链接:https://microjava.iteye.com/blog/525796

    Spring Aop 学习笔记

    Spring Aop 学习笔记

    SpringAop学习笔记以及实现Demo

    SpringAOP学习笔记以及四个可运行的Demo,涵盖经典代理模式、基于注解、基于xml配置这3方面的Demo

    Spring AOP学习笔记

    NULL 博文链接:https://linres.iteye.com/blog/281221

    SpringAOP学习笔记

    ,文章属于基础级文章,适合入门级的小伙伴,它的概念,应用场景,实现原理及Spring的AOP的开发。全称:面向切面编程(AspectOrientedProgramming),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。...

    spring ioc aop mvc boot-学习笔记.docx

    自己学习spring课程的笔记。笔记都是根据尚硅谷的课程(spring ioc,spring aop,spring mvc,spring boot等)写的。 主要内容:spring ioc,spring aop,spring mvc,spring boot

    Spring技术内幕学习笔记.docx

    《Spring技术内幕》学习笔记1——IoC容器体系结构 《Spring技术内幕》学习笔记2——IoC定位Bean定义资源 《Spring技术内幕》学习笔记3——IoC容器载入Bean定义资源文件 ...《Spring技术内幕》学习笔记7——AOP基础

    javaSpring学习笔记

    在“Java Spring学习笔记”中,你将找到对Spring框架的全面介绍,包括IoC(控制反转)和DI(依赖注入)原理、AOP(面向切面编程)、Spring MVC、Spring Boot等核心内容。每个主题都结合了理论知识和实际示例,帮助你...

    spring学习笔记(有代码有注解解释)

    内容概要:学习Spring的一些学习笔记,主要学习Spring 框架两大核心机制(IoC、AOP) 笔记大纲:阅读笔记可以学习了解一下内容 如何使用 IoC ;配置文件;IoC 底层原理;通过运行时类获取 bean;通过有参构造创建 ...

    JBossESB学习笔记.rar_Jboss_ESB_esb和aop

    Jboss_ESB学习笔记以及总结和实例

    java SSH框架架构学习_笔记.struts,spring,hibernate. IOC,AOP,JDBC,编写个留言管理系统网站

    java SSH框架架构学习_笔记.zip java SSH框架架构学习_笔记.struts,spring,hibernate. IOC,AOP,JDBC,编写个留言管理系统网站

    JBoss ESB 学习笔记

    自己辛苦整理的网上的JBoss ESB学习笔记 ,非常详细,代码操作都有截图; 希望大家多支持! 学习笔记PDF的目录如下: 1——搭建ESB开发环境 2 2——第一个ESB代码Hello World 12 3——第二个ESB代码Hello World ...

    Spring学习笔记(16)----使用Spring配置文件实现AOP

    NULL 博文链接:https://coolszy.iteye.com/blog/541997

    spring的学习笔记

    spring很好的学习笔记,我想对于想学好或者面试好ioc和aop的同学有帮助的

    Java/JavaEE 学习笔记

    Java/JavaEE 学习笔记 作者在杰普学习时的学习笔记,是J2ee初学者必备手册,是大家学习J2EE开发的很好的参考笔记。 Java/JavaEE 学习笔记 内容目录: Unix 学习笔记..........7 一、Unix前言............7 二、...

    spring学习笔记

    spring从HelloWorld到ioc,aop,对JDBC,hibernate,struts1,struts2的支持笔记

    Spring入门学习笔记|Spring学习.pdf

    Spring入门学习笔记,内容包括Spring介绍,Spring配置文件,Spring配置数据源,Spring的注解开发,Spring集成Junit,Spring的AOP,jdbcTemplate介绍,Spring控制事务流程,Spring集成web。

    Spring框架学习笔记文档

    Spring框架的学习笔记,包括了springIOC,spirng注入,springAoP,SpringJdbc模板的使用,Spring声明式事务。

Global site tag (gtag.js) - Google Analytics