在某些类中, 什么时机, 做什么事情
 切入点(point-cut): 在某些类中(Class<?>[] itfc = new Class<?>[] { IStudentService.class })
 通知: 什么时机, 做什么事情(InvocationHandler的invoke方法)
 切面: 切入点 + 通知
 织入(weaver): Proxy.newProxyInstance: 把切入点和通知施加到具体的业务逻辑上的过程

XML配置AOP步骤:
 1,准备了一个实现接口的bean, 还有一个事务的碎片化方法的类;
 2,把这两个类配置到Spring容器中;
 3,配置springAOP
  1)引入aop命名空间;
  2)配置AOP,格式如下
  <aop:config>
      <aop:point-cut expression=""  id="sample" />
      <aop:aspect ref="">
           <aop:before method="" pointcut-ref="sample">
           <aop:after-returning method="" pointcut-ref="sample">
           <aop:after-throwing method="" pointcut-ref="sample">
      </aop:aspect>
  </aop:config>
   1] aop:config: AOP配置
   2] aop:point-cut: AOP切入点
   3] aop:aspect: AOP切面配置
   4] aop:* 都是一个AOP切面

4,在接口文件中配置一个业务逻辑对象的接口,DI注入,并在test方法中直接调用该接口的具体业务逻辑

//接口:
package com.xk.spring.kp04_aop.aop.s01_xml; public interface IStudentService {
public void save(Student stu);
public void update(Student stu);
}
//Dao 实现类(service) 实现业务逻辑
package com.xk.spring.kp04_aop.aop.s01_xml; public class StudentServiceImpl implements IStudentService {
@Override
public void save(Student stu) {
System.out.println("调用Dao的save方法....");
}
@Override
public void update(Student stu) {
System.out.println("调用Dao的update方法...");
@SuppressWarnings("unused")
int i = 10/0;
}
}
//事物管理:将代码中的污染源抽出一个类,专门处理事物
package com.xk.spring.kp04_aop.aop.s01_xml; public class TransactionManager {
/**
* 事物开始
*/
public void begin(){
System.out.println("TransactionManager.begin()");
}
/**
* 事物提交
*/
public void commit(){
System.out.println("TransactionManager.commit()");
}
/**
* 事物回滚
*/
public void rollback(Throwable e){
System.out.println("TransactionManager.rollback()");
System.out.println("rollback()");
} /**
* 事物结束
*/
public void finished(){
System.out.println("TransactionManager.close()");
}
}
//Bean
//将bean注入就是指将数据注入Spring
package com.xk.spring.kp04_aop.aop.s01_xml; public class Student {
private String name;
private Integer age; public Student() { } public Student(String name, Integer age) {
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} @Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}

  

//测试类
package com.xk.spring.kp04_aop.aop.s01_xml; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class AOPXMLTest {
@Autowired
IStudentService seriver; @Test
public void testAOPXML() throws Exception {
seriver.save(new Student("张三", 18));
seriver.update(new Student("张4", 18));
}
}
<!-- xml配置aop -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="service" class="com.xk.spring.kp04_aop.aop.s01_xml.StudentServiceImpl" />
<bean id="manager" class="com.xk.spring.kp04_aop.aop.s01_xml.TransactionManager" />
<aop:config>
<!-- 需要导入两个依赖包 com.springsource.org.aopalliance-1.0.0.jar
和com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
-->
<aop:pointcut
expression="execution(* com.xk.spring.kp04_aop.aop.s01_xml.*Service.*(..))"
id="stuService" />
<aop:aspect ref="manager">
<aop:before method="begin" pointcut-ref="stuService" />
<!-- 回滚:在回滚的时候 throwing是要在TransactionManager类中配置的 参数"(throwable e)" -->
<aop:after-throwing method="rollback"
pointcut-ref="stuService" throwing="e" />
</aop:aspect>
</aop:config>
</beans> <!-- 错误:Pointcut is not well-formed: expecting 'name pattern' at character
position 配置aop报错:原因是配置切点表达式的时候报错了, 星号后面没有加空格: <aop:config> <aop:pointcut
id="transactionPointcut" expression="execution(* project.mybatis.service.*.*(..))"
/> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="omsMTransactionAdvice"
/> </aop:config> 其中,切入点表达式的使用规则: 1、execution(): 表达式主体。 2、第一个*号:表示返回类型,*号表示所有的类型。
3、包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包,com.sample.service.impl包、子孙包下所有类的方法。
4、第二个*号:表示类名,*号表示所有的类。 5、*(..):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数。 -->

xml的方式配置AOP:Aspect Oriented Programming的更多相关文章

  1. AOP Aspect Oriented Programming

    原理AOP(Aspect Oriented Programming),也就是面向方面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP将应用系统分为两部分,核心业务逻辑(Core bus ...

  2. Spring AOP(aspect oriented programming) 转载

    1.面向切面的基本原理 软件系统可以看成是由一组关注点组成的,其中,直接的业务关注点,是直切关注点.而为直切关注点提供服务的,就是横切关注点. 01.什么是面向切面编程 横切关注点:影响应用多处的功能 ...

  3. Java 面向切面编程(Aspect Oriented Programming,AOP)

    本文内容 实例 引入 原始方法 装饰者模式 JDK 动态代理和 cglib 代理 直接使用 AOP 框架--AspectWerkz 最近跳槽了,新公司使用了 AOP 相关的技术,于是查点资料,复习一下 ...

  4. Aspect Oriented Programming

    AOP(Aspect Oriented Programming),面向切面编程(也叫面向方面)是目前软件开发中的一个热点.利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度 ...

  5. Java实战之03Spring-03Spring的核心之AOP(Aspect Oriented Programming 面向切面编程)

    三.Spring的核心之AOP(Aspect Oriented Programming 面向切面编程) 1.AOP概念及原理 1.1.什么是AOP OOP:Object Oriented Progra ...

  6. AOP(Aspect Oriented Programming),即面向切面编程

    AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...

  7. Spring面向切面编程(AOP,Aspect Oriented Programming)

    AOP为Aspect Oriented Programming的缩写,意为:面向切面编程(也叫面向方面),可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...

  8. AOP为Aspect Oriented Programming的缩写,意为:面向切面编程

    在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的 ...

  9. javascript 高阶函数 实现 AOP 面向切面编程 Aspect Oriented Programming

    AOP的主要作用是吧一些跟核心业务逻辑模块无关的功能 -日志统计, 安全控制, 异常处理- 抽离出来, 再通过"动态织入"的方式掺入业务逻辑模块中. 这里通过扩展Function. ...

随机推荐

  1. RTTI(运行时类型识别),typeid,dynamic_cast

    dynamic_cast注意: 1.只能应用于指针和引用的转换: 2.要转换的类型中必须包含虚函数: 3.转换成功则返回地址,如果失败则返回NULL: 参见项目:RTTI

  2. 【java】Comparator的用法

    文章转载自: http://blog.csdn.net/u012250875/article/details/55126531 1.为什么写? comparator 是javase中的接口,位于jav ...

  3. QT---事件系统

    1         QT事件系统 1.1  事件的定义 QT中事件是有专门的类QEvent,常见的有键盘事件QKeyEvent.鼠标事件QMouseEvent和定时器事件QTimerEvent.例如用 ...

  4. jfinal集成cas单点认证实践

    本示例jfinal集成cas单点认证,采用获取到登录用户session信息后,在本地站点备份一份session信息,主要做以下几个步骤: 1.站点引入响应jar包: 2.在web.xml中配置对应过滤 ...

  5. lua --- 逻辑运算符小结

    lua中的逻辑运算符,认为只有false.nil为假,其他的都为真(包括0.空串) a and b    -- 如果a为false,则返回a,否则返回b a or b   -- 如果a为true,则返 ...

  6. Asp.net core 学习笔记 ( upload/download files 文件上传与下载 )

    更新 :  2018-01-22  之前漏掉了一个 image 优化, 就是 progressive jpg refer : http://techslides.com/demos/progressi ...

  7. vue 点击一个div,使input获得焦点

    <div class="inputMessage" @click="inputMessage">输入留言</div> <input ...

  8. yii框架中使用gii的用法

    首先在config文件中的 main-local.php中添加一句 'allowedIPs' => ['*'],如下图所示:

  9. vi常用快捷键

    vi常用快捷键 1)移动光标 h :光标左移一个字符k :光标上移一个字符j :光标下移一个字符l :光标右移一个字符 0 :光标移至行首$ :光标移至行尾 H :光标移至屏幕首行M :光标移至屏幕中 ...

  10. 配置samba 服务器 共享Linux目录

    配置samba 服务器 共享Linux目录 1.安装: yum install -y samba* 2.修改配置文件 vim /etc/samba/smb.conf [web] path = /usr ...