不问收获_但问耕耘 2016-09-26 06:36 采纳率: 66.7%
浏览 961
已采纳

新人求助:springAOP自动代理配置

如下,这是源码

接口

 package test.spring.aop.bean;

public interface Eatable {

    public void eat();
}

实现类

 package test.spring.aop.bean;

public class EatLunch implements Eatable {

    public void eat() {
        System.out.println("吃午饭...");
    }

}

通知

 package test.spring.aop.bean;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

public class EatHelper implements MethodBeforeAdvice,AfterReturningAdvice{

    public void afterReturning(Object arg0, Method arg1, Object[] arg2,
            Object arg3) throws Throwable {

        System.out.println("吃完饭去刷牙了。。");
    }

    public void before(Method arg0, Object[] arg1, Object arg2)
            throws Throwable {
        System.out.println("要吃饭了,去洗手了。。");

    }

}

Spring配置文件 app3.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:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">



    <!-- SpringAOP学习  Spring自动代理 -->

    <bean id="eatLunch" class="test.spring.aop.bean.EatLunch"></bean>

    <!-- SpringAOP学习之配置文件第1步    配置通知bean-->
    <bean id="eatHelper" class="test.spring.aop.bean.EatHelper">
    </bean>

    <!-- SpringAOP学习之配置文件第2步    配置切点通知者-->
    <bean id="eatHelperAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice" ref="eatHelper"></property>
        <property name="pattern" ref=".*eat"></property>
    </bean>
    <!-- SpringAOP学习之配置文件第3步    自动代理对象-->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
    </bean>






</beans>

junit测试类

 package test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import test.spring.aop.bean.Eatable;

public class Test_SpringAop2 {

    @Test
    public void test1(){
        ApplicationContext appCtx = new ClassPathXmlApplicationContext("app3.xml");
        Eatable eatable = (Eatable)appCtx.getBean("eatLunch");
        eatable.eat();
    }

}

运行之后junit报错,如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eatLunch' defined in class path resource [app3.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eatHelperAdvisor' defined in class path resource [app3.xml]: Cannot resolve reference to bean '.*eat' while setting bean property 'pattern'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '.*eat' is defined

采用手动代理配置可以得到正确结果,如下:
图片说明

  • 写回答

2条回答

  • Veggiel 2016-09-26 07:42
    关注
     <context:annotation-config />
     <context:componet-scan base-package="test.spring.aop.bean" />
     <aop:aspectj-autoproxy />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?