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 a 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");
}
}
Come and create an Advice, support Around, Before, Inster Returning, and THROWS in Spring, here is as simple as Before Advice:
Public Class Printbeforeadvice Implements MethodbeforeAdvice {
Public void before (Method Arg0, Object [] arg1, object arg2) throws throwable {
System.out.println ("in printbeforeadvice);
}
}
With your own business interface and advice, the rest is how to assemble them, first use
ProxyFactory is implemented in programming, as follows:
Public class aoptestmain {
Public static void main (String [] args) {
FooImpl fooImpl = new fooIMPL ();
PrintBeforeadvice myadvice = new printbeforeadvice ();
ProxyFactory Factory = New ProxyFactory (FooImpl);
Factory.addbeforeadvice (MyAdvice);
FooInterface myinterface = (foointerface) Factory.getProxy ();
MyInterface.printfoo ();
Myinterface.dummyfoo ();
}
}
Now executing the program, the magical result appears:
In PrintBeforeAdvice
In FooImpl.Printfoo
In PrintBeforeAdvice
In FooImpl.dummyfoo
Although this can experience the use of AOP in Spring, it is not a way to be recommended. Since Spring uses Spring, the Bean required to assemble in ApplicationContext is the best policy. Implement the above features only need to write a simple ApplicationContext. Yes, as follows:
"http://www.springframework.org/dtd/spring-beans.dtd">the aop application context
FooInterface
Myadvice
Of course, the code in main should also make corresponding modifications:
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 result of the above operation, 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 it. What methods require Advice, and the ApplicationContext is as follows:
"http://www.springframework.org/dtd/spring-beans.dtd">
The Springeva Application CONTEXT
Class = "org.springframework.aop.support.RegexpMethodpointcutadvisor">
* Print. *
FooInterface
MyAdvisor
The main program does not need any modifications, and the results have changed:
In PrintBeforeAdvice
In FooImpl.Printfoo
In FooImpl.dummyfoo
At this point, it should have understood the method of use of AOP in Spring. Of course, the most important application in Spring is Transaction Manager, which will take a look at the ApplicationContext example:
Class = "Org.springframework.beans.factory.config.propertyPlaceHolderConfigurer">
/Web-inf/jdbc.properties
Class = "Org.springframework.jdbc.datasource.driverManagerDataSource">
$ {jdbc.driverclassname}
$ {jdbc.url}
$ {jdbc.username}
$ {jdbc.password}
Class = "org.springframework.orm.hibernate.localsessionFactoryBean">
SmartMenu.hbm.xml
$ {hibernate.diaalect}
Class = "org.springframework.orm.hibernate.hibernateTransactionManager">
Class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> Propagation_Required, Readonly
Propagation_Required, Readonly
Well, if you want to completely understand the Spring's AOP, it is best to see the source code, open source is good!