Spring四种增强
前置增强 (org.springframework.aop.BeforeAdvice) 表⽰在⽬标⽅法执⾏前来实施增强
后置增强 (org.springframework.aop.AfterReturningAdvice) 表⽰在⽬标⽅法执⾏后来实施增强
环绕增强 (org.aopalliance.intercept.MethodInterceptor) 表⽰在⽬标⽅法执⾏前后同时实施增强
异常抛出增强 (org.springframework.aop.ThrowsAdvice) 表⽰在⽬标⽅法抛出异常后来实施增强
前置增强
创建ISomeService接⼝和实现类
package cn.happy.day07ProxyFactory;
/**
* Created by Administrator on 2018/3/8.
*/
public interface ISomeService {
public void doSome();
}
package cn.happy.day07ProxyFactory;
/**
* Created by Administrator on 2018/3/8.
*/
public class SomeServiceImpl implements ISomeService {
public void doSome() {
System.out.println("do Something");
}
}
创建实体类并实现MethodBeforeAdvice接⼝
package cn.happy.day07ProxyFactory;
import org.springframework.aop.MethodBeforeAdvice;
import flect.Method;
/**
* Created by Administrator on 2018/3/8.
*/
public class BeforeAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("前置增强");
}
}
配置xml⽂件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans" xmlns:aop="/schema/aop"
xmlns:context="/schema/context"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd /schema/context /schema/context/spring-context.xsd">
<!--⽬标类型-->
<bean id="service" class="cn.happy.day07ProxyFactory.SomeServiceImpl"></bean>
<!--增强-->
<bean id="before" class="cn.happy.day07ProxyFactory.BeforeAdvice"></bean>
<!--代理⼯⼚Bean-->
<bean id="Proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="service"/>
<property name="interceptorNames" value="before"/>
测试结果:
后置增强
后置增强与前置增⼀样,只是实体类实现接⼝是AfterReturnningAdvice
这⾥不做解释
环绕增强
环绕增强需实现MethodInterceptor
实体类⽅法
package cn.happy.day09ProxyFactory;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import flect.Method;
/**
* Created by Administrator on 2018/3/8.
*/
public class MethodAdvice implements MethodInterceptor {
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("环绕增强");
methodInvocation.proceed();
System.out.println("环绕增强--");
return null;
}
}
配置xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans" xmlns:aop="/schema/aop"
xmlns:context="/schema/context"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd /schema/context /schema/context/spring-context.xsd">
<!--⽬标类型-->
<bean id="service" class="cn.happy.day09ProxyFactory.SomeServiceImpl"></bean>
<!--增强-->
<bean id="method" class="cn.happy.day09ProxyFactory.MethodAdvice"></bean>
<!--代理⼯⼚Bean-->
<bean id="Proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="service"/>
<property name="interceptorNames" value="method"/>
</bean>
结果:
异常抛出增强
异常抛出增强需实现ThrowsAdvice
serviceImpl需要伪造⼀个异常
package cn.happy.day10ProxyFactory;
/**
* Created by Administrator on 2018/3/8.
*/
public class SomeServiceImpl implements ISomeService {
public void doSome() {
//制造异常
int i=5/0;
System.out.println("do Something");
spring framework组件}
}
实体类
package cn.happy.day10ProxyFactory;
import org.springframework.aop.ThrowsAdvice;
/**
* Created by Administrator on 2018/3/8.
*/
public class MyThrowsAdvice implements ThrowsAdvice {
public void afterThrowing(Exception ex){
System.out.println("⽹络异常,异常出现了");
}
}
配置xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans" xmlns:aop="/schema/aop"
xmlns:context="/schema/context"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd /schema/context /schema/context/spring-context.xsd">
<!--⽬标类型-->
<bean id="service" class="cn.happy.day10ProxyFactory.SomeServiceImpl"></bean>
<!--增强-->
<bean id="MyThrows" class="cn.happy.day10ProxyFactory.MyThrowsAdvice"></bean>
<!--代理⼯⼚Bean-->
<bean id="Proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="service"/>
<property name="interceptorNames" value="MyThrows"/>
</bean>
</beans>
package day10;
import cn.happy.day10ProxyFactory.ISomeService;
import org.junit.Test;
import t.ApplicationContext;
import t.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator on 2018/3/3.
*/
public class Test20180308 {
//异常增强
@Test
public void Spring(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("l"); ISomeService service=(Bean("Proxy");
try{
service.doSome();
}catch (Exception e){
e.printStackTrace();
}
}
}
结果:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论