Detailed introduction and simple examples of Spring Automatic proxy creators, spring details

Source: Internet
Author: User

Detailed introduction and simple examples of Spring Automatic proxy creators, spring details

Spring Automatic proxy Builder

Preface:

In classic spring Aop, You can manually create a proxy Bean for the target Bean. The configuration file must declare a proxy for each Bean to be enhanced. A large number of proxy beans are declared in the result configuration file.

In the classic Spring Aop, Spring provides the Aotu proxy creator. With the automatic proxy creator, you no longer need to use ProxyFactoryBean to manually create a proxy.

Interface Animal and Book:


 package com.zzj.aop; public interface Animal { public void eat(); public void drink(); }
package com.zzj.aop;  public interface Book {  public void read(); } 

Target class:

package com.zzj.aop;  public class Human implements Animal, Book{  @Override  public void eat() {   System.out.println("eat...");  }   @Override  public void drink() {   System.out.println("drink...");  }   @Override  public void read() {   System.out.println("read...");  } } 

Pre-notification and post-notification:

package com.zzj.aop;  import java.lang.reflect.Method;  import org.springframework.aop.MethodBeforeAdvice;  public class MethodBefore implements MethodBeforeAdvice {   public void before(Method arg0, Object[] arg1, Object arg2)    throws Throwable {   System.out.println("before " + arg0.getName());  }  } 
package com.zzj.aop;  import java.lang.reflect.Method;  import org.springframework.aop.AfterReturningAdvice;  public class MethodAfter implements AfterReturningAdvice {   public void afterReturning(Object arg0, Method arg1, Object[] arg2,    Object arg3) throws Throwable {   System.out.println( "after " + arg1.getName());  }  } 

Spring configuration file:

<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns =" http://www.springframework.org/schema/beans "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 "> <! -- Define the target object --> <bean id = "human" class = "com. zzj. aop. Human"> </bean> <! -- Define notification --> <bean id = "beforeAdvice" class = "com. zzj. aop. methodBefore "> </bean> <bean id =" afterAdvice "class =" com. zzj. aop. methodAfter "> </bean> <! -- Define the start point --> <bean id = "methodNamePointcut" class = "org. springframework. aop. support. nameMatchMethodPointcut "> <property name =" mappedNames "> <list> <value> eat </value> <value> read </value> </list> </property> </ bean> <! -- Define the post-enhancement device (associated notification and entry point) --> <bean id = "AfterMethodNameAdvisor" class = "org. springframework. aop. support. defaultPointcutAdvisor "> <property name =" advice "ref =" afterAdvice "> </property> <property name =" pointcut "ref =" methodNamePointcut "> </property> </bean> <! -- Define the pre-enhancement device (associated notification and entry point) --> <bean id = "BeforeMethodNameAdvisor" class = "org. springframework. aop. aspectj. aspectJExpressionPointcutAdvisor "> <property name =" advice "ref =" beforeAdvice "> </property> <property name =" expression "> <value> execution (**. * in *(..)) </value> <! -- Match drink --> </property> </bean> <! -- Define the automatic proxy maker --> <bean class = "org. springframework. aop. framework. autoproxy. beanNameAutoProxyCreator "> <property name =" beanNames "> <list> <value> * human </value> </list> </property> <property name =" interceptorNames "> <list> <value> AfterMethodNameAdvisor </value> <value> BeforeMethodNameAdvisor </value> </list> </property> </bean> </beans>

The above automatic proxy can create a proxy for the Bean ending with human.

Test:

package com.zzj.aop;  import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;  public class Test {   /**   * @param args   */  public static void main(String[] args) {   ApplicationContext context = new ClassPathXmlApplicationContext(     "applicationContext.xml");   Animal animal = (Animal) context.getBean("human");   Book book = (Book) animal;   animal.eat();   animal.drink();   book.read();   }  } 

Output:

eat... after eat before drink drink... read... after read 

Spring also provides another automatic proxy Creator: DefaultAdvisorAutoProxyCreator. This automatic proxy generator does not require any configuration. It will automatically check every booster and Bean declared in the Ioc container. If there is a Bean that matches the booster entry point, DefaultAdvisorAutoProxyCreator will automatically create a proxy for it.

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/> 

It should be noted that DefaultAdvisorAutoProxyCreator may proxy the target beans that do not want to be proxies, so be careful when using them.

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.