• Spring>采用静态配置文件方式实现AOP 小强斋


    spring对AOP的支持(采用配置文件的方式)

    1、spring依赖库
                  SPRING_HOME/dist/spring.jar
                  SPRING_HOME/lib/jakarta-commons/commons-logging.jar
                  SPRING_HOME/lib/log4j/log4j-1.2.14.jar
                  SPRING_HOME/lib/aspectj/*.jar
    2、配置如下

    <aop:config>
    		<aop:aspect id="security" ref="securityHandler">
    			<aop:pointcut id="allAddMethod" expression="execution(* com.bjsxt.spring.UserManagerImpl.add*(..))"/>
    			<aop:before method="checkSecurity" pointcut-ref="allAddMethod"/>
    		</aop:aspect>
    	</aop:config>	
    		

    二、例子代码

    UserManager.java

    package com.ncepu.spring;
    
    
    public interface UserManager {
    	public void add(String name,String password);
    	public void delete(int id);
    	public void modify(String name,String password);
    	public void find (int id);
    }
    

    UserManagerImpl.java

    package com.ncepu.spring;
    
    public class UserManagerImpl implements UserManager {
    
    	@Override
    	public void add(String name, String password) {
    	System.out.print("添加用户");
    
    	}
    
    	@Override
    	public void delete(int id) {
    		System.out.print("删除用户");
    
    	}
    
    	@Override
    	public void find(int id) {
    		System.out.print("查找用户");
    	}
    
    	@Override
    	public void modify(String name, String password) {
    		System.out.print("修改用户");
    
    	}
    
    }
    

    SecurityHandler.java

    package com.ncepu.spring;
    
    
    
    public class SecurityHandler {
    
    	private void checkSecurity() {
    		System.out.println("安全性检查");
    	}
    
    }
    

    我们可以通过Advice中添加一个JoinPoint参数,这个值会由spring自动传入,从JoinPoint中可以取得参数值、方法名等等

    package com.ncepu.spring;
    
    import org.aspectj.lang.JoinPoint;
    
    
    public class SecurityHandler {
    
    	private void checkSecurity(JoinPoint jointpoint) {
    		Object[] args=jointpoint.getArgs();
    		
    		for(int i=0;i<args.length;i++)
    			System.out.println(args[i]);
    		System.out.println(jointpoint.getSignature().getName());
    		System.out.println(jointpoint.getTarget());
    		System.out.println("安全性检查");
    	}
    
    }
    

    Client.java

    package com.ncepu.client;
    
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.ncepu.spring.UserManager;
    
    public class Client {
    	
    	public static void main(String args[])
    	{
    		BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
    		UserManager userManager=(UserManager)factory.getBean("usermanager");
    		userManager.add("wsz","ncepu");
    	}
    
    }
    

    applicationContext.xml

    	<bean id="usermanager" class="com.ncepu.spring.UserManagerImpl"></bean>
    	<bean id="securityHandler" class="com.ncepu.spring.SecurityHandler"></bean>
    
    	<aop:config>
    		<aop:aspect id="security" ref="securityHandler">
    			<aop:pointcut id="allAddMethod"
    				expression="execution(* com.ncepu.spring.UserManagerImpl.add*(..))" />
    			<aop:before method="checkSecurity" pointcut-ref="allAddMethod" />
    		</aop:aspect>
    	</aop:config>
    




     

  • 相关阅读:
    光学
    ZYNQ学习笔记2——实例
    ZYNQ学习笔记
    AD使用技巧
    关于浮点运算的一点见解
    解决ccs不能同时导入两个相同工程名的问题
    multisum14 快捷键
    你的进程为什么被OOM Killer杀死了?
    Linux下哪些进程在消耗我们的cache?
    linux 安装python3.7.5
  • 原文地址:https://www.cnblogs.com/xiaoqiangzhaitai/p/5637491.html
Copyright © 2020-2023  润新知