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. 为HTML表格添加交互功能------DataTables

    DataTables是一个功能强大的Javascript库,用于为HTML表格添加交互功能,虽然简单性是整个项目的核心设计原则,但入门看起来相当艰巨.但是,采取这些第一步并在您的网站上运行DataTa ...

  2. C#定义只能处理枚举类型的泛型类型

    internal sealed class GenericTypeThatRequireAnEnum<T> { ; //该 static 字段在不同的封闭类型之间是独立不共享的 //静态构 ...

  3. windows安装使用docker

    doker就是一个容器,如果想要在windows安装还必须要用另外一个工具docker-toolbox.下载地址:https://mirrors.aliyun.com/docker-toolbox/w ...

  4. Redis持久化AOF和RDB对比

    RDB持久化 AOF持久化 全量备份,一次保存整个数据库 增量备份,一次保存一个修改数据库的命令 保存的间隔较长 保存的间隔默认一秒 数据还原速度快 数据还原速度一般 save会阻塞,但bgsave或 ...

  5. [Spring] Annotation注释

    自动扫描: 在<beans>标签内, <context:annotation-config />允许使用注解 <context:component-scan base-p ...

  6. Redisson实现分布式锁(一)

    为什么要使用分布式锁? 单机情况下,多线程同时访问并改变临界资源(可变共享变量),将会使得这个变量不可预知,所以我们引入了同步(lock—synchronized).但在分布式场景下(多机部署),业务 ...

  7. 无法访问SVN历史记录的问题

      今天在eclipse中发现无法访问SVN的历史记录,提示条目不可读,截图如下: 用小乌龟客户端试了试也不行,截图如下: 最后解决办法是在SVN服务器上将代码仓库中conf目录下的svnserve. ...

  8. spring ----> 搭建spring+springmvc+mybatis出现的几个问题

    环境: idea ce 2018.1+maven3.5.3+mysql8.0.11+jdk1.8 spring4.3.7+spring mvc4.3.7+mybatis3.4.1+tomcat7.0. ...

  9. LeetCode--404--左叶子之和

    问题描述: 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 方法:recursive class ...

  10. 2018-02-16 GetSameTypeQuestion

    '目前存在的BUG '图片补丁存在多个URL Private Declare Function URLDownloadToFile Lib "urlmon" Alias " ...