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 String add1();
- public void add2();
- }
- package cn.happy.spring12;
- /**
- * Created by lihuohuo on 2017/7/31.
- */
- public class IDoSomes implements IDoSome {
- public void add() {
- System.out.println("=========1=======");
- }
- public String add1() {
- System.out.println("=========2=======");
- return "呵呵abc";
- }
- public void add2() {
- System.out.println("=========3=======");
- }
- }
- package cn.happy.spring12;
- import org.springframework.aop.AfterReturningAdvice;
- import org.springframework.aop.MethodBeforeAdvice;
- import java.lang.reflect.Method;
- /**
- * Created by lihuohuo on 2017/7/31.
- */
- public class MyAfterAdvice implements AfterReturningAdvice{
- public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
- System.out.println("========after=======");
- if(o!=null){
- System.out.println(o.toString().toUpperCase());
- }
- }
- }
- <?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.spring12.IDoSomes"></bean>
- <bean id="afterAdvice" class="cn.happy.spring12.MyAfterAdvice"></bean>
- <bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="target" ref="Idosome"></property>
- <property name="interceptorNames" value="afterAdvice"></property>
- </bean>
- </beans>
- @Test
- public void test02(){
- ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext08.xml");
- cn.happy.spring12.IDoSome i=(cn.happy.spring12.IDoSome)ctx.getBean("someProxy");
- i.add();
- String id=i.add1();
- System.out.println(id);
- i.add2();
- }
下面是環繞增強:實現接口是:MethodInterceptor
- package cn.happy.spring13;
- /**
- * Created by lihuohuo on 2017/7/31.
- */
- public interface IDoSome {
- public void add();
- public String add1();
- public void add2();
- }
- package cn.happy.spring13;
- /**
- * Created by lihuohuo on 2017/7/31.
- */
- public class IDoSomes implements IDoSome {
- public void add() {
- System.out.println("=========1=======");
- }
- public String add1() {
- System.out.println("=========2=======");
- return "呵呵abc";
- }
- public void add2() {
- System.out.println("=========3=======");
- }
- }
- package cn.happy.spring13;
- import org.aopalliance.intercept.MethodInterceptor;
- import org.aopalliance.intercept.MethodInvocation;
- /**
- * Created by lihuohuo on 2017/7/31.
- */
- public class MethodAdvice implements MethodInterceptor {
- public Object invoke(MethodInvocation methodInvocation) throws Throwable {
- System.out.println("之前============");
- Object result=methodInvocation.proceed();
- String temp=null;
- if(result!=null){
- temp=(String)result;
- temp=temp.toUpperCase();
- }
- System.out.println("之後================");
- System.out.println("======temp========="+temp);
- return temp;
- }
- }
- <?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.spring13.IDoSomes"></bean>
- <bean id="methodAdvice" class="cn.happy.spring13.MethodAdvice"></bean>
- <bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="target" ref="Idosome"></property>
- <property name="interceptorNames" value="methodAdvice"></property>
- </bean>
- </beans>
- @Test
- public void test03(){
- ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext09.xml");
- cn.happy.spring13.IDoSome i=(cn.happy.spring13.IDoSome)ctx.getBean("someProxy");
- i.add();
- String id=i.add1();
- System.out.println(id);
- i.add2();
- }
最後一個是異常增強:實現接口是:ThrowsAdvice
- package cn.happy.spring14;
- /**
- * Created by lihuohuo on 2017/7/31.
- */
- public interface IDoSome {
- public void add() throws UserException;
- public String add1();
- public void add2();
- }
- package cn.happy.spring14;
- /**
- * Created by lihuohuo on 2017/7/31.
- */
- public class IDoSomes implements IDoSome {
- public void add() throws UserException {
- System.out.println("=========1=======");
- throw new UserException("總算有錯了");
- }
- public String add1() {
- System.out.println("=========2=======");
- return "呵呵abc";
- }
- public void add2() {
- System.out.println("=========3=======");
- }
- }
- package cn.happy.spring14;
- import org.springframework.aop.ThrowsAdvice;
- /**
- * Created by lihuohuo on 2017/7/31.
- */
- public class MyThrowsAdvice implements ThrowsAdvice{
- public void afterThrowing(UserException ex){
- System.out.println("錯誤提示:"+ex.getMessage());
- }
- }
- package cn.happy.spring14;
- /**
- * Created by lihuohuo on 2017/7/31.
- */
- public class UserException extends Exception{
- public UserException() {
- super();
- }
- public UserException(String message) {
- super(message);
- }
- }
- <?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.spring14.IDoSomes"></bean>
- <bean id="throwsAdvice" class="cn.happy.spring14.MyThrowsAdvice"></bean>
- <bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="target" ref="Idosome"></property>
- <property name="interceptorNames" value="throwsAdvice"></property>
- </bean>
- </beans>
- @Test
- public void test04(){
- ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext10.xml");
- cn.happy.spring14.IDoSome i=(cn.happy.spring14.IDoSome)ctx.getBean("someProxy");
- try {
- i.add();
- } catch (UserException e) {
- e.printStackTrace();
- }
- }
還有2種配置文件改變 方法的前置增強
- package cn.happy.spring16;
- /**
- * Created by lihuohuo on 2017/7/31.
- */
- public interface IDoSome {
- public void adTd();
- public void adTd1();
- public void add2();
- }
- package cn.happy.spring16;
- /**
- * Created by lihuohuo on 2017/7/31.
- */
- public class IDoSomes implements IDoSome {
- public void adTd() {
- System.out.println("=========1=======");
- }
- public void adTd1() {
- System.out.println("=========2=======");
- }
- public void add2() {
- System.out.println("=========3=======");
- }
- }
- package cn.happy.spring16;
- 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=======");
- }
- }
第一種使用方法名字 name 對特定的方法進行增強:
- <?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.spring15.IDoSomes"></bean>
- <bean id="beforeAdvice" class="cn.happy.spring15.MyBeforeAdvice"></bean>
- <bean id="beforeAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
- <property name="advice" ref="beforeAdvice"></property>
- <property name="mappedNames" value="add"></property>
- </bean>
- <bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="target" ref="Idosome"></property>
- <property name="interceptorNames" value="beforeAdvisor"></property>
- </bean>
- </beans>
第二種 根據名的 .*T*.的這種方法
- <?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.spring16.IDoSomes"></bean>
- <bean id="beforeAdvice" class="cn.happy.spring16.MyBeforeAdvice"></bean>
- <bean id="beforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
- <property name="advice" ref="beforeAdvice"></property>
- <property name="patterns" value=".*T.*"></property>
- </bean>
- <bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="target" ref="Idosome"></property>
- <property name="interceptorNames" value="beforeAdvisor"></property>
- <property name="proxyTargetClass" value="true"></property>
- </bean>
- </beans>
下面是測試類 ,都是一樣的,。
- @Test
- public void test06(){
- ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext12.xml");
- cn.happy.spring16.IDoSome i=(cn.happy.spring16.IDoSome)ctx.getBean("someProxy");
- i.add2();
- i.adTd();
- i.adTd1();
- }