为什么我的输出结果没有MoocAspect before,只有AspectBiz呢?怎么觉得是配置文件没有生效??

来源:5-4 Advice应用(上)

谢同学谢童鞋

2016-03-06 20:26

配置文件如下:

<?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"

    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-4.0.xsd">

       

       <bean id="moocAspect" class="com.imooc.aop.schema.advice.MoocAspect"></bean>

<bean id="aspectBiz" class="com.imooc.aop.schema.advice.AspectBiz"></bean>

<aop:config>

<aop:aspect id="moocAspectAOP" ref="moocAspect">

<aop:pointcut expression="execution(* com.imooc.aop.schema.advice.biz.*Biz.*(..))" id="moocPoint"/> 

  <aop:before method="before" pointcut-ref="moocPoint"/>

  </aop:aspect>

  </aop:config> 

 </beans>


输出结果:

三月 06, 2016 8:42:08 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh

INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5bc22f22: startup date [Sun Mar 06 20:42:08 CST 2016]; root of context hierarchy

三月 06, 2016 8:42:08 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from URL [file:/D:/Program%20Files%20(x86)/eclipse-jee-luna-SR2-win32-x86_64/eclipse/D/workspace/spring/bin/spring-aop-schema-aspect.xml]

AspectBiz

三月 06, 2016 8:42:08 下午 org.springframework.context.support.AbstractApplicationContext doClose

INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@5bc22f22: startup date [Sun Mar 06 20:42:08 CST 2016]; root of context hierarchy


写回答 关注

3回答

  • 灏仁
    2016-04-11 22:39:47
    已采纳

    试一下在execution()里面的*后空一格,在写包那些方法

    小小幻术师

    分析半小时没发现问题,用你的方法解决了,再次感谢!

    2017-05-08 19:48:11

    共 3 条回复 >

  • 谢同学谢童鞋
    2016-03-10 10:46:07

    package com.imooc.aop.schema.advice;

    public class AspectBiz {

    public void biz(){

    System.out.println("AspectBiz");

    //throw new RuntimeException();

    }

    }


    package com.imooc.aop.schema.advice;

    import org.aspectj.lang.ProceedingJoinPoint;

    public class MoocAspect {

    public void before(){

    System.out.println("MoocAspect before");

    }

    public void afterReturning(){

    System.out.println("MoocAspect after");

    }

    public void afterThrowing(){

    System.out.println("MoocAspect throwing");

    }

    public void after(){

    System.out.println("MoocAspect after");

    }

    public Object around(ProceedingJoinPoint pjp){

    Object obj = null;

    try {

    System.out.println("MoocAspect around 1");

    obj = pjp.proceed();

    System.out.println("MoocAspect around 2");

    } catch (Throwable e) {

    e.printStackTrace();

    }

    return obj;

    }

    }


    package com.imooc.aop.schema.test;

    import org.junit.Test;

    import org.junit.runner.RunWith;

    import org.junit.runners.BlockJUnit4ClassRunner;

    import com.imooc.aop.schema.advice.AspectBiz;

    import com.imooc.test.UnitTestBase;

    @RunWith(BlockJUnit4ClassRunner.class)

    public class TestAOPSchemaAdvice extends UnitTestBase{

    public TestAOPSchemaAdvice(){

    super("classpath*:spring-aop-schema-aspect.xml");

    }

    @Test

    public void testBiz(){

    AspectBiz biz = super.getBean("aspectBiz");

    biz.biz();

    }

    }

    http://img.mukewang.com/56e0dfb0000140f002460146.jpg

    兮蓅 回复兮蓅

    我知道了

    2016-12-08 21:11:32

    共 2 条回复 >

  • SilveryBullet
    2016-03-08 12:30:15

    要不要把java文件也贴上来,这样更容易确定问题

Spring入门篇

为您带来IOC和AOP的基本概念及用法,为后续高级课程学习打下基础

268785 学习 · 963 问题

查看课程

相似问题