28

AOP编程

Spring278 views

假设有Service方法类似UserService

package com.hjide.aop;
 
public interface UserService {
	public User getUser(int id) throws Exception;
	public void save(User u) throws Exception;
	public int delete(User u) throws Exception;
}

实现类似如下

package com.hjide.aop;
 
public class UserServiceImpl implements UserService {
 
	public int delete(User u)  throws Exception{
		System.out.println("delete");
		return 0;
	}
 
	public User getUser(int id) throws Exception {
		User u = new User();
		u.setId(1);
		u.setName("new user");
		u.setType(2);
		System.out.println("getUser");
		return u;
	}
 
	public void save(User u) throws Exception {
		System.out.println("save");
	}
 
}


User代码如下:

package com.hjide.aop;
 
public class User {
	private int id;
	private String name;
	private int type;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public String toString(){
		StringBuilder sb = new StringBuilder();
		sb.append("id=");
		sb.append(id);
		sb.append(",");
		sb.append("name=");
		sb.append(name);
		sb.append(",");
		sb.append("type=");
		sb.append(type);
		sb.append(",");
		return sb.toString();
	}
}

利用aop加入权限检查和操作日志
权限检查

package com.hjide.aop;
 
import org.aspectj.lang.JoinPoint;
 
public class Permission  {
 
	public void permissionCheck(JoinPoint jp)
			throws AccessControlException {
		System.out.print("Permission Check.");
 
		Object[] objs = jp.getArgs();
		for(Object o:objs){
			if(o instanceof User){
				User u = (User)o;
				if(u.getType()==3)
					throw new AccessControlException("Permission denied.");
			}
		}
		System.out.println("OK");
 
	}
 
}

日志

package com.hjide.aop;
 
import org.aspectj.lang.JoinPoint;
 
public class Log{
 
	public void writelog(JoinPoint jp) throws Throwable {
		System.out.print("write log:");
 
		Object[] objs = jp.getArgs();
		for(Object o:objs){
			System.out.println("value="+o);
		}
 
	}
 
}

权限检查异常

package com.hjide.aop;
 
public class AccessControlException extends Exception {
 
	public AccessControlException() {
		super();
		// TODO Auto-generated constructor stub
	}
 
	public AccessControlException(String arg0, Throwable arg1) {
		super(arg0, arg1);
		// TODO Auto-generated constructor stub
	}
 
	public AccessControlException(String arg0) {
		super(arg0);
		// TODO Auto-generated constructor stub
	}
 
	public AccessControlException(Throwable arg0) {
		super(arg0);
		// TODO Auto-generated constructor stub
	}
 
}

配置spring的Context文件,使aop工作

 

<?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:aop=”http://www.springframework.org/schema/aop”
xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd”>

<bean name=”UserService” class=”com.hjide.aop.UserServiceImpl”>
</bean>
<bean name=”permissionCheck” class=”com.hjide.aop.Permission”></bean>
<bean name=”Log” class=”com.hjide.aop.Log”></bean>
<aop:config>
<aop:aspect id=”securityAspect” ref=”permissionCheck”>
<aop:pointcut id=”AccessControlMethod”
expression=”execution(* com.hjide.aop.*Service.save*(..)) or execution(* com.hjide.aop.*Service.delete*(..))” />
<aop:before method=”permissionCheck” pointcut-ref=”AccessControlMethod” />
</aop:aspect>
<aop:aspect id=”logAspect” ref=”Log”>
<aop:pointcut id=”logMethod”
expression=”execution(* com.hjide.aop.*Service.*(..))” />
<aop:after method=”writelog” pointcut-ref=”logMethod” />
</aop:aspect>
</aop:config>

</beans>

运行测试代码

package com.hjide.aop;
 
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Test {
	public static void main(String[] args){
		BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserService us = (UserService)factory.getBean("UserService");
		User u;
		try {
			u = us.getUser(1);
			System.out.println("======================================");
			u.setId(3);
			u.setType(2);
			try {
				us.save(u);
			} catch (Exception e1) {
				System.err.println(e1.getMessage());
			}
			System.out.println("======================================");
			u.setType(3);
			try {
				us.delete(u);
			} catch (Exception e) {
				System.err.println(e.getMessage());
			}
			System.out.println("======================================");
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
 
	}
}

运行结果
getUser
write log:value=1
======================================
Permission Check.OK
save
write log:value=id=3,name=new user,type=2,
======================================
Permission Check.
Permission denied.
======================================


Tags: ,

作者:Jock

Leave a Reply

You must be logged in to post a comment.

Switch to our mobile site