先贴出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 @Aspect11 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