名称 , 正则 顾问(示例)

名称顾问

public interface ISomeService {
    public void doSome();
    public String LJL();
}

public class SomeService implements ISomeService {
    public void doSome() {
        System.out.println("弱水三千只取一瓢饮,沧海万倾唯系一江潮");
    }

    public String LJL() {
        return "ljl";
    }
}

public class MyBeforeAdvice implements MethodInterceptor {

    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("--------------------前");
        Object result=methodInvocation.proceed();
        String temp=null;
        if(result!=null){
            temp=(String)result;
            temp=temp.toUpperCase();
        }
        System.out.println("--------------------后");
        return temp;
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--目标对象-->
    <bean id="someService" class="cn.advice01.SomeService"></bean>
    <!--增强 通知-->
    <bean id="beforeAdvice" class="cn.advice01.MyBeforeAdvice"></bean>
    <!--增强 顾问-->
    <bean id="beforeAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
        <property name="advice" ref="beforeAdvice"></property>
        <property name="mappedName" value="doSome"></property>
    </bean>
    <!--aop-->
    <bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--指定目标对象-->
        <property name="target" ref="someService"></property>
        <!--做什么样的做强-->
        <property name="interceptorNames" value="beforeAdvice"></property>
    </bean>
</beans>
    /*名称顾问*/
    @Test
    public void GW01() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("advice01.xml");
        cn.advice01.ISomeService ss = (cn.advice01.ISomeService) ctx.getBean("someProxy");
        ss.doSome();
        String result = ss.LJL();
        System.out.println(result);
    }

正则顾问

public interface ISomeService {
    public void doSome();
    public String LJL();
}

public class SomeService implements ISomeService {
    public void doSome() {
        System.out.println("弱水三千只取一瓢饮,沧海万倾唯系一江潮");
    }

    public String LJL() {
        return "ljl";
    }
}

public class MyBeforeAdvice implements MethodInterceptor {

    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("--------------------前");
        Object result=methodInvocation.proceed();
        String temp=null;
        if(result!=null){
            temp=(String)result;
            temp=temp.toUpperCase();
        }
        System.out.println("--------------------后");
        return temp;
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--目标对象-->
    <bean id="someService" class="cn.advice02.SomeService"></bean>
    <!--增强 通知-->
    <bean id="beforeAdvice" class="cn.advice02.MyBeforeAdvice"></bean>
    <!--增强 顾问-->
    <bean id="beforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice" ref="beforeAdvice"></property>
        <property name="pattern" value=".*d.*"></property>
    </bean>
    <!--aop-->
    <bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!--指定目标对象-->
        <property name="target" ref="someService"></property>
        <!--做什么样的做强-->
        <property name="interceptorNames" value="beforeAdvice"></property>
        <!--业务类型有接口,Spring默认底层使用jdk代理,如不用jdk,强制没有接口的情况下使用cglib代理-->
        <property name="proxyTargetClass" value="true"></property>
    </bean>
</beans>

   /*正则顾问*/
    @Test
    public void GW02() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("advice02.xml");
        cn.advice02.ISomeService ss = (cn.advice02.ISomeService) ctx.getBean("someProxy");
        ss.doSome();
        String result = ss.LJL();
        System.out.println(result);
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值