XML配置文件头部理解

26 篇文章 0 订阅
18 篇文章 0 订阅

简介

学习web开发两年时间了,虽然以前经常使用如Hibernate、Spring等框架的xml配置文件,但是对于他的头部几乎是一个小白,今天发现理解这些还是非常有用的,所以接下来做简单的介绍。

源码示例

<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-3.0.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

<aop:aspectj-autoproxy />

<bean id="customerBo" class="com.yiibai.customer.bo.impl.CustomerBoImpl" />

<!-- Aspect -->
<bean id="logAspect" class="com.yiibai.aspect.LoggingAspect" />

<aop:config>

  <aop:aspect id="aspectLoggging" ref="logAspect">

    <!-- @Before -->
    <aop:pointcut id="pointCutBefore"
      expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomer(..))" />

    <aop:before method="logBefore" pointcut-ref="pointCutBefore" />

    <!-- @After -->
    <aop:pointcut id="pointCutAfter"
       expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomer(..))" />

    <aop:after method="logAfter" pointcut-ref="pointCutAfter" />

    <!-- @AfterReturning -->
    <aop:pointcut id="pointCutAfterReturning"
       expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomerReturnValue(..))" />

    <aop:after-returning method="logAfterReturning"
      returning="result" pointcut-ref="pointCutAfterReturning" />

    <!-- @AfterThrowing -->
    <aop:pointcut id="pointCutAfterThrowing"
      expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomerThrowException(..))" />

    <aop:after-throwing method="logAfterThrowing"
      throwing="error" pointcut-ref="pointCutAfterThrowing" />

    <!-- @Around -->
    <aop:pointcut id="pointCutAround"
      expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomerAround(..))" />

    <aop:around method="logAround" pointcut-ref="pointCutAround" />

  </aop:aspect>

</aop:config>

</beans>
解释

<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-3.0.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

xmlns:xsi相当于定义了一个前缀xsi,这个前缀有一个标识符是“http://www.w3.org/2001/XMLSchema-instance”,这个可以理解为单纯的字符串,而后边的xsi:schemaLocation则是先指定刚才定义的标识符“http://www.w3.org/2001/XMLSchema-instance”,然后给出这个标识符表示的前缀的定义文档所在的地方“http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ”,此时的这个URL不能理解为单纯的字符串,而是一个真实的网络地址,表示要从这里去取xsi的定义文件。

这个的理解可以类似于web.xml当中的Servlet的配置。先单独提取出来头部,第一行和第二行,一个是xmlns一个却是xmlns:xsi,这两个虽然看着不一样,但是本质上是一样的,第一个只是使用了当前文档的默认头部,比如后边的文档内容当中使用的<bean>,这个是没有任何前缀的,但是他的原理跟有前缀的是一样的,只是他是用了默认前缀。

如果在文档内容中要使用aop所指定的元素,此时就必须带上aop前缀用来表示这个元素来自于AOP这个schema定义文件(什么是schema文件去网上查)。



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值