Annotation方式配置AOP
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的更多相关文章
- 框架源码系列七:Spring源码学习之BeanDefinition源码学习(BeanDefinition、Annotation 方式配置的BeanDefinition的解析)
		
一.BeanDefinition 1. bean定义都定义了什么? 2.BeanDefinition的继承体系 父类: AttributeAccessor: 可以在xml的bean定义里面加上DTD ...
 - 基于配置文件的方式配置AOP
		
之前说的都是通过注释的方式配置,接下来说说如何使用配置文件配置AOP 还是原来的代码,去掉所有注释,接下来配置最基本的几个bean. 然后使用<aop:config>标签进行配置,然后配切 ...
 - xml的方式配置AOP:Aspect Oriented Programming
		
在某些类中, 什么时机, 做什么事情 切入点(point-cut): 在某些类中(Class<?>[] itfc = new Class<?>[] { IStudentServ ...
 - Spring_基于配置文件的方式配置AOP
		
applicationContext-xml.xml <?xml version="1.0" encoding="UTF-8"?> <bean ...
 - AcpectJ注释方式配置AOP
		
1.AspectJ的概念 @AspectJ类似于Java注解的普通Java类 Spring可以使用AspectJ来做切入点解析 AOP的运行时仍旧是纯的Spring AOP,对Aspect ...
 - 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 ...
 - 22Spring基于配置文件的方式配置AOP
		
直接看代码: package com.cn.spring.aop.impl; //加减乘除的接口类 public interface ArithmeticCalculator { int add(in ...
 - spring-AOP框架(基于配置文件的方式配置AOP)
		
.xml: ref-指向,order-指定优先级
 - Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制
		
Spring入门6事务管理2 基于Annotation方式的声明式事务管理机制 201311.27 代码下载 链接: http://pan.baidu.com/s/1kYc6c 密码: 233t 前言 ...
 
随机推荐
- 虹软2.0 免费人脸识别C#类库分享
			
目前只封装了人脸检测部分的类库,供大家交流学习,肯定有问题,希望大家在阅读使用的时候及时反馈,谢谢!使用虹软技术开发完成 戳这里下载SDKgithub:https://github.com/dayAn ...
 - CSS sprite使用
			
CSS Sprites叫css精灵或者雪碧图,是一种网页图片应用处理方式. CSS Sprites其实就是把网页中一些背景图片整合到一张图片文件中,再利用CSS的“background-image”, ...
 - springBoot集成Redis遇到的坑(择库)源码分析为什么择库失败
			
提示: springboot提供了一套链接redis的api,也就是个jar包,用到的连接类叫做LettuceConnectionConfiguration,所以我们引入pom时是这样的 <de ...
 - Python Selenium Cookie 绕过验证码实现登录
			
Python Selenium Cookie 绕过验证码实现登录 之前介绍过博客园的通过cookie 绕过验证码实现登录的方法.这里并不多余,会增加分析和另外一种方法实现登录. 1.思路介绍 1.1. ...
 - 如何模拟一个http请求并把response的内容保存下载下来,导出到excel中(结尾福利)
			
def doExport(self): # 模拟一个http请求 url = u'%s?dumptype=investigation&dumpid=%s&timezone=8' % ( ...
 - 雷林鹏分享:jQuery EasyUI 树形菜单 - 创建基础树形网格
			
jQuery EasyUI 树形菜单 - 创建基础树形网格 树形网格(TreeGrid)组件从数据网格(DataGrid)继承,但是允许在行之间存在父/子节点关系.许多属性继承至数据网格(DataGr ...
 - breakthroughs in statistics | 统计学历史
			
<breakthroughs in statistics>- 这本书理解透了,统计方面应该可以封神了. 亚马逊上有卖,貌似还有好几卷. Breakthroughs in Statistic ...
 - 数组方法map(映射),reduce(规约),foreach(遍历),filter(过滤)
			
数组方法map(映射),reduce(规约),foreach(遍历),filter(过滤) map()方法返回一个由原数组中每一个元素调用一个指定方法后返回的新数组 reduce()方法接受一个函数作 ...
 - 创建gitlab ssh 密钥
			
SSH代表用于管理网络,操作系统和配置的Secure Shell或Secure Socket Shell,并且每次都不需要使用用户名和密码即可验证GitLab服务器. 您可以设置SSH密钥以提供计算机 ...
 - Artem and Array CodeForces - 442C (贪心)
			
大意: 给定序列$a$, 每次任选$a_i$删除, 得分$min(a_{i-1},a_{i+1})$(无前驱后继时不得分), 求最大得分. 若一个数$x$的两边都比$x$大直接将$x$删除, 最后剩余 ...