一、
package com.spring.aop.test.aspectj.test_2;
import java.util.Date;
/**
* 管理员
* @author Administrator
*
*/
public class Operator {
private Integer id ;
private String name ;
private String password ;
private Integer popedom ;
private Date loginTime ;
..set()/get()....
}
二、
package com.spring.aop.test.aspectj.test_2;
import java.util.Date;
/**
* 新闻类别信息
* @author Administrator
*
*/
public class NewsType {
private Integer id ;
private String typeName ;
private String desc ;
private Operator operator ;
private Date addTime ;
public NewsType(){}
...set()/get()...
}
三、
package com.spring.aop.test.aspectj.test_2;
import java.util.List;
public interface BasicDao<T> {
boolean save(T t) ;
boolean delete(T t) ;
boolean update(T t) ;
T get(Integer id) ;
List<T> get() ;
}
四、
package com.spring.aop.test.aspectj.test_2;
import java.util.List;
public class NewsTypeDaoImpl implements BasicDao<NewsType> {
public boolean delete(NewsType t) {
System.out.println("=======删除NewsType对象=======");
return true;
}
public NewsType get(Integer id) {
System.out.println("=======获取一个NewsType对象=======");
return new NewsType();
}
public List<NewsType> get() {
System.out.println("======获取一个NewsType对象列表======");
return null;
}
public boolean save(NewsType t) {
System.out.println("======保存NewsType对象======");
return true;
}
public boolean update(NewsType t) {
System.out.println("======更新NewsType对象======");
return true;
}
}
五、
package com.spring.aop.test.aspectj.test_2;
import java.io.*;
import java.util.Date;
public aspect NewsTypeAspect {
private static java.text.SimpleDateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd H:m:s");
//定义操作时间切入点
pointcut operatorTime(): execution(* *.*());
//定义保存切入点
pointcut saveNewsType(NewsTypeDaoImpl dao): target(dao)&&execution(boolean NewsTypeDaoImpl.save(NewsType));
//定义删除切入点
pointcut deleteNewsType(NewsTypeDaoImpl dao): target(dao)&&execution(boolean NewsTypeDaoImpl.delete(NewsType));
//输出操作开始时间
before(): operatorTime(){
System.out.println("用户操作开始时间:"+df.format(new Date()));
}
//保存时,判断用户是否拥有保存权限
boolean around(NewsTypeDaoImpl dao): saveNewsType(dao){
NewsType type = (NewsType)thisJoinPoint.getArgs()[0];
if(type.getOperator().getPopedom() > 0){
System.out.println("====开始保存NewsType对象====");
System.out.println("保存时间:"+df.format(new Date()));
return proceed(dao);
}else{
System.out.println("你暂时还没有保存NewsType对象的权利");
System.out.println("你的权限为:"+type.getOperator().getPopedom());
return false;
}
}
//删除时,要提示用户是否真的要删除此对象
boolean around(NewsTypeDaoImpl dao): deleteNewsType(dao){
boolean flag = false;
try{
NewsType type = (NewsType)thisJoinPoint.getArgs()[0];
if(type.getOperator().getPopedom() > 0){
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
System.out.println("开始删除操作......");
System.out.println("Enter a operator<Y/N>:");
String info = stdin.readLine();
if(info.indexOf("Y") != -1){
System.out.println("可以删除......");
flag = proceed(dao);
if(flag){
System.out.println("=======删除Newstype对象成功=======");
}else{
System.out.println("=======删除NewsType对象失败=======");
}
}else{
System.out.println("数据未被删除......");
}
}else{
System.out.println("你没有删除NewsType对象的权限......");
}
}catch(Exception e){
System.out.println(e.getMessage());
}
return flag;
}
}
六、
package com.spring.aop.test.aspectj.test_2;
public class NewsTypeDaoTest {
public static void main(String[] arg){
Operator op = new Operator();
op.setId(1);
op.setName("admin");
op.setPassword("password");
op.setPopedom(1);
NewsType type = new NewsType();
type.setId(1);
type.setOperator(op);
type.setTypeName("新闻");
NewsTypeDaoImpl dao = new NewsTypeDaoImpl();
// if(dao.save(type)){
//
// System.out.println("save ok......");
// }else{
//
// System.out.println("save failed......");
// }
if(dao.delete(type)){
System.out.println("delete NewsType succ......");
}else{
System.out.println("delete NewsType failed......");
}
}
}