zoukankan      html  css  js  c++  java
  • Spring学习笔记之二----基于XML的Spring AOP配置

    1. 在Spring配置文件中,通常使用<aop:config>元素来设置AOP,其中应包括:
      • <aop:aspect>指定aspect,aspect是一个POJO类,包含了很多的advice;advice是aspect中的方法,它包含了要应用在切面上的逻辑;
      • <aop:pointcut>指定pointcut,pointcut是一个表达式,用于指定在哪些对象或者类型上应用aspect的advice;
      • <aop:before>, <aop:after>,<aop:after-returning>, <aop:after-throwing>, or <aop:around>是<aop:aspect>的子元素,用于指定将aspect中的哪一个advice方法(通过method属性)应用到哪一个pointcut上(通过pointcut-ref属性);
    2. 如何通过一个静态字段创建一个bean:
      • 使用一个内置的FactoryBean:FieldRetrievingFactoryBean,并指定其staticField属性,属性值就是该静态字段的全路径;
      • 使用<util:constant>标记,其static-field用于指定静态字段的全路径;
    3. 如果通过一个对象属性来创建一个bean:
      • 使用一个内置的FactoryBean:PropertyPathFactoryBean,其targetObject属性用于指定属性所在的对象bean,propertyPath用于属性的全路径;
      • 使用<util:property-path>标记,其path属性指定beanName....propertyName;
    4. 有时候bean需要知道一些其在spring container中的原数据信息,这需要让bean的类型实现一些特定的接口:
      接口名 说明
      BeanNameAware  Bean name
      BeanFactoryAware 当前bean所在的Bean Factory,通过它你能调用container的一些service
      ApplicationContextAware 当前bean所在的Application Context,通过它你能调用container的一些service
      MessageSourceAware  得到当前Appliaction Context中的Message Source,通过它你能得到Message
      ApplicationEventPublisherAware  得到当前Appliaction Context中的Application Event Publisher,通过它你能publish Application Events 
      ResourceLoaderAware   得到当前Appliaction Context中的Resource Loader,通过它你能load External Resources

      这些接口中的Setter方法将在bean属性被设置之后,在初始化方法被调用之前被Spring Container自动调用:
      • 通过构造函数或者工厂方法创建bean实例;
      • 设置bean的属性值;
      • 调用这些接口的Setter方法;
      • 将bean实例传递给BeanPostProcessor实现类的postProcessBeforeInitialization方法;
      • 调用bean的初始化方法;
      • 将bean实例传递给BeanPostProcessor实现类的postProcessAfterInitialization方法;
      • 使用bean;
      • 当container被关闭时;调用析构器方法;
    5. 在Spring中,各个bean之间可以通过Event来进行交流,通常使用以下3个步骤来完成:
      • 申明Event的类型,所有的Event都必须继承自ApplicationEvent类;
      • 发布Event的bean必须要实现ApplicationEventPublisher接口,如果要publish event时,请调用publishEvent()方法;
      • 侦听Event的bean都必须要实现ApplicationListener接口,并实现onAppicationEvent方法;如果Listener只想侦听特殊的Event,可以实现ApplicationListener接口的范型形式;
      application context它自己也在发布container events,例如ContextClosedEvent,ContextRefreshedEvent,RequestHandledEvent。
    6. Property Editor主要用于实现字符串与属性类型值之间的相互转换,Spring提供了很多内置的PropertyEditors,在使用这些之前,应首先注册它们;
      Spring提供了CustomNumberEditor, ClassEditor, FileEditor, LocaleEditor, StringArrayPropertyEditor, and URLEditor;其中ClassEditor, FileEditor, LocaleEditor, and URLEditor已经被预注册了;而其他的则需要注册到一个类型为CustomEditorConfigurer的bean里面;
      你也可以创建自定义的PropertyEditor,通过实现java.beans.PropertyEditor接口或者继承java.beans.PropertyEditorSupport类;在PropertyEditorSupport类中,getAsText()用于将property转换为一个字符串值,setAsText()用于将字符串值转换为一个属性值,获取或设置属性值可以通过调用getValue()和setValue()方法;
      一个PropertyEditor只能针对一种类型,当它被放置到与这个类型相同的包中,并且类名是这个类型名+Editor,则Spring Container会自动搜索它,并不需要注册到CustomEditorConfigurer中。
    7. bean的定义可以被继承,被继承的bean称之为父bean,继承的bean称之为子bean;子bean继承父bean的bean定义,但不是全部;比如autowire属性就不能被继承;
      子Bean可以复写父bean的定义,如果在父bean中定义了class属性,并且子bean的类型和父bean的类型一样,则子bean不需要定义class属性;
      父bean可以是抽象的,只需设置abstract="true",Spring Container则不会初始化这个bean;
      父bean可以不用定义类型;
      如果子bean和父bean都有一个集合类型的属性,子bean可以自定义集合类型属性中的item,并添加继承的父bean集合类型属性里面所有item,只需在元素上面设置merge="true"
    8. 我们可以使用SpEL去配置Spring
  • 相关阅读:
    模式识别之svm()---支持向量机svm 简介1995
    机器视觉之相关资源--- 计算机视觉相关的部分测试数据集和源码站点
    包的引入,不要引入自己目录下的。
    内省对象 用的少,被BeanUtils代替
    使用增强for循环遍历集合的时候操作集合的问题?
    Integer 内部实现
    eclipse常用快捷键
    java 运行时环境和编译器环境
    js 随机变换图片
    js 事件点击 显示 隐藏
  • 原文地址:https://www.cnblogs.com/cdutedu/p/4769146.html
Copyright © 2011-2022 走看看