基于xml配置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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="myMathCal" class="com.lwt.xml.Calculator.impl.MyMathCal"></bean>
<bean id="logUtilsAOP" class="com.lwt.xml.Calculator.utils.LogUtilsAOP"></bean>
<bean id="logUtilsAroundAOP" class="com.lwt.xml.Calculator.utils.LogUtilsAroundAOP"></bean>
<bean id="multiAOP" class="com.lwt.xml.Calculator.utils.MultiAOP"></bean>
<!-- aop名称空间-->
<aop:config>
<!-- 全局切入点表达式,无也可-->
<aop:pointcut id="globalPoint" expression="execution(* com.lwt.xml.Calculator.impl.*.*(..))"/>
<!-- 告诉spring哪个是切面类,相当于添加了@Aspect注解,执行顺序默认按配置顺序-->
<aop:aspect ref="logUtilsAOP" order="1">
<aop:pointcut id="myexecution" expression="execution(* com.lwt.xml.Calculator.impl.*.*(..))"/>
<aop:before method="logStart" pointcut="execution(* com.lwt.xml.Calculator.impl.*.*(..))"></aop:before>
<aop:after-returning method="logReturn" pointcut-ref="myexecution" returning="result"></aop:after-returning>
<aop:after-throwing method="logException" pointcut-ref="myexecution" throwing="e"></aop:after-throwing>
<aop:after method="logFinally" pointcut-ref="myexecution"></aop:after>
</aop:aspect>
<aop:aspect ref="multiAOP" order="2">
<aop:before method="logStart" pointcut-ref="globalPoint"></aop:before>
<aop:after-returning method="logReturn" pointcut-ref="globalPoint" returning="result"></aop:after-returning>
<aop:after-throwing method="logException" pointcut-ref="globalPoint" throwing="e"></aop:after-throwing>
<aop:after method="logFinally" pointcut-ref="globalPoint"></aop:after>
</aop:aspect>
<aop:aspect ref="logUtilsAroundAOP" order="3">
<aop:around method="myAround" pointcut-ref="globalPoint"></aop:around>
</aop:aspect>
<!-- 在切面类中使用4+1个通知方法配置切面中的通知方法何时何地运行-->
</aop:config>
</beans>
public class AopTest {
/*
有接口
*/
@Test
public void test1(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-configAOP1.xml");
// 从ioc容器中拿到目标对象,注意:如果想要用类型,一定用他的接口类型,不用用本类
Calculator bean1 = ioc.getBean(Calculator.class);
/*如果拿被代理对象获取,则无法实现*/
bean1.add(1,2);
System.out.println(bean1); // com.lwt.Calculator.impl.MyMathCal@d29f28
// ioc容器中保存的组件是代理对象$Proxy26
System.out.println("ioc容器中保存的组件是代理对象"+bean1.getClass());
Object bean2 = ioc.getBean("myMathCal");
System.out.println(bean2);
System.out.println("ioc容器中保存的组件是代理对象"+bean2.getClass());
}
/*
无接口
*/
@Test
public void test2(){
// ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-configAOP1.xml");
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-configAOP1_xml.xml");
// 从ioc容器中拿到目标对象,注意:如果想要用类型,一定用他的接口类型,不要用本类。但是,当不实现接口后,则用假神被代理类或通过默认首字母小写id来获取bean对象
MyMathCal bean1 = ioc.getBean(MyMathCal.class);
bean1.div(4,2);
System.out.println(bean1); // com.lwt.Calculator.impl.MyMathCal@d29f28
// ioc容器中保存的组件是代理对象class com.lwt.Calculator.impl.MyMathCal$$EnhancerBySpringCGLIB$$9b6eec87
System.out.println("ioc容器中保存的组件是代理对象"+bean1.getClass());
Object bean2 = ioc.getBean("myMathCal");
System.out.println(bean2);
System.out.println("ioc容器中保存的组件是代理对象"+bean2.getClass());
}
}