【问题标题】:No bean named 'errorNotifier' is defined没有定义名为“errorNotifier”的 bean
【发布时间】:2011-12-08 18:44:11
【问题描述】:

我正在尝试在春季建立一个 bean 工厂,这应该很简单,但我就是不知道为什么它不起作用。今天大部分时间都在看示例,阅读 stackOverflow 上的其他类似帖子,阅读 Spring In Action 以及 Spring Recipes,但到目前为止还没有成功。第二双眼睛可能很快就会发现我的错误。

错误通知接口

public interface ErrorNotifier {
    public void notifyCopyError(String srcDir, String destDir, String filename);        
}

错误通知器实现

public class EmailErrorNotifier implements ErrorNotifier {

    private MailSender mailSender;

    /**
     * Blank constructor
     */
    public EmailErrorNotifier() {
    }

    public void setMailSender(MailSender mailSender) {
        this.mailSender = mailSender;
    }

    @Override
    public void notifyCopyError(String srcDir, String destDir, String filename) {

        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("system@localhost");
        message.setTo("admin@localhost");
        message.setSubject("Finished Uploading File");
        message.setText("Your upload failed!");

        mailSender.send(message);

    }
}

我在 applicationContext.xml 中的配置

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${email.host}"/>
        <property name="protocol" value="${email.protocol}"/>
        <property name="port" value="${email.port}"/>
        <property name="username" value="${email.username}"/>
        <property name="password" value="${email.password}"/>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
            </props>
        </property>
    </bean>

    <bean id="errorNotifier" name="errorNotifier"  class="com.bdw.controller.EmailErrorNotifier">           
        <property name="mailSender" ref="mailSender"/>
    </bean>

以及我测试它的类

public class test {

    public static void main(String[] args) {

        ApplicationContext context =
                new ClassPathXmlApplicationContext(
                ApplicationContext.CLASSPATH_ALL_URL_PREFIX
                + "applicationContext.xml");

        ErrorNotifier notifier =
                context.getBean("errorNotifier", ErrorNotifier.class);
        notifier.notifyCopyError("test", "test", "test");

    }
}

我在 tomcat 或 glassfish 日志中没有收到任何错误,只有以下输出:

线程“main”中的异常 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 名为“errorNotifier”的 bean 定义在 org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:527) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1083) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:274) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 在 org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1079) 在 test.main(test.java:21)

如果我将 context.getBean 参数更改为查找 mailSender,我会得到 No bean named 'mailSender'。

有什么想法吗?

【问题讨论】:

  • 我将从以下内容开始:调整 Spring 日志级别并确保启动时没有损坏,并确保 applicationContext 在执行时实际上位于类路径中。
  • applicationContext 当前位于 /src/main/resources/META-INF/spring/applicationContext.xml
  • 嗯,如果你把它放在 /src/main/resources 中(假设是一个 Maven 项目),它可以工作吗?
  • 修复它!有趣的是,Spring ROO 将邮件设置放在 META-INF 中,如果它在那里不起作用:-|如何让它在默认位置工作?
  • 我想知道是不是那个类路径前缀弄乱了它?不确定:/

标签: spring spring-roo


【解决方案1】:

applicationContext 文件可能不在类路径上;我不确定 all_URL_prefix 是否会超过文件系统和 jar 的根级别,从而使测试变得不稳定。尝试移动配置文件,或更改从中获取配置文件的位置列表。

【讨论】:

  • 你太棒了 Dave,很高兴我能从 stackoverflow 上的专家那里学到东西。 ClassPathXmlApplicationContext("META-INF/spring/applicationContext.xml") 可以解决问题。之前试过这个解决方案,但是假设根目录是 META-INF,所以显然它不起作用。
  • 不是很厉害,只是被同类型的错误给砸了;)
猜你喜欢
  • 2012-03-25
  • 2012-05-29
  • 2011-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多