2 liangyu henry liangyu_henry 于 2016.09.26 14:36 提问

新人求助: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
Veggiel   2016.09.26 15:42
已采纳
 <context:annotation-config />
 <context:componet-scan base-package="test.spring.aop.bean" />
 <aop:aspectj-autoproxy />
liangyu_henry
liangyu_henry 感谢,问题解决了,<property name="pattern" ref=".*eat"></property> 里面应该是value=".*eat"我写成ref了
大约一年之前 回复
Veggiel
Veggiel   2016.09.26 15:37

放到bean前面试试


Csdn user default icon
上传中...
上传图片
插入图片