spring aop配置
业务逻辑:当Poet朗诵完诗歌,观众自动鼓掌
步骤1:创建目标类(被代理类),通知类
??? 目标类:
??? //创建接口的代理比创建继承的代理更松耦合
??? public interface IPoet{
??????????? void elocnte(String content);
??? }
??? public class Poet implements IPoet{
??????????? public Poet(){}
??????????? public return elocnte(String content){
???????????????????? System.out.println(content);
???????????? }
??? }
??? 通知类:
??? package com.spring.aop.demo;
??? public class AudienceAdvice implements MethodAfterAdvice{
????????????? private Audience audience;
??????????????public AudienceAdvice(){}
????????????? //实现返回后通知接口? 当诗人朗诵完诗歌成功后鼓掌
????????????? public?void afterReturning(Object returnValue,Method method,Object[] args,Object target){
????????????????????? audience.clapHand();
????????????? }
????????????? public void setAudience(Audience audience){
??????????????????? this.audience=audience;
???????????? }??
???? }
???? //观众? 行为:鼓掌
???? public class Audience{
?????????????? public void clapHand(){
????????????????????????? System.out.println("audience clap hand");
??????????????? }
?????}
步骤2:定义切点(切点即为全部连接点的一个子集)切点定义的方法有几种,最常用的为正则表达式和AspectJ
???? <!-- 声明正则表达式切点-->
???? <bean id="elocntePointcut" value=".*elocnte"/>
?????</bean>
???? <!-- 声明AspectJ切点-->
???? <bean id="elocntePointcut" value="execution(* *.elocnte(..))"/>
???? </bean>
步骤3:定义通知
????? <!-- 定义通知-->
???? <bean id="audienceAdvice" ref="audience"/>
???? </bean>
步骤3:定义切面
???? <!--声明切面 即关联切点和通知-->
???? <bean id="audienceAdvisor" ref="audienceAdvice"/>
??????????? <property name="pointcut" ref="elocntePointcut"/>
???? </bean>
???? <!--使用下面的配置可以合并上面的声明正则表达式切点和声明切点动作-->
???? <bean id="aduienceAdvisor"???????????????????????????????????????????
?????????????? ref="audienceAdvice"/>
????????????<property name="apttern" value=".*elocnte"/>
???? </bean>
???? <!--如果是使用AspectJ声明的切点-->
???? <bean id="aduienceAdvicor"????????????
????????????????? ref="audienceAdvice"/>
???????????? <property name="expression" value="execution(* *.elocnte(..))"/>
????? </bean>
步骤4:使用ProxyFactoryBean创建目标类的代理
??????<bean id="poetProxy" ref="com.spring.aop.demo.Poet"/>
????????????? <property name="interceptorNames">
??????????????????? <list>
???????????????????????????<value>audienceAdvisor</value>
????????????????????</list>
????????????? </property>
??????????????<property name="proxyInterfaces">
????????????????????<list>
??????????????????????????? <value>com.spring.aop.demo.IPoet</value>
??????????????????? </list>
????????????? </property>
??????</bean>
????? <!--使用抽象代理-->
??????<bean id="poetProxyBase" class=="org.springframework.aop.framework.ProxyFactionBean"
???????????????? abstract="true">
????????????? <property name="proxyInterfaces"
??????????????????????? value="com.spring.aop.demo.IPoet"/>
????????????? <property name="interceptorNames" value="audienceAdvisor"/>
????? </bean>
????? <bean id="stevieProxy" parent="poetProxyBase">
????????????? <property name="target" ref="stevie"/>
?????? </bean>
?????? <!--使用自动代理-->
?????? <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
步骤5:测试
?????? public Test{
????????????? public static void main(String[] args){
????????????????????? ApplicationContext c=new ClassPathXmlApplicationContext("applicationContext.xml");
??????????????????????IPoet poet=(IPoet)c.getBean("poetProxy");
??????????????????????poet.elocnte("lala lala la");
??????????????????????//如果使用自动代理则写为????????????????????
??????????????????????//ApplicationContext c=new ClassPathXmlApplicationContext("applicationContext.xml");
??????????????????????//IPoet poet=(IPoet)c.getBean("poet");
??????????????????????//poet.elocnte("lala lala la");???????
??????????????}
?
??????? }
?
//代码还未验证
???????