天天看點

關于AOP的@Aspect代理

@Aspect 代理:

             package com.ml.test;

public interface Manager            //接口

{

public void addUser(String name, String password);

public void delUser(int id);

public void modifyUser(int id, String name, String password);

}

package com.ml.test;

public class ManagerImpl implements Manager      //接口實作類

{

public void addUser(String name, String password)

{

System.out.println("UserManagerImpl.addUser() -- name: "); 

}

public void delUser(int id)

{

System.out.println("UserManagerImpl.delUser() -- id: ");  

}

public void modifyUser(int id, String name, String password)

{

System.out.println("UserManagerImpl.modifyUser() -- id: ");

}

}

package com.ml.test;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.After;

@Aspect

public class MySecurityManagerImpl                                            //安全檢查類,Aspect

{

@After("execution(* com.ml.test.*.*(..))")

public void checkSecurity()

{

System.out.println("checkSecurity the program");

}

}

public class Client

{

public static void main(String[] args)

{

Manager managerimpl=new ManagerImpl();

AspectJProxyFactory factory = new AspectJProxyFactory();

factory.setTarget(managerimpl);   //① 設定目标對象

factory.addAspect(MySecurityManagerImpl.class);  //②添加切面類

ManagerImpl proxy = factory.getProxy(); //③ 生成織入切面的代理對象

proxy.addUser("123", "345");

proxy.delUser(12);

proxy.modifyUser(123, "233", "133");*/

}

}