权限管理实现方法有很多种,用用aop,顺便复习下spring了^_^
普通代理的实现:
Buy.java
package com.proxyExample;
public interface Buy {
public void buyThing(String message);
}
BuyImpl.java
package com.proxyExample;
public class BuyImpl implements Buy {
public void buyThing(String message) {
System.out.println("我要买东西了^_^"+message);
}
}
BuyProxy.java
package com.proxyExample;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class BuyProxy implements InvocationHandler {
private Object obj;
public BuyProxy(Object obj) {
this.obj = obj;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("开始调用被代理的类的方法、嘎嘎"+method);
method.invoke(obj, args);
System.out.println("结束调用被代理的类的方法、嘎嘎"+method);
return null;
}
}
Test.java
package com.proxyExample;
import java.lang.reflect.Proxy;
public class Test {
public static void main(String args[]) {
Buy bb = new BuyImpl();
BuyProxy pro = new BuyProxy(bb);
Buy go = (Buy) Proxy.newProxyInstance(bb.getClass().getClassLoader(),
bb.getClass().getInterfaces(), pro);
go.buyThing("买烤鸡,烤鸭,红烧牛排");
}
}
AOP代理的实现:
Student.java
package com.aop;
public interface Student {
public void addStudent(String name);
}
StudentImpl.java
package com.aop;
public class StudentImpl implements Student {
public void addStudent(String name) {
System.out.println("欢迎"+name+"来到spring");
}
}
BeforeAdvice.java
package com.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class BeforeAdvice implements MethodBeforeAdvice {
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.println(" 这是BeforeAdvice类的before方法. ");
}
}
AfterAdvice.java
package com.aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class AfterAdvice implements AfterReturningAdvice {
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
System.out.println("这是AfterAdvice类的afterReturning方法.");
}
}
CompareInterceptor.java
package com.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class CompareInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation arg0) throws Throwable {
Object result = null;
String stu_name = arg0.getArguments()[0].toString();
if (stu_name.equals("")||stu_name==null) {
result = arg0.proceed();
} else {
System.out.println("此学生是" + stu_name);
}
return result;
}
}
Test.java
package com.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext("/src/applicationContext.xml");
Student s = (Student) ctx.getBean("student");
s.addStudent("kava");
}
}
ApplicationContext.xml
<?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.0.xsd">
<bean id="beforeAdvice" class="com.aop.BeforeAdvice"></bean>
<bean id="afterAdvice" class="com.aop.AfterAdvice"></bean>
<bean id="compareInterceptor" class="com.aop.CompareInterceptor"></bean>
<bean id="studenttarget" class="com.aop.StudentImpl"></bean>
<bean id="student"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.aop.Student</value>
</property>
<property name="interceptorNames">
<list>
<value>beforeAdvice</value>
<value>afterAdvice</value>
<value>compareInterceptor</value>
</list>
</property>
<property name="target">
<ref bean="studenttarget" />
</property>
</bean>
</beans>
对CompareInterceptor.java进行处理可以增加控制逻辑,实现权限控制
package com.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class CompareInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation arg0) throws Throwable {
Object result = null;
String stu_name = arg0.getArguments()[0].toString();
if (stu_name.equals("")||stu_name==null) {
return mapping.findForward("login");
} else {
System.out.println("此学生是" + stu_name);
return result = arg0.proceed();
}
}
}
可参考例子:
http://lighter.javaeye.com/blog/42673
http://kukuqiu.javaeye.com/blog/163199
![](https://i-blog.csdnimg.cn/blog_migrate/d86d1f994902d874120d7f9d14cefac0.jpeg)
普通代理的实现:
Buy.java
package com.proxyExample;
public interface Buy {
public void buyThing(String message);
}
BuyImpl.java
package com.proxyExample;
public class BuyImpl implements Buy {
public void buyThing(String message) {
System.out.println("我要买东西了^_^"+message);
}
}
BuyProxy.java
package com.proxyExample;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class BuyProxy implements InvocationHandler {
private Object obj;
public BuyProxy(Object obj) {
this.obj = obj;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("开始调用被代理的类的方法、嘎嘎"+method);
method.invoke(obj, args);
System.out.println("结束调用被代理的类的方法、嘎嘎"+method);
return null;
}
}
Test.java
package com.proxyExample;
import java.lang.reflect.Proxy;
public class Test {
public static void main(String args[]) {
Buy bb = new BuyImpl();
BuyProxy pro = new BuyProxy(bb);
Buy go = (Buy) Proxy.newProxyInstance(bb.getClass().getClassLoader(),
bb.getClass().getInterfaces(), pro);
go.buyThing("买烤鸡,烤鸭,红烧牛排");
}
}
AOP代理的实现:
Student.java
package com.aop;
public interface Student {
public void addStudent(String name);
}
StudentImpl.java
package com.aop;
public class StudentImpl implements Student {
public void addStudent(String name) {
System.out.println("欢迎"+name+"来到spring");
}
}
BeforeAdvice.java
package com.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class BeforeAdvice implements MethodBeforeAdvice {
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.println(" 这是BeforeAdvice类的before方法. ");
}
}
AfterAdvice.java
package com.aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class AfterAdvice implements AfterReturningAdvice {
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
System.out.println("这是AfterAdvice类的afterReturning方法.");
}
}
CompareInterceptor.java
package com.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class CompareInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation arg0) throws Throwable {
Object result = null;
String stu_name = arg0.getArguments()[0].toString();
if (stu_name.equals("")||stu_name==null) {
result = arg0.proceed();
} else {
System.out.println("此学生是" + stu_name);
}
return result;
}
}
Test.java
package com.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext("/src/applicationContext.xml");
Student s = (Student) ctx.getBean("student");
s.addStudent("kava");
}
}
ApplicationContext.xml
<?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.0.xsd">
<bean id="beforeAdvice" class="com.aop.BeforeAdvice"></bean>
<bean id="afterAdvice" class="com.aop.AfterAdvice"></bean>
<bean id="compareInterceptor" class="com.aop.CompareInterceptor"></bean>
<bean id="studenttarget" class="com.aop.StudentImpl"></bean>
<bean id="student"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.aop.Student</value>
</property>
<property name="interceptorNames">
<list>
<value>beforeAdvice</value>
<value>afterAdvice</value>
<value>compareInterceptor</value>
</list>
</property>
<property name="target">
<ref bean="studenttarget" />
</property>
</bean>
</beans>
对CompareInterceptor.java进行处理可以增加控制逻辑,实现权限控制
package com.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class CompareInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation arg0) throws Throwable {
Object result = null;
String stu_name = arg0.getArguments()[0].toString();
if (stu_name.equals("")||stu_name==null) {
return mapping.findForward("login");
} else {
System.out.println("此学生是" + stu_name);
return result = arg0.proceed();
}
}
}
可参考例子:
http://lighter.javaeye.com/blog/42673
http://kukuqiu.javaeye.com/blog/163199