本帖最后由 wd9053 于 2011-03-07 16:47:27 编辑

解决方案 »

  1.   

    <aop:pointcut id="cut" expression="execution(* test.*.work(..))" />
    这里有问题吧  execution(* test.*.work(..))修改成 execution(* test..*.*(..)) 试试
      

  2.   

    很奇特! 我把你的代码改了一下就好了。
    public class Main {
        public static void main(String[] args) throws Exception{
    //        BeanFactory factory = new XmlBeanFactory(new FileSystemResource("bin/applicationContext.xml"));
    //        MyBean bean = (MyBean)factory.getBean("myBean");
    //        bean.work();
            
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            MyBean bean = (MyBean)context.getBean("myBean");
            bean.work();
            System.out.println(bean.getClass().getName());
        }
    }被注释掉的是你的代码。
    你的MyBean是一个类,而不是一个接口的实现类。所以spring不得不用cglib帮你生成了MyBean的子类,来构成代理。我想原因可能是BeanFactory不会用cglib。但是,但是ApplicationContext的实现类会这么干。得到的结论是:没有特殊原因,不要用BeanFactory,用ApplicationContext最好!
      

  3.   

    我试了一下,提取接口对于BeanFactory也没有作用,切点前后的方法还是不被调用有什么BeanFactory能够成功使用AOP的例子吗?package test;public class MyBean implements BeanInterface {
    /* (non-Javadoc)
     * @see test.BeanInterface#work()
     */
    public void work(){
    System.out.println("Do something");
    }
    }
    package test;import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    import org.springframework.core.io.FileSystemResource;public class Main {
    public static void main(String[] args) throws Exception{
    BeanFactory factory = new XmlBeanFactory(new FileSystemResource("./src/applicationContext.xml"));
    BeanInterface bean = (BeanInterface)factory.getBean("myBean");
    //ApplicationContext context = new FileSystemXmlApplicationContext("./src/applicationContext.xml");
    //BeanInterface bean = (BeanInterface)context.getBean("myBean");
    bean.work();
    }
    }
      

  4.   

    静静的期待答案 也在学Spring 有点雾水的说
      

  5.   


    程序运行不报错,只是AOP切点前后的方法没有被调用
      

  6.   

    我是4楼的,你确定你的机器上我的程序也没有执行到切面的代码吗?我的执行结果如下:log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
    log before bean method
    Do something
    log after bean method
    test.MyBean$$EnhancerByCGLIB$$9f83794e
    你要的AOP切点前后的方法调用都出来了啊。
    难道是spring版本问题? 我用的是spring2.5.6。 你的呢?
      

  7.   

     1. BeanFactory factory = new XmlBeanFactory(new FileSystemResource("./src/applicationContext.xml"));
    改为:BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
    2. <aop:config>
            <aop:aspect ref="myLogger">
                <aop:pointcut id="cut" expression="execution(* test.*.work(..))" />
                <aop:before method="logBefore" pointcut-ref="cut" />
                <aop:after-returning method="logAfter" pointcut-ref="cut" />
            </aop:aspect>
    改为:<aop:config>
            <aop:aspect ref="myLogger">
                <aop:pointcut id="cut" expression="execution(* test.*.work(..))" />
                <aop:before method="logBefore" pointcut-ref="cut" />
                <aop:after-returning method="logAfter" pointcut-ref="cut" />
            </aop:aspect>
            </aop:config>
    在我电脑上能运行!!!LZ试下!!
      

  8.   

    欢迎加入java ee 经验群14471754 基本都是三年以上程序员
      

  9.   

    确实 LZ少了个 </aop:config>