spring的增强分类:前置增强,后置增强,异常增强,环绕增强。
增强的意义在于我们不改变接口,实现类,不动方法的前提下在原内容上增加内容。
首先我们看一下前置增强:前置增强必须实现的接口:MethodBeforeAdvice
- MethodBeforeAdvice
- package cn.happy.spring11;
- /**
- * Created by lihuohuo on 2017/7/31.
- */
- public interface IDoSome {
- public void add();
- public void add1();
- public void add2();
- }
- package cn.happy.spring11;
- /**
- * Created by lihuohuo on 2017/7/31.
- */
- public class IDoSomes implements IDoSome{
- public void add() {
- System.out.println("=========1=======");
- }
- public void add1() {
- System.out.println("=========2=======");
- }public void add2() {
- System.out.println("=========3=======");
- }
- }
- package cn.happy.spring11;
- import org.springframework.aop.MethodBeforeAdvice;
- import java.lang.reflect.Method;
- /**
- * Created by lihuohuo on 2017/7/31.
- */
- public class MyBeforeAdvice implements MethodBeforeAdvice{
- public void before(Method method, Object[] objects, Object o) throws Throwable {
- System.out.println("=========before=======");
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- 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 ">
- <bean id="Idosome" class="cn.happy.spring11.IDoSomes"></bean>
- <bean id="beforeAdvice" class="cn.happy.spring11.MyBeforeAdvice"></bean>
- <bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="target" ref="Idosome"></property>
- <property name="interceptorNames" value="beforeAdvice"></property>
- </bean>
- </beans>
- @Test
- public void test01(){
- ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext07.xml");
- IDoSome i=(IDoSome)ctx.getBean("someProxy");
- i.add();
- i.add1();
- i.add2();
- }
- package cn.happy.spring12;
- /**
- * Created by lihuohuo on 2017/7/31.
- */
- public interface IDoSome {
- public void add();
- public