zoukankan      html  css  js  c++  java
  • 顾问

    public interface ISomeService {
        public void doSome();
        public void dade();
    }
    import org.springframework.aop.MethodBeforeAdvice;
    import java.lang.reflect.Method;
    
    public class MyBeforeAdvise implements MethodBeforeAdvice {
    
        public void before(Method method, Object[] objects, Object o) throws Throwable {
            System.out.println("=============log==================");
        }
    }
    public class SomeService implements ISomeService {
        //核心业务
        public void doSome(){
            System.out.println("我们都要找到Java开发工作,薪资6,7,8,9,10K");
        }
    
        public void dade() {
            System.out.println("++++++++++++++de+++++++++++++");
        }
    }

    1.名称匹配方法顾问

    配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:p="http://www.springframework.org/schema/p"
           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
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
    ">
       <!--01.目标对象-->
        <bean id="someService" class="cn.bdqn.spring13.SomeService"></bean>
        <!--02.增强 通知-->
        <bean id="beforeAdvice" class="cn.bdqn.spring13.MyBeforeAdvise"></bean>
        <!--02.增强 :顾问-->
        <bean id="beforeAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
            <property name="advice" ref="beforeAdvice" ></property>
            <property name="mappedNames" value="doSome"></property>
        </bean>
        <!--03.aop -->
        <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
            <!--配置需要增强的目标对象-->
            <property name="target" ref="someService"></property>
            <!--做怎么样的增强-->
            <property name="interceptorNames" value="beforeAdvisor"></property>
        </bean>
    </beans>

    2.正则 顾问

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:p="http://www.springframework.org/schema/p"
           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
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
    ">
       <!--01.目标对象-->
        <bean id="someService" class="cn.bdqn.spring14.SomeService"></bean>
        <!--02.增强 通知-->
        <bean id="beforeAdvice" class="cn.bdqn.spring14.MyBeforeAdvise"></bean>
        <!--02.增强 :顾问-->
        <bean id="beforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
            <property name="advice" ref="beforeAdvice" ></property>
            <property name="pattern" value=".*b.*"></property>
        </bean>
        <!--03.aop -->
        <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
            <!--配置需要增强的目标对象-->
            <property name="target" ref="someService"></property>
            <!--做怎么样的增强-->
            <property name="interceptorNames" value="beforeAdvisor"></property>
        </bean>
    </beans>

    单侧

     /*名称匹配方法顾问*/
        @Test
        public void test08(){
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring11.xml");
            ISomeService service = (ISomeService) ctx.getBean("proxyService");
            service.doSome();
            service.dade();
        }
     /*正则   顾问*/
        @Test
        public void test09(){
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring12.xml");
            ISomeService service = (ISomeService) ctx.getBean("proxyService");
            service.doSome();
            service.dade();
        }
  • 相关阅读:
    Log Explorer的使用
    Windows消息大全
    Devepress LayoutControl的使用
    IIS7虚拟目录出现HTTP错误500.19(由于权限不足而无法读取配置文件)
    检索参数信息并填充指定的 SqlCommand 对象的 Parameters 集合
    Failed to access IIS metabase.
    SQL Server FOR XML PATH 语句的应用
    C#调用API:mouse_event 模拟鼠标事件
    C#执行SQL脚本
    ADO.NET 中的表达式
  • 原文地址:https://www.cnblogs.com/qjt970518--/p/7263602.html
Copyright © 2011-2022 走看看