zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    美联储主席和欧洲央行说了什么
    12月CPI,PPI有哪些变化
    中国人民银行行长易纲就贯彻落实中央经济工作会议精神接受采访谈
    2018年个人的一些简单预测
    从首套房利率走势看市场
    百城价格房价周期和郑州、武汉房价比较分析
    国际非农超预期美联储主席态度软化,国内适度宽松货币+积极财政仍是主基调
    三大经济体年2018年末形势一览
    从房地产住宅销售面积增速看房地产行业
    枚举类
  • 原文地址:https://www.cnblogs.com/sdnu-zhang/p/8528154.html
Copyright © 2011-2022 走看看