SSM-Spirng-面向切面编程-使用XML配置开发SpringAOP
XML需要引入AOP命名空间,AOP可配置元素:
AOP 配置元素 | 用途 | 备注 |
---|---|---|
aop:advisor | 定义AOP的通知器 | 老方式,少用 |
aop:aspect | 定义一个切面 | |
aop:before | 定义前置通知 | |
aop:after | 定义后置通知 | |
aop:around | 定义环绕通知 | |
aop:after-retuming | 定义返回通知 | |
aop:after-throwing | 定义异常通知 | |
aop:config | 顶层的AOP配置元素 | AOP 的配置是以它为开始的 |
aop:declare-parents | 给通知引入新的额外接口,增强功能 | |
aop:pointcut | 定义切点 |
案例:
//定义一个接口
public interface RoleService {
public void printRole(Role role);
}
实现类:
public class RoleServiceImpl implements RoleService {
public void printRole(Role role) {
System.out.println(role.getId());
System.out.println(role.getRoleName());
System.out.println(role.getNote());
}
}
切面类
public class XmlAspect {
public void before(){
System.out.println("before...");
}
public void after(){
System.out.println("after...");
}
public void afterThrowing(){
System.out.println("afterThrowing...");
}
public void afterReturning(){
System.out.println("afterReturning...");
}
}
没有任何的注解,需要使用XML去向SpringIOC容器描述他们
前置通知,后置通知,返回通知,异常通知
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
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/util http://www.springframework.org/schema/beans/spring-util.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-context.xsd
">
<!--bean-->
<bean id="xmlAspect" class="springAopXml.aspect.XmlAspect"></bean>
<bean id="roleService" class="springAopXml.service.impl.RoleServiceImpl"></bean>
<aop:config>
<!--引用xmlAspect作为切面-->
<aop:aspect ref="xmlAspect">
<!--定义通知-->
<aop:before method="before" pointcut="execution(* springAopXml.service.impl.RoleServiceImpl.printRole(..))"/>
<aop:after method="after" pointcut="execution(* springAopXml.service.impl.RoleServiceImpl.printRole(..))"/>
<aop:after-returning method="afterReturning" pointcut="execution(* springAopXml.service.impl.RoleServiceImpl.printRole(..))"/>
<aop:after-throwing method="afterThrowing" pointcut="execution(* springAopXml.service.impl.RoleServiceImpl.printRole(..))"/>
</aop:aspect>
</aop:config>
</beans>
通过引入XML定义AOP命名空间,定义roleService类和切面类,然后通过< aop:config > 取定义AOP的内容信息:
- < aop:aspect >:用于定义切面类,这里是xmlAspect
- < aop:before >:定义前置通知
- < aop:after >:定义后置通知
- < aop:after-returning >:定义返回通知
- < aop:after-throwing >:定义异常通知
- < aop:pointcut >:定义切点
环绕通知
和其他通知一样,可以织入到约定流程当中
//在切面类加入新方法
public void around(ProceedingJoinPoint jp){
System.out.println("around before...");
try {
jp.proceed();
} catch (Throwable throwable) {
new RuntimeException("回调原来流程,产生异常。。。。");
}
System.out.println("around after...");
}
加入下面配置即可使用环绕通知:
<aop:around method="around" pointcut="execution(* springAopXml.service.impl.RoleServiceImpl.printRole(..))"/>
测试
public class test {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("Spring/spring06.xml");
RoleService bean = context.getBean(RoleService.class);
springApo.pojo.Role role= new springApo.pojo.Role();
role.setId(1L);
role.setRoleName("ssss");
role.setNote("23123123");
bean.printRole(role);
}
}
### 给通知传递参数
通过XML也能引入参数到通知中,以下列为例:
public void before(Role role){
System.out.println(role.getId+role.getRoleName,+role.getNote);
}
带上参数role,修改前置通知的配置
<aop:before method="before"
pointcut="execution(* springAopXml.service.impl.RoleServiceImpl.printRole(..)) and args(role)"/>
引入
注解能做的事情,XML同样能做