简体   繁体   中英

Property Inject in Spring Framework

I have problem injecting a value to a bean using Spring MVC.

Here's my partial configuration of 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>

And here's the bean to inject:

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

To test this, I create a test class:

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

When I run this test case, I got an exception:

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

What's wrong with it? Please help me. Thx!

You don't need @Autowired on smtpHost , it should be already populated if you get the MailSender bean from the Spring context. Which you don't, because instantiating it with new MailSender() bypasses Spring altogether. Instead you should use context.getBean(MailSender.class) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM