zoukankan      html  css  js  c++  java
  • Spring.........

    1.前置增强

    接口     类

    public interface ISomeService {
        public void doSome();
    }
    public class SomeService implements ISomeService {
        //核心业务
        public void doSome(){
            System.out.println("我们都要找到Java开发工作,薪资6,7,8,9,10K");
        }
    }
    复制代码
    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==================");
        }
    }
    复制代码

    配置文件

    复制代码
    <?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.spring09aop01.SomeService"></bean>
    
        <!--02.增强 通知-->
        <bean id="beforeAdvice" class="cn.bdqn.spring09aop01.MyBeforeAdvise"></bean>
    
        <!--03.aop -->
        <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
            <!--配置需要增强的目标对象-->
            <property name="target" ref="someService"></property>
            <!--做怎么样的增强-->
            <property name="interceptorNames" value="beforeAdvice"></property>
            <property name="proxyTargetClass" value="true"></property>
        </bean>
    
    </beans>
    复制代码

    单侧

    复制代码
    @Test
        // 前置增强
        public void test04(){
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring07aop01.xml");
            SomeService service = (SomeService) ctx.getBean("proxyService");
            service.doSome();
        }
    复制代码

    2.后置增强

    public class MyAfterReturningAdvice implements AfterReturningAdvice {
        public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
            System.out.println("===========after================");
        }
    }

    配置文件

    复制代码
    <?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.spring10afterreturingadvice.SomeService"></bean>
    
        <!--02.增强 通知-->
        <bean id="afterAdvice" class="cn.bdqn.spring10afterreturingadvice.MyAfterReturningAdvice"></bean>
    
        <!--03.aop -->
        <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
            <!--配置需要增强的目标对象-->
            <property name="target" ref="someService"></property>
            <!--做怎么样的增强-->
            <property name="interceptorNames" value="afterAdvice"></property>
    
        </bean>
    
    
    </beans>
    复制代码

    单侧

    复制代码
    @Test
        // 后置增强
        public void test05(){
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring08aop02.xml");
            ISomeService service = (ISomeService) ctx.getBean("proxyService");
            service.doSome();
        }
    复制代码

    3.环绕增强

    复制代码
    public class MyMethodInterceptor implements MethodInterceptor {
        public Object invoke(MethodInvocation methodInvocation) throws Throwable {
            System.out.println("before");
            methodInvocation.proceed();
            System.out.println("after");
            return null;
        }
    }
    复制代码

    配置文件

    复制代码
    <?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.spring11methodinterceptor.SomeService"></bean>
    
        <!--02.增强 通知-->
        <bean id="methodAdvice" class="cn.bdqn.spring11methodinterceptor.MyMethodInterceptor"></bean>
    
        <!--03.aop -->
        <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
            <!--配置需要增强的目标对象-->
            <property name="target" ref="someService"></property>
            <!--做怎么样的增强-->
            <property name="interceptorNames" value="methodAdvice"></property>
    
        </bean>
    
    
    </beans>
    复制代码

    单侧

    复制代码
     @Test
        // 环绕增强
        public void test06(){
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring09aop03.xml");
            ISomeService service = (ISomeService) ctx.getBean("proxyService");
            service.doSome();
        }
    复制代码

    4.异常增强

    public class MyThrowsAdvice implements ThrowsAdvice {
        public void afterThrowing(Exception ex){
            System.out.println("错误");
        }
    
    }
    public interface IHHJJ {
        public void run();
        public void run(String style);
    }
    复制代码
    public class HHJJImpl implements IHHJJ {
        public void run() {
    
        }
    
        public void run(String style) {
    
        }
    }
    复制代码

    配置文件

    复制代码
    <?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.spring12throwadvice.SomeService"></bean>
    
        <!--02.增强 通知-->
        <bean id="throwsAdvice" class="cn.bdqn.spring12throwadvice.MyThrowsAdvice"></bean>
    
        <!--03.aop -->
        <bean id="proxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
            <!--配置需要增强的目标对象-->
            <property name="target" ref="someService"></property>
            <!--做怎么样的增强-->
            <property name="interceptorNames" value="throwsAdvice"></property>
    
        </bean>
    
    
    </beans>
    复制代码

    单侧

    复制代码
     @Test
        // 异常增强
        public void test07(){
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContextspring10aop04.xml");
            ISomeService service = (ISomeService) ctx.getBean("proxyService");
            service.doSome();
        }
    复制代码
  • 相关阅读:
    ie下常见的css兼容问题
    jQuery Easing 使用方法及其图解
    数组中常用的15个方法
    js按位运算符及其妙用
    图片格式知多少
    Linux top命令的用法详解
    Another app is currently holding the yum lock; waiting for it to exit...
    ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this S tatem
    Gitlab与Jenkins结合构成持续集成(CI)环境
    cobbler无人值守安装
  • 原文地址:https://www.cnblogs.com/zfx123--/p/7263794.html
Copyright © 2011-2022 走看看