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();
        }
  • 相关阅读:
    前端和后端的区别和分工
    IntelliJ和tomcat中的目录结构
    JAVA开发环境和IntelliJ工具安装
    Linux下Python+Selenium自动化模拟用户登录(备注:记录一次强行卸载rpm依赖包,引发的rpm、yum等命令异常,无法远程xftp工具)
    Docker 操作命令梳理(镜像下载、容器创建、Dockerfile)
    Centos 6.6 Docker安装(内网坏境)
    C# 连接Access2010 数据库之初探
    C#使用NLog记录日志
    现实两种
    C#中的两把双刃剑:抽象类和接口
  • 原文地址:https://www.cnblogs.com/qjt970518--/p/7263602.html
Copyright © 2011-2022 走看看