日期:2014-05-18 浏览次数:20719 次
package cn.it.utils;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited //代表注解是可以被继承的。
public @interface UserPrivilegeInfo {
public String privilege(); // 它代表的是权限.
}
package cn.it.service;
import cn.it.domain.User;
import cn.it.utils.UserPrivilegeInfo;
public interface BookService {
@UserPrivilegeInfo(privilege = "修改图书")
public void update(User user) throws Exception;
@UserPrivilegeInfo(privilege = "查看图书")
public void find(User user) throws Exception;
@UserPrivilegeInfo(privilege = "删除图书")
public void delete(User user) throws Exception;
@UserPrivilegeInfo(privilege = "添加图书")
public void add(User user) throws Exception;
}
package cn.it.service;
import cn.it.dao.BookDao;
import cn.it.domain.User;
import cn.it.utils.UserPrivilegeInfo;
public class BookServiceImpl implements BookService {
public void delete(User user) {
// 1.得到这个方法上的注解.
// 2.得到当前操作这个方法的用户。
// 3.查询数据库,判断用户具有什么权限,与注解上的权限比较。
BookDao dao = new BookDao();
dao.delete();
}
public void update(User user) {
BookDao dao = new BookDao();
dao.update();
}
public void find(User user) {
BookDao dao = new BookDao();
dao.find();
}
public void add(User user) {
BookDao dao = new BookDao();
dao.add();
}
}
package cn.it.service.factory;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
import javax.persistence.Temporal;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.junit.Test;
import cn.it.domain.User;
import cn.it.service.BookService;
import cn.it.service.BookServiceImpl;
import cn.it.utils.DataSourceUtils;
import cn.it.utils.UserPrivilegeInfo;
public class BookServiceFactory {
private static BookServic