SpringAOP里的几个术语,什么切面,切点之类的,官方的说明太抽象。为了更好地理解记忆,这里几下我自己的通俗的理解。

切面:就是日记类,什么前置通知后置通知(这些都是所谓的Advice)的具体方法都是写在这个日记类里的,所以把这些抽象出来的日记类就是一个切面(横切关注点)。

切点:就是某个具体的业务逻辑类,比如xxxService,也就是具体的DAO,Service之类的。

目录层级:

AOP相关的几个类就是com.aop.xmltype这个报下的4个类。

ICalculatorxml.java

package com.aop.xmltype;

/**
* 加减乘除接口,用于AOP测试
*
* @author Wei
*
*/
public interface ICalculatorxml {
/**
* 加法
*
* @param a
* @param b
* @return a+b
*/
public int doAdd(int a, int b); /**
* 减法
*
* @param a
* @param b
* @return a-b
*/
public int doSub(int a, int b); /**
* 乘法
*
* @param a
* @param b
* @return a*b
*/
public int doMul(int a, int b); /**
* 除法
*
* @param a
* @param b
* @return a/b
*/
public int doDiv(int a, int b); /**
* 求绝对值
*
* @param a
* @param b
* @return |a-b|
*/
public int doAbs(int a, int b);
}

  

CalculatorImplxml.java

package com.aop.xmltype;

public class CalculatorImplxml implements ICalculatorxml {

	/*
* { System.out.println("CalculatorImpl...{} ..."); }
*/ @Override
public int doAdd(int a, int b) {
int rtn = a + b;
System.out.println(a + "+" + b + "=" + rtn);
return rtn;
} @Override
public int doSub(int a, int b) {
// TODO Auto-generated method stub
return a - b;
} @Override
public int doMul(int a, int b) {
// TODO Auto-generated method stub
return a * b;
} @Override
public int doDiv(int a, int b) {
if (b == 0) {
return -1;
}
return a / b;
} @Override
public int doAbs(int a, int b) {
return Math.abs(a - b);
} }

  

MyAspectxml.java

package com.aop.xmltype;

public class MyAspectxml {

	public void logBefore() {
// String className = j.getClass().getName();
System.out.println("MyAspectxml logBefore(),开始计算...,");
} public void logAfter() {
System.out.println("已经计算结束...");
}
}

  

MyAOPTest.java

package com.aop.xmltype;

import org.junit.Test;

import com.util.Pub;

public class MyAOPTest {

	ICalculatorxml cal;

	@Test
public void testAop() {
cal = new CalculatorImplxml();
cal.doAdd(13, 99);
} public static void main(String[] args) { ICalculatorxml cal = (ICalculatorxml) Pub.getBeanCtx().getBean("calImplxml");
System.out.println("-----------------分割符号----------\n\n\n\n");
cal.doAdd(13, 99);
}
}

  

执行结果:

AOP相关部分的配置,完整的xml配置在最后。

bean.xml

 <!-- 切点的bean -->
<bean class="com.aop.xmltype.CalculatorImplxml" id="calImplxml"></bean>
<!-- 切面的bean -->
<bean class="com.aop.xmltype.MyAspectxml" id="myaspxml"></bean>
<!-- aop xmlType,用xml的形式配置AOP前置通知 -->
<aop:config>
<!--aop:pointcut 其实放在这儿也可以 -->
<!-- <aop:pointcut expression="execution (* com.aop.xmltype.CalculatorImplxml.*(..))"
id="pointcut1" /> --> <!-- 配置切面和通知 ,aop:aspect标签需要通过ref指定配置好的bean,id随便配置或者不配置,id的值可以随意起 -->
<aop:aspect id="myaspxml" ref="myaspxml" order="2">
<!-- 配置切点,即 要被记日记的对象, aop:pointcut 放在这儿也可以 ,切点不需要根对应的bean相关联,
只要expression指定的方法所在的类被Spring扫描得到就行,即只要所在的类配置了bean就可以 -->
<aop:pointcut expression="execution (* com.aop.xmltype.CalculatorImplxml.*(..))"
id="pointcut1" />
<!-- 切面里的具体的用于记录的方法就是一个通知,需要用通过pointcut-ref来指定具体的切点, -->
<aop:before method="logBefore" pointcut-ref="pointcut1" />
<aop:after method="logAfter" pointcut-ref="pointcut1" /> </aop:aspect> </aop:config>

完整的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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<bean id="moocAppctx" class="imooc_spring.test.aware.MoocApplicationContext"
init-method="hhhh">
</bean> <!-- 引入db.properties -->
<context:property-placeholder location="classpath:db.properties" /> <!-- 配置C3P0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driverName}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.pwd}"></property>
</bean> <!-- 配置 Spring 的 org.springframework.jdbc.core.JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> <bean id="moocBeanNameAware" class="imooc_spring.test.aware.MoocBeanNameAware"></bean> <!-- 测试 SpEL: 可以为属性进行动态的赋值(了解) -->
<bean id="girl" class="com.helloworld.User">
<property name="userName" value="周迅"></property>
</bean> <!-- <bean id="boy" class="com.helloworld.User" init-method="init" destroy-method="destroy">
<property name="userName" value="高胜远"></property> <property name="wifeName"
value="#{girl.userName}"></property> </bean> --> <bean id="girl2" class="com.helloworld.User2">
<property name="userName" value="Talor Swift"></property>
</bean> <!-- autowired测试,自动装配测试 -->
<bean id="people" class="test.spring.autowired.Person" scope="prototype"
autowire="byName">
<property name="name" value="小明"></property>
<!-- <property name="cat" ref="cat222"></property> -->
<!-- <property name="cat" ref="cat1"></property> -->
</bean> <bean id="cat" class="test.spring.autowired.Cat" scope="prototype">
<property name="name" value="波斯猫"></property>
</bean>
<!-- <bean id="cat222" class="test.spring.autowired.Cat"> <property name="name"
value="我是小喵喵"></property> </bean> --> <bean id="people2" class="test.spring.autowired.Person" scope="prototype"
autowire="byName">
<property name="name" value="小明"></property>
<property name="cat" ref="cat222"></property>
</bean> <bean id="cat222" class="test.spring.autowired.Cat" scope="prototype">
<property name="name" value="波斯猫"></property>
</bean> <!--context:component-scan 指定 扫描的包 -->
<!--可以通过 resource-pattern 指定扫描的资源, resource-pattern="myrepository/*.class"
的含义: 只扫描 base-package 对应包下的 目录为 myrepository 的所有java Bean -->
<!-- <context:component-scan base-package="imooc_spring.test.anotation"
resource-pattern="myrepository/*.class"></context:component-scan> --> <!-- context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"
子节点指定排除哪些注解 context:include-filter type="annotation" 需要结合context:component-scan
标签的 use-default-filters="false"来使用 context:exclude-filter type="assignable"
这个expression指的是自己写的类,意思排除哪些类 expression="imooc_spring.test.anotation.TestObj" -->
<context:component-scan base-package="imooc_spring.test.anotation">
<!-- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"
/> --> <!-- <context:exclude-filter type="assignable" expression="imooc_spring.test.anotation.TestObj"
/> --> </context:component-scan>
<context:component-scan base-package="com.aop"></context:component-scan> <!-- aop测试,需要引入aop命名空间 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> <!-- aop annotationType, --> <!-- 切点的bean -->
<bean class="com.aop.xmltype.CalculatorImplxml" id="calImplxml"></bean>
<!-- 切面的bean -->
<bean class="com.aop.xmltype.MyAspectxml" id="myaspxml"></bean>
<!-- aop xmlType,用xml的形式配置AOP前置通知 -->
<aop:config>
<!--aop:pointcut 其实放在这儿也可以 -->
<!-- <aop:pointcut expression="execution (* com.aop.xmltype.CalculatorImplxml.*(..))"
id="pointcut1" /> --> <!-- 配置切面和通知 ,aop:aspect标签需要通过ref指定配置好的bean,id随便配置或者不配置,id的值可以随意起 -->
<aop:aspect id="myaspxml" ref="myaspxml" order="2">
<!-- 配置切点,即 要被记日记的对象, aop:pointcut 放在这儿也可以 ,切点不需要根对应的bean相关联,
只要expression指定的方法所在的类被Spring扫描得到就行,即只要所在的类配置了bean就可以 -->
<aop:pointcut expression="execution (* com.aop.xmltype.CalculatorImplxml.*(..))"
id="pointcut1" />
<!-- 切面里的具体的用于记录的方法就是一个通知,需要用通过pointcut-ref来指定具体的切点, -->
<aop:before method="logBefore" pointcut-ref="pointcut1" />
<aop:after method="logAfter" pointcut-ref="pointcut1" /> </aop:aspect> </aop:config> </beans>

项目名为:imooc_spring.rar,在我的qq微云上。

点我获取项目。

Spring AOP基于xml配置实例的更多相关文章

  1. [刘阳Java]_Spring AOP基于XML配置介绍_第9讲

    基于注解配置的Spring AOP固然简单,但是这节我们会给大家介绍基于XML配置的AOP是如何应用的.为什么这么说了,因为后面我们还会介绍到Spring对Dao操作的事务管理(基于AOP的XML文件 ...

  2. Spring AOP-xml配置

    在spring AOP(一)中介绍了AOP的基本概念和几个术语,现在学习一下在XML中如何配置AOP. 在XML中AOP的配置元素有以下几种: AOP配置元素 描述 <aop:config> ...

  3. spring-第十八篇之spring AOP基于XML配置文件的管理方式

    1.在XML配置文件中配置切面.切入点.增强处理.spring-1.5之前只能使用XML Schema方式配置切面.切入点.增强处理. spring配置文件中,所有的切面.切入点.增强处理都必须定义在 ...

  4. Spring AOP之xml 配置实现

    首先这个配置模式估计现在已经不用了,因为我在我们公司的项目里面并没有看到这么配置AOP相关的东西.不过,这个就和学习spring的控制反转(IOC)和依赖注入(DI)一样,刚刚开始的时候,都是从简单的 ...

  5. Spring Aop(七)——基于XML配置的Spring Aop

    转发:https://www.iteye.com/blog/elim-2396043 7 基于XML配置的Spring AOP 基于XML配置的Spring AOP需要引入AOP配置的Schema,然 ...

  6. 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP

    上节我们提到了使用基于注解实现的AOP,这节我们将用基于xml配置的方式来实现的AOP. 1.首先建立一个类,作为切面类,这个类主要用来实现注解中各种通知要实现的方法. package com.yan ...

  7. Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较

    本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同. 相关引入包等Spring  AOP编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一 ...

  8. spring的基于xml的AOP配置案例和切入点表达式的一些写法

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  9. Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用

    Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...

随机推荐

  1. RTTI、虚函数和虚基类的实现方式、开销分析及使用指导(虚函数的开销很小,就2次操作而已)

    白杨 http://baiy.cn “在正确的场合使用恰当的特性” 对称职的C++程序员来说是一个基本标准.想要做到这点,首先要了解语言中每个特性的实现方式及其开销.本文主要讨论相对于传统 C 而言, ...

  2. Oracle的TPCC测试,原来也是个作弊的东西...

    http://www.oaktable.net/content/sorted-hash-clusters-rip 根据Jonathan Lewis老先生的测试实例,发觉cluster 的sort功能, ...

  3. 宣布正式发布 Windows Azure Notification Hub,新增 SQL Server AlwaysOn 可用性组侦听器支持

    今天,我们非常高兴地宣布,针对使用 Windows Azure 的移动和企业开发人员推出一些新功能.这些新功能可以减少构建移动应用程序的开发时间和成本,并能帮助企业开发人员实现高可用性和全球业务连续性 ...

  4. 用auto_ptr类模板帮助动态内存管理

    动态内存使用最多的是在C++应用程序的代码中.有过编程经验的程序员虽然都知道new操作符的使用一定要与delete匹配,在某些场合仍然可能有内存溢出.当异常被掷出时,程序的正常控制流程被改变,因此导致 ...

  5. Java实现HTML转PDF的总结

    Java实现HTML转PDF的几种方法—主要解决中文乱码问题 第一:同事在做HTML转PDF中遇到乱码问题 ********************************************** ...

  6. 从头開始写项目Makefile(七):统一目标输出文件夹

    [版权声明:转载请保留出处:blog.csdn.net/gentleliu. Mail:shallnew at 163 dot com]     上一节我们把规则单独提取出来,方便了Makefile的 ...

  7. 百度 LBS 开放平台,开发人员众測计划正式启动

    Hi各位亲爱滴开发人员:   你是否以前-- 期望第一时间率先接触到百度LBS开放平台的最新功能? 期望被邀请作为最最尊贵的首批试用志愿者感受志愿者的特权? 期望自己的意见被产品经理採纳.优化功能.从 ...

  8. MyGui 3.2.0(OpenGL平台)的编译

    MyGui是一个用来创建用户图形界面的库,用于游戏和3D应用程序.这个库的主要目标是达到:快速.灵活.易用. 1.下载准备: 源代码:http://svn.code.sf.net/p/my-gui/c ...

  9. HBA简介及原理

    HBA,即主机总线适配器英文“Host Bus Adapter”缩写.是一个使计算机在服务器和存储装置间提供输入/输出(I/O)处理和物理连接的电路板和/或集成电路适配器. 简介 主机总线适配器(Ho ...

  10. 关于js封装框架类库之选择器引擎(二)

    在上篇介绍了选择器的获取标签.id.类名的方法,现在我们在上篇基础上继续升级 1.问题描述:上篇get('选择器')已经实现,如果get方法里是一个选择器的父元素,父元素是DOM对象,那么如何获取元素 ...