1.结构图
2.业务层接口IAccountService:
package com.itheima.service;
/*业务层接口*/
public interface IAccountService {
/*模拟保存账户*/
/*无返回,无参数*/
void saveAccount();
/*模拟更新账户*/
/*无返回,有参数*/
void updateAccount(int i);
/*模拟账户删除*/
/*有返回,无参数*/
int deleteAccount();
}
3.实现类AccountServiceImp:
package com.itheima.service.Imp;
import com.itheima.service.IAccountService;
/*
* 账户的业务层实现类
* */
public class AccountServiceImp implements IAccountService {
@Override
public void saveAccount() {
System.out.println("保存方法开始执行");
}
@Override
public void updateAccount(int i) {
System.out.println("更新方法");
}
@Override
public int deleteAccount() {
System.out.println("删除方法");
return 0;
}
}
4.切面Logger
package com.itheima.utils;
/*
* 用于记录日志的工具类,它里面提供了公共的代码
*/
public class Logger {
/*
* 用于打印日志,计划在其切入点方法执行之前就执行(切入点方法就是业务层方法)
* */
public void printLog(){
System.out.println("Logger类中的printLog方法开始记录日志了。。。。");
}
}
5.测试类AopTest:
package com.itheima;
import com.itheima.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopTest {
public static void main(String[] args) {
/*1. 获取容器*/
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
/*2. 获取对象*/
IAccountService as = ac.getBean("accountService",IAccountService.class);
/*3. 执行切面的方法噶*/
as.saveAccount();
/*4.执行没有进行切面的方法:结果是不会出现该语句的:Logger类中的printLog方法开始记录日志了。。。。*/
as.updateAccount(1);
as.deleteAccount();
/*5.要想每个方法执行切面增强的方法,可以使用通配符扫描包*/
}
}
6.配置文件bean.xml
<?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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置业务层:配置spring的IOC,把service对象配置进来-->
<bean id="accountService" class="com.itheima.service.Imp.AccountServiceImp"></bean><!--accountService需要增强加个日志-->
<!--
spring中基于XML的AOP配置
1. 把通知bean交给spring阿里管理
2. 使用aop:config标签表明开始AOP的配置
3. 使用aop:aspect表明配置切面
id属性:是给切面提供一个唯一的标识
ref属性:是指定通知类bean的id
4. 在aop:aspect标签的内部使用对应的标签来配置通知的类型
我们现在示例的是让printLog方法在切入点方法执行之前执行:所以是前置通知
aop:before:表示配置的前置通知
method属性:用于指定Logger类中的哪个方法是前置通知
//以上四步之前都不可能与accountService发生关联,所以得配置切面让它关联起来
pointcut属性:用于指定切入点的表达式,该表达式的含义指的是对业务层中哪些方法的增强
5.切点表达式的写法:
关键字:execution(表达式)
表达式:
访问修饰符 返回值 包名.包名.包名....类名.方法名(参数列表)
标准的写法(单个):
public void com.itheima.service.Imp.AccountServiceImp.saveAccount()
访问修饰符可以省略
void com.itheima.service.Imp.AccountServiceImp.saveAccount()
返回值可以使用通配符,表示任意返回值
* com.itheima.service.Imp.AccountServiceImp.saveAccount()
包名可以使用通配符,表示任意包。但是有几个包名,就需要写几个*.
* *.*.*.*.AccountServiceImp.saveAccount()
包名可以使用..表示当前包以及其子包
* *..AccountServiceImp.saveAccount()
。。。。以上的都是通配下的指定方法。。。。。
类名和方法名都可以使用*来通配
* *..*.*()//此时是空参的才能被切入调用
参数列表:
可以直接写数据类型:
基本引用直接写名称 int
引用类型写包名.类名的方式 java.lang.String
例如:* *..*.*(int)带有此类型的就会被使用
例如:* *..*.*(*)带有此类型的就会被使用
可以使用通配符表示任意类型,但是必须有参数
可以使用..全部都可以调用
全通配写法(包下全部):
* *..*.*(..)
重要: 实际开发中的切入点表达式通常写法:
切到业务层实现类下的所有方法
* com.itheima.service.impl.*.*(..)
-->
<!--配置Logger类-->
<bean id="logger" class="com.itheima.utils.Logger"></bean><!--写了一个日志的通知,里面可以写入日志的增强-->
<!--配置AOP-->
<aop:config>
<!--配置切面-->
<aop:aspect id="logAdvice" ref="logger"><!--配置切面,然后这个切面就在引用这个日志类-->
<!--配置通知的类型,并且建立通知方法和切入点方法的关联-->
<aop:before method="printLog" pointcut="execution(public void com.itheima.service.Imp.AccountServiceImp.saveAccount())"></aop:before>
</aop:aspect>
</aop:config>
</beans>
7.pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--spring依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--切面依赖-->
<!--语言包:解释bean文件中pointcut的包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
</dependencies>