package com.xk.spring.kp04_aop.aop.s02_annotation;

public interface IStudentService {
public void save(Student stu);
public void update(Student stu);
}
package com.xk.spring.kp04_aop.aop.s02_annotation;

import org.springframework.stereotype.Service;

@Service
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.s02_annotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; //自定义切面
@Aspect
@Component //一般写在工具类上面
public class TransactionManager {
@Pointcut("execution(* com.xk.spring.kp04_aop.aop.s02_annotation.*Service.*(..))")
public void stuService(){}//此方法名为execution表达式的别名,即id="";
/**
* 事物开始
*/
@Before("stuService()")//
public void begin(){
System.out.println("TransactionManager.begin()");
}
/**
* 事物提交
*/
@AfterReturning("stuService()")
public void commit(){
System.out.println("TransactionManager.commit()");
}
/**
* 事物回滚
*/
@AfterThrowing(value = "stuService()",throwing = "e")
public void rollback(Throwable e){
System.out.println("TransactionManager.rollback()");
System.out.println("rollback()");
} /**
* 事物结束
*/
@After("stuService()")
public void finished(){
System.out.println("TransactionManager.close()");
}
@Around("stuService()")
public void all(ProceedingJoinPoint point){
try {
begin();
point.proceed();//此处有连接,必须写,不然执行到此将不再向下执行
commit();
} catch (Throwable e) {
rollback(e);
}finally{
finished();
} }
}
package com.xk.spring.kp04_aop.aop.s02_annotation;

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 + "]";
}
}
<?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">
<!-- 自动生成代理 -->
<aop:aspectj-autoproxy />
<!-- 指定IOC扫描的包 -->
<context:component-scan
base-package="com.xk.spring.kp04_aop.aop.s02_annotation"/>
</beans>
package com.xk.spring.kp04_aop.aop.s02_annotation;

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 AOPAnnoationTest {
@Autowired
IStudentService seriver; @Test
public void testAOPXML() throws Exception {
seriver.save(new Student("张三", 18));
seriver.update(new Student("张4", 18));
}
}

Annotation方式配置AOP的更多相关文章

  1. 框架源码系列七:Spring源码学习之BeanDefinition源码学习(BeanDefinition、Annotation 方式配置的BeanDefinition的解析)

    一.BeanDefinition 1. bean定义都定义了什么? 2.BeanDefinition的继承体系  父类: AttributeAccessor: 可以在xml的bean定义里面加上DTD ...

  2. 基于配置文件的方式配置AOP

    之前说的都是通过注释的方式配置,接下来说说如何使用配置文件配置AOP 还是原来的代码,去掉所有注释,接下来配置最基本的几个bean. 然后使用<aop:config>标签进行配置,然后配切 ...

  3. xml的方式配置AOP:Aspect Oriented Programming

    在某些类中, 什么时机, 做什么事情 切入点(point-cut): 在某些类中(Class<?>[] itfc = new Class<?>[] { IStudentServ ...

  4. Spring_基于配置文件的方式配置AOP

    applicationContext-xml.xml <?xml version="1.0" encoding="UTF-8"?> <bean ...

  5. AcpectJ注释方式配置AOP

    1.AspectJ的概念   @AspectJ类似于Java注解的普通Java类   Spring可以使用AspectJ来做切入点解析   AOP的运行时仍旧是纯的Spring AOP,对Aspect ...

  6. Annotation方式实现AOP

    1.添加其他jar包 2.配置applicationContext.xml文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <?xml version=&quo ...

  7. 22Spring基于配置文件的方式配置AOP

    直接看代码: package com.cn.spring.aop.impl; //加减乘除的接口类 public interface ArithmeticCalculator { int add(in ...

  8. spring-AOP框架(基于配置文件的方式配置AOP)

    .xml: ref-指向,order-指定优先级

  9. Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制

    Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制 201311.27 代码下载 链接: http://pan.baidu.com/s/1kYc6c 密码: 233t 前言 ...

随机推荐

  1. 几种RAID介绍(总结)

    概念 RAID是Redundent Array of Inexpensive Disks的缩写,简称为“磁盘阵列”.后来RAID中的字母I被改作了Independent,RAID就成了“独立冗余磁盘阵 ...

  2. python中装饰器

    在介绍装饰器之前,要先了解装饰器的相关基础知识. 嵌套函数: 最后引入一个基本的装饰器的例子: __author__ = "YanFeixu" import time def ti ...

  3. Effective java 系列之避免过度同步和不要使用原生态类型,优先考虑泛型

    避免过度同步(67):在一个被同步的方法或代码块中,不要调用哪些被设计成被覆盖的方法或者是由客户端以函数对象的形式提供的方法(21). 有点拗口,书上提供的创建者与观察者模式,add方法太多,看得眼花 ...

  4. 小程序模板中data传值有无...

    A:<template is="gemSelectColor" data="{{optionData}}" />B:<template is= ...

  5. 机器学习 之k-means和DBSCAN的区别

    目录 1.定义和区别(优缺点对比) 2.kmeans原理 3.DBSCAN原理 1.定义和区别(优缺点对比) 聚类分为:基于划分.层次.密度.图形和模型五大类: 均值聚类k-means是基于划分的聚类 ...

  6. 将本地 项目文件托管到 github

    1.新建一个本地 repository文件夹 2.将想要 托管的项目或文件 复制到repository 文件夹下 2. 右键 git bash here 输入命令 git init 生成本地仓库 4. ...

  7. loj#6491. zrq 学反演

    题意:求\(\sum_{i_1=1}^m\sum_{i_2=1}^m...\sum_{i_n=1}^mgcd(i_1,i_2,...i_n)\) 题解:\(\sum_{d=1}^md\sum_{i_1 ...

  8. python-django rest framework框架之分页

    1. 以前django做的分页组件当数据量特别大的时候,性能不是很高,有以下三种方式处理:        a. 记录当前访问页的最后一条数据id,往后取多少条        b. 最多显示120页   ...

  9. python-day2笔记

    # 1.为何要有操作系统:# 程序员掌握计算机系统所有的细节有很大难度,并且管理这些部件并加以优化使用,是一件极富挑战性的工作,# 于是,计算机安装了一层软件(系统软件),称为操作系统.它的任务就是为 ...

  10. leetcode-algorithms-34 Find First and Last Position of Element in Sorted Array

    leetcode-algorithms-34 Find First and Last Position of Element in Sorted Array Given an array of int ...