【问题标题】:Property Inject in Spring FrameworkSpring 框架中的属性注入
【发布时间】:2015-01-06 14:44:23
【问题描述】:

我在使用 Spring MVC 向 bean 注入值时遇到问题。

这是我对 Spring 的部分配置:

<beans>
    <!-- Enable Annotation based configuration -->
    <context:annotation-config />

    <!-- MailGun.org Authentication -->
    <bean class="com.example.something.util.MailSender">
        <property name="smtpHost" value="smtp.mailgun.org" />
        <property name="smtpUsername" value="USERNAME_HERE" />
        <property name="smtpPassWord" value="PASSWORD_HERE" />
    </bean>
</beans>

这是要注入的 bean:

package com.example.something.util;

@Component
public class MailSender {
    public void sendMail(String recipient, String subject, String text) {
        System.out.println(smtpHost);
    }

    @Autowired
    private String smtpHost;
}

为了对此进行测试,我创建了一个测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:test-spring-context.xml"})
public class MailSenderTest {
    @Test
    public void testSendMail() {
        MailSender sender = new MailSender();
        sender.sendMail("zjhzxhz@gmail.com", "Mail From MailGun", "This is a test mail");
    }
}

当我运行这个测试用例时,我遇到了一个异常:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.example.something.util.MailSender.smtpHost; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 41 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1308)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 43 more

它有什么问题?请帮我。谢谢!

【问题讨论】:

    标签: java spring spring-mvc inject


    【解决方案1】:

    你不需要smtpHost 上的@Autowired,如果你从 Spring 上下文中获取 MailSender bean,它应该已经被填充。你没有这样做,因为用 new MailSender() 实例化它完全绕过了 Spring。相反,您应该使用context.getBean(MailSender.class)。

    【讨论】:

    • 但我不认为context.getBean() 是一个优雅的实现。我可以在这里使用注释吗?
    • @HowardShieh 使用 @Autowired MailSender sender 并将其移动到测试类的字段中,而不是尝试在方法中对其进行初始化。
    • 正如@LuiggiMendoza 所说,您可以在这里使用@Autowired MailSender sender,因为测试是在Spring 上下文中运行的。
    猜你喜欢
    • 1970-01-01
    • 2015-06-13
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 1970-01-01
    • 2022-06-22
    相关资源
    最近更新 更多