Spring课程 Spring入门篇 6-3 ProxyFactoryBean及相关内容(下)
1 解析
2 代码演练
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。2.1 通配符使用
1 解析
2 代码演练
2.1 通配符使用
测试类:
package com.imooc.test.aop; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner; import com.imooc.aop.api.BizLogic; import com.imooc.test.base.UnitTestBase; @RunWith(BlockJUnit4ClassRunner.class) public class TestAOPAPI extends UnitTestBase{ public TestAOPAPI() { super("classpath:spring-aop-api.xml"); // TODO Auto-generated constructor stub } @Test public void testSave(){ BizLogic bLogic = super.getbean("bizLogicImpl"); bLogic.sove(); } }
配置文件:
<?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" xmlns:context="http://www.springframework.org/schema/context" 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-4.0.xsd 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" > <bean id="moocBeforeAdvice" class="com.imooc.aop.api.MoocBeforeAdvice"></bean> <bean id="moocAfterReturningAdvice" class="com.imooc.aop.api.MoocAfterReturningAdvice"></bean> <bean id="moocMethodInterceptor" class="com.imooc.aop.api.MoocMethodInterceptor"></bean> <bean id="moocThrowsAdvice" class="com.imooc.aop.api.MoocThrowsAdvice"></bean>
<bean id="bizLogicImplTarget" class="com.imooc.aop.api.BizLogicImpl"></bean> <!-- 执行所有sa打头的方法,都会引入切面 --> <!-- 实现之一:NameMatchMethodPointcut,根据方法名字进行匹配 ,自带方法 --> <bean id="pointcutBean" class="org.springframework.aop.support.NameMatchMethodPointcut"> <property name="mappedNames"> <list> <value>sa*</value> </list> </property> </bean> <bean id="defaultAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="advice" ref="moocBeforeAdvice"></property> <property name="pointcut" ref="pointcutBean"></property> </bean> <!-- 从这里开始,引入多个通知 --> <!-- 引入的业务类不是ProxyFactoryBean ,而是getObject返回的,即 target==》bizLogicImplTarget --> <bean id="bizLogicImpl" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 具体业务类 --> <property name="target"> <ref bean="bizLogicImplTarget"/> </property> <!-- list通知不是按照顺序执行的,而是按照通知的先后顺序执行的,先执行前置通知,然后执行环绕通知,最后执行后置通知 --> <property name="interceptorNames"> <list> <!-- <value>defaultAdvisor</value> --> <!-- <value>moocAfterReturningAdvice</value> --> <!-- <value>moocMethodInterceptor</value> --> <!-- <value>moocThrowsAdvice</value> --> <value>mooc*</value> </list> </property> </bean> </beans>
实现类:
package com.imooc.aop.api; public class BizLogicImpl implements BizLogic{ @Override public String sove() { System.out.println("syso save"); return "BizLogicImpl save"; } }
接口:
package com.imooc.aop.api; public interface BizLogic { String sove(); }
环绕通知:
package com.imooc.aop.api; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; /** * 引入环绕通知 * * 实现 MethodInterceptor 接口 * @author weijingli * */ public class MoocMethodInterceptor implements MethodInterceptor{ @Override public Object invoke(MethodInvocation methodinvocation) throws Throwable { //实现前置通知的方法 //得到方法 得到类 System.out.println("MoocMethodInterceptor1:"+methodinvocation.getMethod().getName()+" " +methodinvocation.getStaticPart().getClass().getName()); Object obj = methodinvocation.proceed(); //实现后置通知方法 System.out.println("MoocMethodInterceptor2:"+obj); return obj; } }
打印日志:
四月 23, 2019 6:56:02 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7fe219e7: startup date [Tue Apr 23 18:56:02 CST 2019]; root of context hierarchy 四月 23, 2019 6:56:02 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [spring-aop-api.xml] MoocMethodInterceptor1:sove java.lang.reflect.Method syso save MoocMethodInterceptor2:BizLogicImpl save 四月 23, 2019 6:56:04 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@7fe219e7: startup date [Tue Apr 23 18:56:02 CST 2019]; root of context hierarchy

更多精彩