先贴出POM的内容,这个毕竟是用的maven来简单构建的
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.samples</groupId>
<artifactId>maven-spring002-aop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<!-- Generic properties -->
<java.version>1.7</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Spring -->
<spring-framework.version>3.2.3.RELEASE</spring-framework.version>
<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.7</slf4j.version>
<!-- Test -->
<junit.version>4.11</junit.version>
<!-- aspectj -->
<!-- <aspectj.version>1.8.7</aspectj.version> -->
<aspectj.version>1.6.6</aspectj.version>
</properties>
<dependencies>
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Test Artifacts -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</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
package com.lee.spring003.aop.annotation;
import org.springframework.stereotype.Repository;
@Repository("teacherDAO")
public class TeacherDAOImpl implements ITeacherDAO {
@Override
public String saveTeacher() {
System.out.println("TeacherDAOImpl - saveTeacher()");
return "save successfully";
}
}
Transaction.java
package com.lee.spring003.aop.annotation;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component("transaction")
@Aspect
public class Transaction {
@Pointcut("execution(* com.lee.spring003.aop.annotation.TeacherDAOImpl.*(..))")
public void beginTransactionPointCut() { // 方法签名
};
@Before("beginTransactionPointCut()")
public void beginTransaction() {
System.out.println("Begin transaction...");
}
@AfterReturning("beginTransactionPointCut()")
public void commit() {
System.out.println("Transaction commit...");
}
}