Spring 框架中的属性注入
Property Inject in Spring Framework
我在使用 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
有什么问题吗?请帮我。谢谢!
您不需要 smtpHost
上的 @Autowired
,如果您从 Spring 上下文中获取 MailSender
bean,它应该已经被填充。你没有,因为用 new MailSender()
实例化它完全绕过 Spring 。相反,您应该使用 context.getBean(MailSender.class)
.
我在使用 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
有什么问题吗?请帮我。谢谢!
您不需要 smtpHost
上的 @Autowired
,如果您从 Spring 上下文中获取 MailSender
bean,它应该已经被填充。你没有,因为用 new MailSender()
实例化它完全绕过 Spring 。相反,您应该使用 context.getBean(MailSender.class)
.