gpt4 book ai didi

java - Spring @Autowired 不工作并返回 null

转载 作者:行者123 更新时间:2023-12-01 18:20:06 29 4
gpt4 key购买 nike

我在 Spring 中的 @Autowired 上遇到问题:

MailSender.java

package com.example.package.util;

@Component
public class MailSender {
@Autowired
public MailSender(VelocityEngine velocityEngine, JavaMailSender mailSender) {
this.velocityEngine = velocityEngine;
this.mailSender = mailSender;
}

public void send() {
// Dosomething
}

private final VelocityEngine velocityEngine;

private final JavaMailSender mailSender;
}

MailService.java

package com.example.package.service;

@Service
public class MailService {
public void sendEmail(String email) {
// mailSender is always null
String mailBody = mailSender.send();
}

// Autowired here is not working
@Autowired
@Qualifier("myMailSender")
private MailSender mailSender;

// Autowired here is working
@Autowired
private UserDao userDao;
}

配置

<context:annotation-config />
<context:component-scan base-package="com.example.package" />

<!-- Mail Service -->
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="resourceLoaderPath" value="classpath:/mails" />
<property name="preferFileSystemAccess" value="false" />
</bean>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.mailgun.org" />
<property name="username" value="postmaster@testzilla.org" />
<property name="password" value="" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
<bean id="myMailSender"
class="com.example.package.service.MailService">
<property name="mailSender" ref="mailSender" />
</bean>

正如我上面提到的,MailService 类中的 mailSender 对象始终为 null

可能 MailSender 类和 MailService 类中的 mailSender 对象存在冲突。变量名称相同,但不同。

所以我尝试重命名变量,但仍然不起作用。

单元测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:test-spring-context.xml"})
public class MailSenderTest {
@Autowired
@Qualifier("myMailSender")
private MailSender mailSender;

@Test
public void test() {
String templatePath = "/verifyEmail.vm";
Map<String, Object> model = new HashMap<String, Object>();

System.out.println(mailSender);

String mailBody = mailSender.getMailContent(templatePath, model);
System.out.println(mailBody);
}
}

当我运行测试用例时,出现以下异常:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.package.util.MailSender com.example.package.service.UserService.mailSender; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.package.util.MailSender] 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), @org.springframework.beans.factory.annotation.Qualifier(value=testzillaMailSender)}
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)
... 54 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.package.util.MailSender] 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), @org.springframework.beans.factory.annotation.Qualifier(value=testzillaMailSender)}
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)
... 56 more

你能帮我吗?谢谢。

最佳答案

您的MailService类不是由spring管理的。一种方法是添加注释(如@Service)让spring为你处理它。例如

@Service
public class MailService {
public void sendEmail(String email) {
String templatePath = "/verifyEmail.vm";
Map<String, Object> model = new HashMap<String, Object>();
model.put("email", email);

// mailSender is always null
String mailBody = mailSender.getMailContent(templatePath, model);
}

// Autowired here is not working
@Autowired
private MailSender mailSender;
}

关于java - Spring @Autowired 不工作并返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27840860/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com