码迷,mamicode.com
首页 > 其他好文 > 详细

spreing 增强

时间:2017-08-20 10:13:54      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:http   row   接口   spring   factor   ref   3.x   print   port   

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 增强

标签:http   row   接口   spring   factor   ref   3.x   print   port   

原文地址:http://www.cnblogs.com/s1297-lgy/p/7398775.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!