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. 使用http://start.spring.io/ 生成工程

    今天学习spring-cloud,无意中发现一个spring提供的构建工程的页面,想记录下,发现有个博客写的很好就直接抄过来了.  原文链接: https://blog.csdn.net/u01050 ...

  2. (转+整理)C#中使用GUID

    GUID(全局统一标识符)是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的.通常平台会提供生成GUID的API.生成算法很有意思,用到了以太网卡地址.纳秒级时间.芯片ID码和许多可 ...

  3. (24)协程---joinall和value

    # spawn(函数,参数...) 启动一个协成 # join()  阻塞,直到某个协程执行完毕 # joinall 类似于join 只不过 g1.join() g2.join() gevent.jo ...

  4. RNA Spike-in Control(转)

    Spike-in Control:添加/加入(某种物质)的对照(组)在某些情况下,待检验样本中不含待测物质或者含有但是浓度很低,为了证明自己建立的方法能对样本中待测物质进行有效的检测,可在待检样本中加 ...

  5. 【源码分析】Mybatis使用中,同一个事物里,select查询不出之前insert的数据

    一.问题场景模拟问题:第二次查询和第一次查询结果一模一样,没有查询出我新插入的数据 猜测:第二次查询走了Mybatis缓存 疑问:那为什么会走缓存呢? 1.service方法 @Override @T ...

  6. drozer安装出现的问题

    首先先检查你的手机或者模拟器是否开启了drozer agent 安装出现的问题: (1)找不到java路径: 解决方法:新建一个TXT文件,在文件中写如下内容: [executables] java ...

  7. rsync未授权访问漏洞利用

    漏洞描述:rsync是Linux系统下的数据镜像备份工具,使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他ssh,rsync主机同步.也就是说如果你可以连接目标IP的r ...

  8. stl集合算法

    accumulate() 累加  accumulate: 对指定范围内的元素求和,然后结果再加上一个由val指定的初始值.  #include<numeric> vector<i ...

  9. Windows与Linux端口占用查询及处理

    Windows下端口占用查询 输入命令:netstat -ano,列出所有端口的情况.在列表中我们观察被占用的端口,比如是49157,首先找到它. 查看被占用端口对应的PID,输入命令:netstat ...

  10. 一、I/O操作(缓存流,数据流,对象流)

    一.缓存流 以介质是硬盘为例子说明,字节流和字符流的缺点: 每次读写的时候,都会访问硬盘,如果读写频率比较高的时候,性能不佳.为了解决问题,采用缓存流. 缓存流在读取的时候,会一次性读较多的数据到缓存 ...