Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to manually create a proxy object ProxyFactory by Spring api

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article analyzes "how Spring api manually creates a proxy object ProxyFactory". The content is detailed and easy to understand, "Spring api how to manually create a proxy object ProxyFactory" interested friends can follow the editor's ideas slowly in-depth to read, I hope that after reading can be helpful to everyone. Let's follow the editor to learn more about "how Spring api manually creates a proxy object ProxyFactory".

You can customize the creation of proxy objects through annotations, and you can also create proxy objects by SpringAPI and manual programming.

Several important API:

ProxyFactory

MethodInterceptor

Advice

AfterReturningAdvice

MethodBeforeAdvice

Import java.lang.reflect.Method

Import org.aopalliance.intercept.Interceptor

Import org.aopalliance.intercept.MethodInterceptor

Import org.aopalliance.intercept.MethodInvocation

Import org.junit.Test

Import org.springframework.aop.AfterAdvice

Import org.springframework.aop.AfterReturningAdvice

Import org.springframework.aop.MethodBeforeAdvice

Import org.springframework.aop.framework.ProxyFactory

Import cn.hessian.service.HelloWorldService

Import cn.hessian.service.impl.HelloWorldServiceImpl2

/ * *

* @ author beijing

* 2013-4-2

, /

Public class SpringProgramicProxyDemo {

@ Test

Public void test () {

/ / the implemented interface required by the proxy object

Class [] interfaces=new Class [] {HelloWorldService.class}

/ / create an agent factory using the API of spring

ProxyFactory proxyFactory=new ProxyFactory (interfaces)

/ / set the target object

ProxyFactory.setTarget (new HelloWorldServiceImpl ())

/ * *

* Set whether proxies created by this configuration should be prevented from being cast to Advised to query proxy status.

Default is "false", meaning that any AOP proxy can be cast to Advised.

* * /

ProxyFactory.setOpaque (true)

/ / add method advance notification

ProxyFactory.addAdvice (new MethodBeforeAdvice () {

@ Override

Public void before (Method method, Object [] args, Object target)

Throws Throwable {

System.out.println ("1MMI-intercept before method invocation")

}

});

/ / multiple method pre-or post-notifications can be added

ProxyFactory.addAdvice (new MethodBeforeAdvice () {

@ Override

Public void before (Method method, Object [] args, Object target)

Throws Throwable {

System.out.println ("2murmuri-intercept before a method call")

}

});

/ / multiple method pre-or post-notifications can be added

ProxyFactory.addAdvice (new AfterReturningAdvice () {

@ Override

Public void afterReturning (Object returnValue, Method method

Object [] args, Object target) throws Throwable {

System.out.println ("method called after method completion-1")

}

});

/ / multiple method pre-or post-notifications can be added

ProxyFactory.addAdvice (new AfterReturningAdvice () {

@ Override

Public void afterReturning (Object returnValue, Method method

Object [] args, Object target) throws Throwable {

System.out.println ("method called after method completion-2")

}

});

/ / for surround notifications, only one can be added, and it is useless to add more. Spring will select the first advice. Please see the result.

ProxyFactory.addAdvice (new MethodInterceptor () {

@ Override

Public Object invoke (MethodInvocation invocation) throws Throwable {

System.out.println ("1murmuri-surround notice")

Object [] params=invocation.getArguments ()

Method method=invocation.getMethod ()

Object target=invocation.getThis ()

Object bytes=method.invoke (target, params)

Byte [] result= (byte []) bytes

System.out.println ("1murmuri-the result generated around the notification:" + new String (result))

Return Beijing Life Circle. GetBytes ()

}

});

/ / for surround notifications, only one can be added, and it is useless to add more. Spring will select the first advice. Please see the result.

ProxyFactory.addAdvice (new MethodInterceptor () {

@ Override

Public Object invoke (MethodInvocation invocation) throws Throwable {

System.out.println ("2murmuri-surround notice")

Object [] params=invocation.getArguments ()

Method method=invocation.getMethod ()

Object target=invocation.getThis ()

Object bytes=method.invoke (target, params)

Byte [] result= (byte []) bytes

System.out.println ("2murmuri-the result generated around the notification:" + new String (result))

Return bytes

}

});

Object proxy=proxyFactory.getProxy (proxyFactory.getClass () .getClassLoader ())

Class [] inters=proxy.getClass () .getInterfaces ()

For (Class str: inters) {

System.out.println (str.getSimpleName ())

}

HelloWorldService helloWorldService= (HelloWorldService) proxy

System.out.println (new String (helloWorldService.sayHelloWorld (Beijing)

}

}

On how Spring api manually create proxy object ProxyFactory to share here, I hope that the above content can make you improve. If you want to learn more knowledge, please pay more attention to the editor's updates. Thank you for following the website!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report