没有名为“errorNotifier”的 bean;被定义

发布于 2024-12-10 08:34:41 字数 3407 浏览 0 评论 0原文

我正在尝试在春天建立一个 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'。

有什么想法吗?

I'm trying to setup a bean factory in spring, something which should be really simple to do, but I just can't figure out why it's not working. Spend most of today looking at examples, reading other similar posts on stackOverflow, reading Spring In Action as well as Spring Recipes with no success so far. A second pair of eyes will probably pick up my mistake in no time.

Error Notifier Interface

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

Error Notifier Implementation

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);

    }
}

My config in 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>

And the class in which I test it

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");

    }
}

I don't get any errors in tomcat or glassfish logs, just this output:

Exception in thread "main"
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
bean named 'errorNotifier' is defined at
org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:527)
at
org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1083)
at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:274)
at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at
org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1079)
at test.main(test.java:21)

If I change context.getBean parameter to lookup mailSender, I get No bean named 'mailSender'.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

棒棒糖 2024-12-17 08:34:41

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

The applicationContext file is likely not on the class path; I'm not sure that the all_URL_prefix will dig past the root level of the filesystem and jars, making the test go wonky. Try moving the config file, or changing the list of locations from which to grab the config file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文