Spring 的 aop操作
1 在spring里面进行aop操作,使用aspectj实现
(1)aspectj不是spring一部分,和spring一起使用进行aop操作
(2)Spring2.0以后新增了对aspectj支持
2 使用aspectj实现aop有两种方式
(1)基于aspectj的xml配置
(2)基于aspectj的注解方式
AOP准备操作
1.除了导入基本的jar包之外,还需要导入aop相关的jar包
2.创建spring核心配置文件,导入aop的约束
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- bean definitions here -->
</beans>
使用表达式配置切入点
1切入点,实际增强的方法
2常用的表达式
Execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
(1)execution(* cn.itcast.aop.Book.add(..))
(2)Execution(* cn.itcast.aop.Book.*(..))
(3)Execution(* *.*(..))
(4)匹配所有save开头的方法execution(* save*(..))
操作AOP的配置文件
<!-- 1配置对象 -->
<!-- 2 配置AOP操作-->
<aop:config>
<!-- 2.1配置 切入点 -->
<aop:pointcut expression=”execution(* cn.itcast.aop.Book.*(..))” id=”pointcut1”>
<!-- 2.2 配置切面
把增强用到方法上面
-->
<aop:aspect ref=”myBook”>
<!-- 配置增强类型
Method: 增强类里面使用哪个方法作为前置
-->
<aop:before method=”before1” pointcut-ref=”pointcut1” />
</aop:config>
后置
环绕
Log4j介绍
1 通过log4j可以看到程序运行过程中更详细的信息
(1)经常使用log4j查看日志
2 使用
(1)导入log4j的jar包
(2)复制log4j的配置文件,复制到src下面