zoukankan      html  css  js  c++  java
  • 之前SpringMVC+Mybatis整合,现在加上AOP

    之前SpringMvc和mybatis整合的例子:http://www.cnblogs.com/acehalo/p/3901809.html

    Controller类增加方法,以便测试:

        @RequestMapping(value = "/aopTest")
        @ResponseBody
        public String aopTest(HttpServletRequest request) {
    
            for (int i = 0; i < 3; i++) {
                aopServiceTest.doService();
            }
    
            return "hi";
        }

    新增service类AopServiceTest:

    package com.hi.test.service;
    
    import org.springframework.stereotype.Service;
    
    @Service
    public class AopServiceTest {
        
        public void doService(){
            System.out.println("do service");
        }
    
    }

    新增切面类:

    package com.hi.test.aop;
    
    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 AopTest {
    
        
        @Pointcut("execution(* com.hi.test.service.*.*(..))")
        public void aspect(){    }
        
        @Before("aspect()")  
        public void before(){  
            System.out.println("Before");  
        } 
        
        @After("aspect()") 
        public void after(){
            System.out.println("After");  
        }
    }

    调整之前的spring-aop.xml为:

    <?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:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
           ">
       
        <!-- spring aop -->
        <aop:aspectj-autoproxy proxy-target-class="true"/>
    
      
        <bean id="aop" class="com.hi.test.aop.AopTest"/> 
    
    </beans>

    如果不在spring-aop.xml中加入AopTest这个切面类,切面就没有用,感觉是自动扫描的时候没有扫到这个类,具体原因求知道的解释一下,不胜感激。

    访问 http://localhost:8080/Test/aopTest.do

    出现:
    Before
    do service
    After
    Before
    do service
    After
    Before
    do service
    After

    说明aop有用......

  • 相关阅读:
    hdu1418 欧拉公式
    hdu1215七夕节 筛选法求公因子和
    hdu1215 The area
    hdu1005Number Sequence
    hdu1021 数学题 并不是说难,而是数学题那种简单朴素的思想get不到
    Mongo第三个参数的用法
    js 显示刚刚上传的图片 (onchange事件)
    在linux中安装memcache服务器
    jQuery 倒计时
    PHP获取文章发布时间
  • 原文地址:https://www.cnblogs.com/acehalo/p/3905718.html
Copyright © 2011-2022 走看看