spreing 增强

Posted MyBatis

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spreing 增强相关的知识,希望对你有一定的参考价值。

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();
    }
技术分享

以上是关于spreing 增强的主要内容,如果未能解决你的问题,请参考以下文章

灰度图像直方图变换的一些代码

增强学习笔记 第十章 On-Policy控制的近似

PHPSpreadsheet - 如何创建一个额外的选项卡?

级联行删除javascript

ansible plugins简介

如何在WORDPRESS首页插入一段自定义代码?