• Spring之基于XML配置切面


    public interface Cacl {
        
        int add(int i,int j);
        int sub(int i,int j);
        int mul(int i,int j);
        int div(int i,int j);
    
    }

    import org.springframework.stereotype.Component;
    
    @Component
    public class CaclImpl implements Cacl {
    
        @Override
        public int add(int i, int j) {
            // TODO Auto-generated method stub
            int result = i+j;
            return result;
        }
    
        @Override
        public int sub(int i, int j) {
            // TODO Auto-generated method stub
            int result = i-j;
            return result;
        }
    
        @Override
        public int mul(int i, int j) {
            // TODO Auto-generated method stub
            int result = i*j;
            return result;
        }
    
        @Override
        public int div(int i, int j) {
            // TODO Auto-generated method stub
            int result = i/j;
            return result;
        }
    
    }

    import java.util.Arrays;
    import java.util.List;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.ProceedingJoinPoint;
    
    
    public class Log {
        public void before(JoinPoint joinPoint){
            String methodName = joinPoint.getSignature().getName();
            List<Object> args = Arrays.asList(joinPoint.getArgs());
            System.out.println("before method"+methodName+" begins with "+args);
        }
        
        public void after(JoinPoint joinPoint){
            String methodName = joinPoint.getSignature().getName();
            System.out.println("after method "+methodName+" ends");
        }
        
        public void afterReturning(JoinPoint joinPoint,Object result){
            String methodName = joinPoint.getSignature().getName();
            System.out.println("afterreturning method "+methodName+" ends with "+result);
        }
        
        public void afterThrowing(JoinPoint joinPoint,Exception exception){
            String methodName = joinPoint.getSignature().getName();
            System.out.println("afterthrowing method "+methodName +"occurs by "+exception);
        }
        
        public Object around(ProceedingJoinPoint pjp){
            Object result = null;
            String methodName = pjp.getSignature().getName();
            
            try {
                System.out.println(" around method "+methodName +" begins with "+Arrays.asList(pjp.getArgs()));
                result = pjp.proceed();
                System.out.println("around method "+methodName +" ends ");
            } catch (Throwable e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return result;
        }
        
    }

    这样代码中就没有注解了

    <?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:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
        <!-- 配置bean -->
        <bean id="cacl" class="com.atguigu.spring.aop.impl.xml.CaclImpl"></bean>
        
        <!-- 配置切面的bean -->
        <bean id="logAspect" class="com.atguigu.spring.aop.impl.xml.Log"></bean>
        
        <!-- 配置AOP -->
        <aop:config>
            <!-- 配置切点表达式 -->
            <aop:pointcut expression="execution(* com.atguigu.spring.aop.impl.xml.Cacl.*(int,int))" id="pointcut"/>
            
            <!-- 配置切面及通知 -->
            <aop:aspect ref="logAspect">
                <aop:before method="before" pointcut-ref="pointcut"/>
                <aop:after method="after" pointcut-ref="pointcut"/>
                <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>
                <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="exception"/>
                <aop:around method="around" pointcut-ref="pointcut"/>
            </aop:aspect>
        </aop:config>
    </beans>

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Main {
        
        public static void main(String[] args) {
            
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-xml.xml");
            
            Cacl cacl = (Cacl) ctx.getBean("cacl");
            System.out.println(cacl.add(2,3));
            System.out.println(cacl.div(10,1));    
        }
    }

    ⑥输出结果

    log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
    before methodadd begins with [2, 3]
     around method add begins with [2, 3]
    after method add ends
    afterreturning method add ends with 5
    around method add ends 
    5
    before methoddiv begins with [10, 1]
     around method div begins with [10, 1]
    after method div ends
    afterreturning method div ends with 10
    around method div ends 
    10
  • 相关阅读:
    Authorize 示例
    javscript 实现iframe加载内容页出现Loading效果
    泛型的Distinct(IEqualityComparer)的用法
    关于ViewData与TempData
    将表 自增长列 清零 循环插入时间自增长
    linq count() sum() Min() Max() Average() 用法
    分页的存储过程
    将图片文件与文本文件合并成图片文件
    asp.net mvc利用Json验证数据和导向页面,解决重复提交问题
    JQUERY DIV浮动提示信息
  • 原文地址:https://www.cnblogs.com/sdnu-zhang/p/8528154.html
Copyright © 2020-2023  润新知