我在春季在@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="[email protected]" />
    <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>


如上所述,在null类中,mailSender对象始终为MailService

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

10-05 20:10