zoukankan      html  css  js  c++  java
  • 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>
    




     

  • 相关阅读:
    c# 文件IO操作 StreamReader StreamWriter Split 使用
    UE4材质初探
    栈溢出 stackoverflow 的原因及解决办法
    UE4高级功能-初探超大无缝map的实现LevelStream
    为什么Unreal 4引擎能轻易实时渲染出vray要花半天才能渲染出的场景
    SSAO
    Unity3D 游戏贴图(法线贴图,漫反射贴图,高光贴图)
    巨人纪学峰 未来网游发展趋势是社区化
    Ogre中级教程
    国内自主研发的游戏引擎一览
  • 原文地址:https://www.cnblogs.com/xqzt/p/5637174.html
Copyright © 2011-2022 走看看