AOP in SpringFramework

zhaozj2021-02-16  59

AOP is a very important part of Spring's lightweight container. Get more and more attention, Spring Transaction is managed with AOP, today, through simple example, look at the basic use of AOP in Spring. .

First, determine the target to proxy, default Dynamic Proxy in the JDK in Spring, which only enables the agent of the interface, if you want to proxy the class, you need to use CGLIB's Proxy. Obviously, select "Programming to Interface" is a smarter practice, the following is the interface that will be proxy:

public interface FooInterface {public void printFoo (); public void dummyFoo ();}, and one of its simple implementation: public class FooImpl implements FooInterface {public void printFoo () {System.out.println ( "In FooImpl.printFoo"); } Public void dummyfoo () {system.out.println ("in fooImpl.dummyfoo");}} Next, create an Advice, support Around, Before, After Returning, and THROWS in Spring, here is simple before advice for example: public class PrintBeforeAdvice implements MethodBeforeAdvice {public void before (Method arg0, Object [] arg1, Object arg2) throws Throwable {System.out.println ( "in PrintBeforeAdvice");}} and have their own business interface advice the rest is how to assemble them, the first use ProxyFactory programmatically implemented as follows: public class AopTestMain {public static void main (String [] args) {FooImpl fooImpl = new FooImpl (); PrintBeforeAdvice myAdvice = new PrintBeforeAdvice ( ); ProxyFactory Factory = new proxyfactory (foodvice); fodyvice; foointerface myinterface = FooInterface) Factory.getProxy ();

MyInterface.printfoo (); Myinterface.dummyfoo ();}} Now executing the program, the magical result appears: in PrintFoo in PrintBeforeadvice in fooImpl.dummyfoo Although this can experience AOP in Spring, but this Never recommend the method, since the use of Spring, the Bean required by the installation in ApplicationContext is the best policy, implementing the above features only need to write a simple ApplicationContext, as follows: "Beans > The ao application context foointerface myadvice

Of course, main code should be modified accordingly: public static void main (String [] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext ( "applicationContext.xml"); FooInterface foo = (FooInterface) context.getBean ( "foo" ); Foo.printfoo (); foo.dummyfoo ();} Now run, the result will be exactly the same as the above operation results, is this more elegant? When you need to change the implementation, you can only modify the configuration file, the code in the program does not need any changes. However, at this time, you will find the Before in Advice when you are called by all methods in Proxy 's Object, which obviously cannot meet the needs of the vast majority. At this time, just borrow Advisor, of course In Advisor, use Pattern settings to set what methods require advice, and the applicationContext is as follows:

The springeva application context . * print. * foointerface < / Property>

/WEB-INF/jdbc.properties < / property> $ {jdbc.driverClassName} < / Property> $ {jdbc.url} $ {jdbc.username} < / Property> $ {jdbc.password} smartmenu.hbm.xml $ {hibernate.diaalect}

转载请注明原文地址:https://www.9cbs.com/read-17366.html

New Post(0)