AOP
: Cross-section programming.
, Connection point
{Pre-notification, surround notification, and post-Notification} in this example, the surround notification is used.
(Specify the method that requires proxy ).
Spring 1.2 is used.
Take person as an example:
Iperson interface (which can be understood as a business interface ):
Public
Interface
Iperson {
Public void sleep ();
Public void walk ();
}
Iperson implementation class (which can be understood as a business implementation class ):
Public
Class
PersonimpImplements
Iperson {
Public
Void sleep (){
System. Out
. Println ("sleep..."); // specific business operation, such as inserting data into a database
}
Public
Void walk (){
System. Out
. Println ("walk..."); // specific business operation, such as inserting data into a database
}
}
Define the connection point (
Pre-notification | surround notification
|
Post-Notification} surround notification is used here
If you use a pre-notification, or a post-notification, it is easier ):
Public
Class
AroundnoteImplements
Methodinterceptor {
Public
Object invoke (methodinvocation arg0)Throws
Throwable {
System. Out
. Println ("befor"); // For example, the Service Code, such as opening a transaction
Object o = arg0.proceed ();
System. Out
. Println ("after"); // For example, service code, such as transaction commit and rollback
Return
O;
}
}
Test class:
Public
Class
Proxytest {
Public
Static
Void main (string [] ARGs ){
Applicationcontext AC =New
Classpathxmlapplicationcontext ("applicationcontext. xml"); // enable the spring container and load the configuration
Iperson person = (iperson) AC. getbean ("personimp"); // obtain the target object (business class)
Person. Sleep ();
Person. Walk ();
}
}
/* Above analysis: When sleep is executed, run system. Out in the connection point first.
. Println ("befor ");
Run this code, then run sleep and finally execute system. Out.
. Println ("after ");
If hibernate operates the database in sleep, then system. Out
. Println ("befor ");
This code is changed to enable the transaction, system. Out
. Println ("after ");
This code is changed to the commit of the transaction, so all the transactions can be completed in this way, and the transactions and services are separated. */
Output result:
Befor // if the transaction is enabled
Sleep... // operation on the database
After // if the transaction is committed or rolled back
Befor
Walk...
After
Spring Configuration:
<? 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-2.5.xsd
">
<! -- Target object (business class) -->
<Bean
ID
= "Personimp
"Class
= "Org. LC. autoproxy. personimp
"> </Bean>
<! -- Surround notification AOP {jointpoint (connection point)} -->
<Bean
ID
= "Aroundnote
"Class
= "Org. LC. autoproxy. aroundnote
"> </Bean>
<Bean
Class
= "Org. springframework. AOP. Framework. autoproxy. beannameautoproxycreato
R
">
<! -- If the name is the setbeannames method in beannameautoproxycreator, the following is the same -->
<Property
Name
= "Beannames
">
<Value>
Personimp </value>
</Property>
<! -- Specify the connection point -->
<Property
Name
= "Interceptornames
">
<List>
<Value>
Aroundnote </value>
</List>
</Property>
</Bean>
</Beans>