Aop实例

package com.jushi.aop;
public interface IUser {
    public String findUser(String username);
    public void addUser(String username);
    public void findAll();
}
package com.jushi.aop;
import java.util.HashMap;
import java.util.Map;
public class IUserImpl implements IUser {
    public static Map map = null;
    public static void init(){
        String[] list = {"Lucy", "Tom", "小明", "Smith", "Hello"};
        Map tmp = new HashMap();
        for(int i=0; i<list.length; i++){
            tmp.put(list[i], list[i]+"00");
        }
        map = tmp;
    }
    public void addUser(String username) {
        init();
        map.put(username, username+"11");
        System.out.println("--------------【addUser】: "+username+" --------------");
        System.out.println("【The new List:"+map+"】");
    }
    public void findAll() {
        init();
        System.out.println("---------------【findAll】: "+map+" ------------------");
    }
    public String findUser(String username) {
        init();
        String password = "没查到该用户";
        if(map.containsKey(username)){
            password = map.get(username).toString();
        }
        System.out.println("-----------------【findUser】-----------------");
        System.out.println("-----------------Username:"+username+"-----------------");
        System.out.println("-----------------【Result】:"+password+"------------------");
        return password;
        
    }
}
package com.jushi.aop;
import java.util.Date;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class CheckUser {
    @Pointcut("execution(* com.jushi.aop.*.find*(..))")
    public void checkUser(){
        System.out.println("**************The System is Searching Information For You****************");
    }
    
    @Pointcut("execution(* com.jushi.aop.*.add*(..))")
    public void checkAdd(){
        System.out.println("**************<< Add User >> Checking.....***************");
    }
    
    @Before("checkUser()")
    public void beforeCheck(){
        System.out.println(">>>>>>>> 准备搜查用户..........");
    }
    
    @After("checkUser()")
    public void afterCheck(){
        System.out.println(">>>>>>>> 搜查用户完毕..........");
    }
    @Before("checkAdd()")
    public void beforeAdd(){
        System.out.println(">>>>>>>> 增加用户--检查ing..........");
    }
    
    @After("checkAdd()")
    public void afterAdd(){
        System.out.println(">>>>>>>> 增加用户--检查完毕!未发现异常!..........");
    }
}
package com.jushi.aop;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jushi.entity.User;
public class Test {
    
    public static void main(String as[]){
        BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
        IUser user = (IUser) factory.getBean("user");
        user.findAll();
        
//        User u = new User();
//        u.setUsername("Tom");
//        user.findUser(u.getUsername());
        
//        u.setUsername("haha");
//        user.addUser(u.getUsername());
    }
}
<?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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
                           http://www.springframework.org/schema/tx 
                           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
    <aop:aspectj-autoproxy/>
    <bean id="user" class="com.jushi.aop.IUserImpl"/>
    <bean id="check" class="com.jushi.aop.CheckUser"/>
    
    <!--  <aop:config>
        <aop:pointcut id="find" expression="execution(* com.jushi.aop.*.find*(..))" />
        <aop:pointcut id="add"     expression="execution(* com.jushi.aop.*.add*(..))" />
        
        <aop:aspect id="checkUser" ref="check">
            <aop:before method="beforeCheck" pointcut-ref="find"/>
            <aop:after method="afterCheck" pointcut-ref="find"/>
        </aop:aspect>
        
        <aop:aspect id="checkAdd" ref="check">
            <aop:before method="beforeAdd" pointcut-ref="add"/>
            <aop:after method="afterAdd" pointcut-ref="add"/>
        </aop:aspect>
    
    </aop:config>-->
</beans>


你可能感兴趣的:(Aop实例)