如何解决“FactoryBean在对象创建时抛出异常”;

时间:2015-08-13 07:08:42

标签: spring

我正在试试“beforeAdvice”而不是春天的“JointPoints”。我调用目标方法非常简单。在执行目标类方法之前,应该执行before()方法。

ClientApp:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.nt.services.LoanApprover;

public class ClientApp {
public static void main(String[] args) {
//activate ioc container
ApplicationContext context = new FileSystemXmlApplicationContext("src/com/nt/cfgs/applicationContext.xml");
//get bean
LoanApprover approver = context.getBean("pfb",LoanApprover.class);
//call b.method
approver.approver();
}
}

applicationContext.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="auditAdvice" class="com.nt.aspect.MyBeforeAdvice" />
<bean id="target" class="com.nt.services.LoanApprover" />
<bean id="pfb" class="
    org.springframework.aop.framework.ProxyFactoryBean ">
    <property name="target" ref="target" />
    <property name="interceptorNames">
        <list>
            <value>
                auditAdvice
            </value>
        </list>
    </property>
</bean>
</beans>

MyBeforeAdvice.java

import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class MyBeforeAdvice implements MethodBeforeAdvice{
@Override
public void before(Method method, Object[] args, Object target)
        throws Throwable {
    System.out.println("I am in MyBeforeAdvice class");
}
}

LoanApprover.java

public class LoanApprover {
public void approver(){
    System.out.println("I am in LoanApprover method");
}
}

当我运行此应用程序时,我收到此异常。

Aug 13, 2015 12:16:04 PM     org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing     org.springframework.context.support.FileSystemXmlApplicationContext@7c6cd67b:     startup date [Thu Aug 13 12:16:04 IST 2015]; root of context hierarchy
Aug 13, 2015 12:16:04 PM     org.springframework.beans.factory.xml.XmlBeanDefinitionReader     loadBeanDefinitions
INFO: Loading XML bean definitions from file     [G:\java\Frameworks\SpringAOP\AOPProj3(Before     Advice)\src\com\nt\cfgs\applicationContext.xml]
Exception in thread "main"     org.springframework.beans.factory.BeanCreationException: Error creating bean     with name 'pfb': FactoryBean threw exception on object creation; nested     exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No     bean named '
                    auditAdvice
            ' is defined
at     org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObject     FromFactoryBean(FactoryBeanRegistrySupport.java:175)
at     org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFr    omFactoryBean(FactoryBeanRegistrySupport.java:103)
at     org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanIn    stance(AbstractBeanFactory.java:1517)
  at      org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(Abstract    BeanFactory.java:251)
at     org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBe    anFactory.java:199)
 at     org.springframework.context.support.AbstractApplicationContext.getBean(AbstractA    pplicationContext.java:962)
at test.ClientApp.main(ClientApp.java:13)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:   No bean named '
                auditAdvice
            ' is defined
at     org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefi    nition(DefaultListableBeanFactory.java:694)
at   org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBean    Definition(AbstractBeanFactory.java:1168)
at      org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(Abstract    BeanFactory.java:281)
at  org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBe    anFactory.java:194)
at org.springframework.aop.framework.ProxyFactoryBean.initializeAdvisorChain(ProxyF    actoryBean.java:460)
at org.springframework.aop.framework.ProxyFactoryBean.getObject(ProxyFactoryBean.ja    va:244)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObject    FromFactoryBean(FactoryBeanRegistrySupport.java:168)
... 6 more

请有人告诉我为什么我得到这个例外?

1 个答案:

答案 0 :(得分:1)

只需更改

<value>
    auditAdvice
</value>

<value>auditAdvice</value>

看起来spring在这里使用空格和换行符号作为ID。 有关该主题的更多信息可用here。 希望有所帮助。