不问收获_但问耕耘 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条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题