Web
In web development, user access to the Web page is an important part of the inspection. In the case of strust, we need to write the relevant code in the action's Excute method (usually the function that calls the base class), and it is clear that this is a repetitive labor in each action.
This is a good solution if we can automatically invoke the permission check function of the base class before we run the Excute. AOP provides us with such a workaround.
The following is a simplified example of how to implement this approach.
First we make an interface:
Public interface Checkinterface {
public abstract void Check (String name);
public abstract void Excute (String name);
}
Make a base class again:
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 sth. Else! ");
}
}
}
To do a test class again:
public class Excuteclass extends BaseClass {
Public Excuteclass () {
}
public void Excute (String name) {
System.out.println ("Excute here!") +name);
}
}
OK, let's do 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=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]);
}
}
}
One of the important before method parameters: Object (that is, the interface of the test Class), the method M, object[] args is the object being invoked, respectively. We'll do the 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 indicates that the Excuteclass is a monitoring object and that the Excute method is executed when the beforeadvisor is invoked.
Finally, 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");
}
}