当前位置: 首页 > news >正文

Spring 一二事(10) - annotation AOP

先贴出POM的内容,这个毕竟是用的maven来简单构建的

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 2   <modelVersion>4.0.0</modelVersion>
 3   <groupId>org.springframework.samples</groupId>
 4   <artifactId>maven-spring002-aop</artifactId>
 5   <version>0.0.1-SNAPSHOT</version>
 6   
 7   <properties>
 8 
 9         <!-- Generic properties -->
10         <java.version>1.7</java.version>
11         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
13 
14         <!-- Spring -->
15         <spring-framework.version>3.2.3.RELEASE</spring-framework.version>
16 
17         <!-- Logging -->
18         <logback.version>1.0.13</logback.version>
19         <slf4j.version>1.7.7</slf4j.version>
20 
21         <!-- Test -->
22         <junit.version>4.11</junit.version>
23         
24         <!-- aspectj -->
25 <!--         <aspectj.version>1.8.7</aspectj.version> -->
26         <aspectj.version>1.6.6</aspectj.version>
27 
28     </properties>
29     
30     <dependencies>
31         <!-- Spring and Transactions -->
32         <dependency>
33             <groupId>org.springframework</groupId>
34             <artifactId>spring-context</artifactId>
35             <version>${spring-framework.version}</version>
36         </dependency>
37         <dependency>
38             <groupId>org.springframework</groupId>
39             <artifactId>spring-tx</artifactId>
40             <version>${spring-framework.version}</version>
41         </dependency>
42 
43         <!-- Logging with SLF4J & LogBack -->
44         <dependency>
45             <groupId>org.slf4j</groupId>
46             <artifactId>slf4j-api</artifactId>
47             <version>${slf4j.version}</version>
48             <scope>compile</scope>
49         </dependency>
50         <dependency>
51             <groupId>ch.qos.logback</groupId>
52             <artifactId>logback-classic</artifactId>
53             <version>${logback.version}</version>
54             <scope>runtime</scope>
55         </dependency>
56 
57         <!-- Test Artifacts -->
58         <dependency>
59             <groupId>org.springframework</groupId>
60             <artifactId>spring-test</artifactId>
61             <version>${spring-framework.version}</version>
62             <scope>test</scope>
63         </dependency>
64         <dependency>
65             <groupId>junit</groupId>
66             <artifactId>junit</artifactId>
67             <version>${junit.version}</version>
68             <scope>test</scope>
69         </dependency>
70 
71         <dependency>
72             <groupId>org.aspectj</groupId>
73             <artifactId>aspectjweaver</artifactId>
74             <version>${aspectj.version}</version>
75         </dependency>
76 
77     </dependencies>
78 
79     <build>
80         <plugins>
81             <plugin>
82                 <groupId>org.apache.maven.plugins</groupId>
83                 <artifactId>maven-compiler-plugin</artifactId>
84                 <configuration>
85                     <source>1.7</source>
86                     <target>1.7</target>
87                 </configuration>
88             </plugin>
89         </plugins>
90     </build> 
91 
92 </project>

关于AOP的配置,我个人还是喜欢用XML来配置,一来方便管理,看的清楚,一个项目的aop也不会太多,二来注解形式的不好管理

applicationContext.xml的话只要有2行就行

1    <context:component-scan base-package="com.lee.spring003.aop.annotation"></context:component-scan>
2     
3     <!-- 自动创建代理 -->
4     <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

ITeacherDAO.java

1 package com.lee.spring003.aop.annotation;
2 
3 public interface ITeacherDAO {
4     public String saveTeacher();
5 }

TeacherDAOImpl.java

 1 package com.lee.spring003.aop.annotation;
 2 
 3 import org.springframework.stereotype.Repository;
 4 
 5 @Repository("teacherDAO")
 6 public class TeacherDAOImpl implements ITeacherDAO {
 7 
 8     @Override
 9     public String saveTeacher() {
10         System.out.println("TeacherDAOImpl - saveTeacher()");
11         return "save successfully";
12     }
13 
14 }

Transaction.java

 1 package com.lee.spring003.aop.annotation;
 2 
 3 import org.aspectj.lang.annotation.AfterReturning;
 4 import org.aspectj.lang.annotation.Aspect;
 5 import org.aspectj.lang.annotation.Before;
 6 import org.aspectj.lang.annotation.Pointcut;
 7 import org.springframework.stereotype.Component;
 8 
 9 @Component("transaction")
10 @Aspect
11 public class Transaction {
12     
13     @Pointcut("execution(* com.lee.spring003.aop.annotation.TeacherDAOImpl.*(..))")
14     public void beginTransactionPointCut() {    // 方法签名
15     };
16     
17     @Before("beginTransactionPointCut()")
18     public void beginTransaction() {
19         System.out.println("Begin transaction...");
20     }
21     
22     @AfterReturning("beginTransactionPointCut()")
23     public void commit() {
24         System.out.println("Transaction commit...");
25     } 
26     
27 }

 

github地址:https://github.com/leechenxiang/maven-spring002-aop

 

相关文章:

  • ServiceStack.OrmLite MVC搭建
  • JDBCTemplate
  • 20160323实盘短线操作
  • Java 开发必会的 Linux 命令
  • 第三季度斩获重要投资的15家网络安全公司
  • 被迫尝试了各个版本的asm库读取类annotation
  • 思科推出新一代互联网边界防火墙 助力企业兼顾性能与安全
  • MacBook小技巧
  • 瞧!那个SSD领域的后来者正变为引领者
  • 【转】Data URL和图片,及Data URI的利弊
  • 高效运维最佳实践七字诀,不再憋屈的运维!
  • 报org.hibernate.MappingException: Unknown entity,可能
  • 从世界杯看社交大数据分析如何为行业客户创造价值
  • 前端开发者必备的20个在线工具和指南
  • 轻量函数式 JavaScript:三、管理函数输入
  • [译] 理解数组在 PHP 内部的实现(给PHP开发者的PHP源码-第四部分)
  • __proto__ 和 prototype的关系
  • “大数据应用场景”之隔壁老王(连载四)
  • Babel配置的不完全指南
  • Java 11 发布计划来了,已确定 3个 新特性!!
  • javascript 哈希表
  • Js基础知识(一) - 变量
  • Js实现点击查看全文(类似今日头条、知乎日报效果)
  • ReactNative开发常用的三方模块
  • Spring Cloud中负载均衡器概览
  • SpringCloud(第 039 篇)链接Mysql数据库,通过JpaRepository编写数据库访问
  • 安装python包到指定虚拟环境
  • 这几个编码小技巧将令你 PHP 代码更加简洁
  • [Shell 脚本] 备份网站文件至OSS服务(纯shell脚本无sdk) ...
  • linux 淘宝开源监控工具tsar
  • # Swust 12th acm 邀请赛# [ A ] A+B problem [题解]
  • ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTr
  • (10)ATF MMU转换表
  • (Redis使用系列) SpringBoot中Redis的RedisConfig 二
  • (差分)胡桃爱原石
  • (附源码)spring boot建达集团公司平台 毕业设计 141538
  • (附源码)springboot学生选课系统 毕业设计 612555
  • (附源码)ssm航空客运订票系统 毕业设计 141612
  • (论文阅读32/100)Flowing convnets for human pose estimation in videos
  • (数位dp) 算法竞赛入门到进阶 书本题集
  • (四) Graphivz 颜色选择
  • (转)h264中avc和flv数据的解析
  • (转)Oracle存储过程编写经验和优化措施
  • (转)winform之ListView
  • .NET 4 并行(多核)“.NET研究”编程系列之二 从Task开始
  • .NET Core实战项目之CMS 第一章 入门篇-开篇及总体规划
  • .NetCore项目nginx发布
  • .NET实现之(自动更新)
  • /dev/VolGroup00/LogVol00:unexpected inconsistency;run fsck manually
  • @RequestMapping处理请求异常
  • @SpringBootApplication 包含的三个注解及其含义
  • [ IO.File ] FileSystemWatcher
  • [1525]字符统计2 (哈希)SDUT
  • [AX]AX2012 R2 出差申请和支出报告
  • [BZOJ4337][BJOI2015]树的同构(树的最小表示法)