In web development, user access permission check is an important part. Taking strust as an example, we need to write the relevant code in the excute method of action (generally calling the base class function). Obviously, this is a type of repetitive work in each action.
If we can automatically call the permission check function of the base class before excute runs, this is a good solution. AOP provides us with such a solution.
The following describes the implementation method with a simplified instance.
First, we make an interface:
Public interface checkinterface {
Public abstract void check (string name );
Public abstract void excute (string name );
}
Now, let's make a notification class (advice ):
Import org. springframework. AOP. methodbeforeadvice;
Import java. Lang. Reflect. method;
Import org. Apache. log4j. Logger;
Public class beforeadvisor implements methodbeforeadvice {
Private Static logger = logger. getlogger (beforeadvisor. Class );
Public void before (method M, object [] ARGs, object target) throws throwable {
If (target instanceof checkinterface ){
Logger. debug ("is instanceof checkinterface !!! ");
Checkinterface CI = (checkinterface) target;
CI. Check (string) ARGs [0]);
}
}
}
Among them, the important before method parameters are: Object of the notification passed in by the object target (that is, the interface of the test class). method M and object [] ARGs are the methods and parameters called by the object respectively. Let's create a spring bean definition XML file:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public "-// spring // DTD bean // en" "http://www.springframework.org/dtd/spring-beans.dtd">
<Beans>
<Description> spring Quick Start </description>
<Bean id = "myadvisor" class = "com. wysm. netstar. Test. springaop. beforeadvisor"/>
<Bean id = "mypointcutadvisor2" class = "org. springframework. AOP. Support. regexpmethodpointcutadvisor">
<Property name = "advice">
<Ref local = "myadvisor"/>
</Property>
<Property name = "patterns">
<List>
<Value>. * excute. * </value>
</List>
</Property>
</Bean>
<Bean id = "checkinterface" class = "com. wysm. netstar. Test. springaop. excuteclass"/>
<Bean id = "mycheckclass" class = "org. springframework. AOP. Framework. proxyfactorybean">
<Property name = "proxyinterfaces">
<Value> com. wysm. netstar. Test. springaop. checkinterface </value>
</Property>
<Property name = "target">
<Ref local = "checkinterface"/>
</Property>
<Property name = "interceptornames">
<Value> mypointcutadvisor2 </value>
</Property>
</Bean>
</Beans>
This definition file specifies that excuteclass is a monitoring object. When its excute method is executed, beforeadvisor will be called.
The last is the test class:
Import JUnit. Framework. testcase;
Import org. springframework. Context. applicationcontext;
Import org. springframework. Context. Support. filesystemxmlapplicationcontext;
Public class springtestcase2 extends testcase {
Checkinterface test = NULL;
Protected void setup () throws exception {
Super. Setup ();
Applicationcontext CTX = new filesystemxmlapplicationcontext ("src/COM/wysm/netstar/test/springaop/aoptest. xml ");
Test = (checkinterface) CTX. getbean ("mycheckclass ");
}
Protected void teardown () throws exception {
Super. teardown ();
}
Public void testexcute (){
Test. excute ("supervisor ");
}
}
Make another base class:
Public abstract class baseclass implements checkinterface {
Public baseclass (){
}
Public void check (string name ){
If (name. Equals ("supervisor "))
System. Out. println ("Check pass !! ");
Else {
System. Out. println ("no access privilege! Please do something. Else! ");
}
}
}
Perform another test class:
Public class excuteclass extends baseclass {
Public excuteclass (){
}
Public void excute (string name ){
System. Out. println ("excute here! "+ Name );
}
}