Aop的作用:在程序运行期间,不修改源码对已有方法进行增强
Aspect 切面 :切入点(要增强的方法)和通知(怎么增强)之间的关系
连接点: 可以通过jdk代理的方法
切入点: 需要被增强的方法
Advice: 增强
Target:目标对象
Proxy:代理
aop: config 用于声明开始aop的配置
aop:aspect 配置切面 (配置切入点和终端之间的关系)
aop:pointcut 配置切入点表达式
aop:before 用于配置前置通知
aop:after-returning 后置通知
aop: after-throwing 异常通知
aop: after 最终通知
aop: around 环绕通知
AccountDaoImpl .java
package com.bky.dao.impl;
import com.bky.dao.AccountDao;
public class AccountDaoImpl implements AccountDao {
@Override
public void add() {
int a=1/0;
System.out.println("执行添加功能");
}
@Override
public void delete() {
System.out.println("执行删除功能");
}
@Override
public void update() {
System.out.println("执行修改功能");
}
@Override
public void select() {
System.out.println("执行查询功能");
}
}
AccountDao .java
package com.bky.dao;
public interface AccountDao {
void add();
void delete();
void update();;
void select();
}
AccountLog .java
package com.bky.log;
/**
* 日志配置类
*/
public class AccountLog {
//前置通知
public void Qianzhi(){
System.out.println("前置通知开始进入日志");
}
//后置通知
public void Houzhi(){
System.out.println("后置通知开始记录日志");
}
//异常通知
public void Yichang(){
System.out.println("捕获到异常日志");
}
//最终通知
public void Zuizhong(){
System.out.println("最终通知执行了");
}
}
AccountService .java
package com.bky.service;
public interface AccountService {
void add();
void delete();
void update();;
void select();
}
AccountServiceImpl .java
package com.bky.service.impl;
import com.bky.dao.AccountDao;
import com.bky.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void add() {
accountDao.add();
}
@Override
public void delete() {
accountDao.delete();
}
@Override
public void update() {
accountDao.update();
}
@Override
public void select() {
accountDao.select();
}
}
Test .java
package com.bky.test;
import com.bky.dao.AccountDao;
import com.bky.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
@org.junit.Test
public void testo1(){
ApplicationContext applicationContext
=new ClassPathXmlApplicationContext("xml/beans.xml");
AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
AccountDao accountDao = applicationContext.getBean("accountDao", AccountDao.class);
System.out.println(accountDao);
System.out.println(accountService);
}
@org.junit.Test
public void test02(){
ApplicationContext applicationContext
=new ClassPathXmlApplicationContext("xml/beans.xml");
AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
accountService.add();
accountService.delete();
accountService.select();
accountService.update();;
}
}
beans.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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置service实例-->
<bean id="accountService" class="com.bky.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<bean id="accountDao" class="com.bky.dao.impl.AccountDaoImpl">
</bean>
<!--
<aop:config> 用于声明aop配置
<aop:aspect> 配置切面 (想要增强的通知)
aop:pointcut 配置切入点表达式
aop:before 用于配置前置通知
-->
<!--切入点表达式-->
<bean id="accountlog" class="com.bky.log.AccountLog">
</bean>
<aop:config >
<!--配置切入点表达式 -->
<aop:pointcut id="pt01" expression="execution(public void com.bky.service.impl.AccountServiceImpl.*(..))"/>
<!--配置切面-->
<aop:aspect id="logaspet " ref="accountlog">
<!--配置前置通知-->
<aop:before method="Qianzhi" pointcut-ref="pt01"></aop:before>
<!--配置后置通知-->
<aop:after-returning method="Houzhi" pointcut-ref="pt01"></aop:after-returning>
<!--异常通知-->
<aop:after-throwing method="Yichang" pointcut-ref="pt01"></aop:after-throwing>
<!--最终通知-->
<aop:after method="Zuizhong" pointcut-ref="pt01"></aop:after>
</aop:aspect>
</aop:config>
</beans>