TestServicespring
- package com.zbq.authorize;
- public interface TestService {
- public void view();
- public void modify();
- }
TestServiceImplapp
- package com.zbq.authorize;
- public class TestServiceImpl implements TestService {
- public void modify() {
- System.out.println("用戶修改數據");
- }
- public void view() {
- System.out.println("用戶查看數據");
- }
- }
TestActionide
- package com.zbq.authorize;
- public interface TestAction {
- public void view();
- public void modify();
- }
TestActionImplthis
- package com.zbq.authorize;
- public class TestActiomImpl implements TestAction {
- private TestService ts;
- public TestService getTs() {
- return ts;
- }
- public void setTs(TestService ts) {
- this.ts = ts;
- }
- public void modify() {
- ts.modify();
- }
- public void view() {
- ts.view();
- }
- }
AuthorityInterceptorspa
- package com.zbq.authorize;
- import org.aopalliance.intercept.MethodInterceptor;
- import org.aopalliance.intercept.MethodInvocation;
- public class AuthorityInterceptor implements MethodInterceptor {
- private String user;
- public String getUser() {
- return user;
- }
- public void setUser(String user) {
- this.user = user;
- }
- public Object invoke(MethodInvocation invocation) throws Throwable {
- System.out.println("-----------------");
- String methodName = invocation.getMethod().getName();
- if(!("admin".equals(user)) && !("registedUser".equals(user))){
- System.out.println("您無權執行該方法");
- return null;
- }else if(("registedUser".equals(user)) && ("modify".equals(methodName))){
- System.out.println("您不是管理員,沒法修改數據");
- return null;
- }else {
- return invocation.proceed();
- }
- }
- }
applicationContext.xmlxml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <bean id="serviceTarget" class="com.zbq.authorize.TestServiceImpl"/>
- <bean id="authorityInterceptor" class="com.zbq.authorize.AuthorityInterceptor">
- <property name="user">
- <value>registedUser</value>
- </property>
- </bean>
- <bean id="service" class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="proxyInterfaces">
- <value>com.zbq.authorize.TestService</value>
- </property>
- <property name="target">
- <ref local="serviceTarget"/>
- </property>
- <property name="interceptorNames">
- <list>
- <value>authorityInterceptor</value>
- </list>
- </property>
- </bean>
- <bean id="testAction" class="com.zbq.authorize.TestActiomImpl">
- <property name="ts">
- <ref local="service"/>
- </property>
- </bean>
- </beans>
Clientget
- package com.zbq.authorize;
- import org.springframework.beans.factory.xml.XmlBeanFactory;
- import org.springframework.core.io.ClassPathResource;
- public class Client {
- public static void main(String[] args) {
- ClassPathResource res = new ClassPathResource("applicationContext.xml");
- XmlBeanFactory factory = new XmlBeanFactory(res);
- TestAction ta = (TestAction) factory.getBean("testAction");
- ta.view();
- ta.modify();
- }
- }