AOP的全称是 Aspect Oriented Programming,是面向切面编程的技术,把一个个的横切关注点放到某个模块中去,称之为切面。那么每一个的切面都能影响业务的某一种功能,切面的目的就是功能增强,如日志切面就是一个横切关注点,应用中许多方法需要做日志记录的只需要插入日志的切面即可。(动态代理就可以实现 AOP),这种面向切面编程的思想就是 AOP 思想了。
AspectJ 是一个面向切面的框架,它扩展了Java 语言(即使用 Java 对 AOP 进行了实现)。
..
: 可用于全限定名中和方法参数中,分别表示子包和 0 到 N 个参数。// 注意第一个星符号后面有空格
execution(* cn.wolfcode.ssm.service.impl.*ServiceImpl.*(..))
元素及其子元素org.springframework spring-context 5.0.8.RELEASE org.springframework spring-test 5.0.8.RELEASE test junit junit 4.12 test org.aspectj aspectjweaver 1.8.13
package cn.simplelife.service;/*** @ClassName IEmployeeService* @Description* @Author simplelife* @Date 2022/11/23 10:50* @Version 1.0*/public interface IEmployeeService {void save(String name, String password);
}
package cn.simplelife.service.impl;import cn.simplelife.service.IEmployeeService;/*** @ClassName IEmployeeServiceImpl* @Description* @Author simplelife* @Date 2022/11/23 10:51* @Version 1.0*/public class IEmployeeServiceImpl implements IEmployeeService {@Overridepublic void save(String name, String password) {System.out.println("保存:" + name + " " + password);}
}
package cn.simplelife.utils;/*** @ClassName MyTransactionManger* @Description* @Author simplelife* @Date 2022/11/23 10:52* @Version 1.0*/public class MyTransactionManger {public void begin() {System.out.println("开启事务");}public void commit() {System.out.println("提交事务");}public void rollback() {System.out.println("回滚事务");}
}
package cn.simplelife.service.impl;import cn.simplelife.service.IEmployeeService;
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;import static org.junit.Assert.*;/*** @ClassName IEmployeeServiceImplTest* @Description* @Author simplelife* @Date 2022/11/23 11:05* @Version 1.0*/@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class IEmployeeServiceImplTest {@Autowiredprivate IEmployeeService iEmployeeService;@Testpublic void save() {System.out.println(iEmployeeService.getClass());iEmployeeService.save("张三", "123456");}
}
内存解释:
org.springframework spring-context 5.0.8.RELEASE org.springframework spring-test 5.0.8.RELEASE test junit junit 4.12 test org.aspectj aspectjweaver 1.8.13
package cn.simplelife.service;/*** @ClassName IEmployeeService* @Description* @Author simplelife* @Date 2022/11/23 10:50* @Version 1.0*/public interface IEmployeeService {void save(String name, String password);
}
package cn.simplelife.service.impl;import cn.simplelife.service.IEmployeeService;/*** @ClassName IEmployeeServiceImpl* @Description* @Author simplelife* @Date 2022/11/23 10:51* @Version 1.0*/
@Service
public class IEmployeeServiceImpl implements IEmployeeService {@Overridepublic void save(String name, String password) {System.out.println("保存:" + name + " " + password);}
}
package cn.simplelife.utils;import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;/*** @ClassName MyTransactionManger* @Description* @Author simplelife* @Date 2022/11/23 10:52* @Version 1.0*/@Component
@Aspect
public class MyTransactionManger {@Pointcut("execution(* cn.simplelife.service.impl.*ServiceImpl.*(..))")public void txPoint() {}@Before("txPoint()")public void begin() {System.out.println("开启事务");}@AfterReturning("txPoint()")public void commit() {System.out.println("提交事务");}@AfterThrowing("txPoint()")public void rollback() {System.out.println("回滚事务");}
}
package cn.simplelife.service.impl;import cn.simplelife.service.IEmployeeService;
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;import static org.junit.Assert.*;/*** @ClassName IEmployeeServiceImplTest* @Description* @Author simplelife* @Date 2022/11/23 11:05* @Version 1.0*/@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class IEmployeeServiceImplTest {@Autowiredprivate IEmployeeService iEmployeeService;@Testpublic void save() {System.out.println(iEmployeeService.getClass());iEmployeeService.save("张三", "123456");}
}
注解 | 描述 |
---|---|
@Aspect | 用于定义一个切面 |
@Pointcut | 用于定义切点表达式 |
@Before | 用于定义前置通知 |
@AfterReturning | 用于定义后置通知 |
@AfterThrowing | 用于定义异常时通知 |
@Around | 用于定义环绕通知 |
@After | 用于定义最终通知 |