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();
        }
  • 相关阅读:
    JAVA导出EXCEL表格
    解决springboot配置@ControllerAdvice不能捕获 NoHandlerFoundException问题
    Mysql 查看定时器 打开定时器 设置定时器时间
    IDEA @Autowired 出现红色下划线 报红
    IntelliJ IDEA报warn class is never used
    UML类图符号 各种关系说明以及举例
    提升单元测试体验的利器--Mockito使用总结
    maven2中snapshot快照库和release发布库的应用
    Maven最佳实践-distributionManagement
    访问GitLab的PostgreSQL数据库
  • 原文地址:https://www.cnblogs.com/qjt970518--/p/7263602.html
Copyright © 2011-2022 走看看