@Configuration
public class Class1 {
    @Bean
    public JavaMailSenderImpl mailSender() {
        ....
    }
}

@Component
public class Class2 {
    @Autowired
    JavaMailSenderImpl mailSender;


而且我仍然得到:

No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] 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)}


Could not autowire field: org.springframework.mail.javamail.JavaMailSenderImpl (path_of_where_autowiring); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] 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)}


Class1 bean位置在Package1中,而Class2位置在Package2中。

为什么找不到我的豆子?救命

(也尝试过此link,但没有帮助我)

编辑

public static void main(String[] args) throws URISyntaxException, IOException {
    ConfigurableApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class);
    Implclass nsi = applicationContext.getBean(Implclass.class);
    nsi.the_method_here();
}


@Component
public class Implclass implements Implinterface {
    @Autowired
    JavaMailSenderImpl mailSender;
    @Override
    public void the_method_here(){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(sender);
        message.setTo(receiver);
        message.setSubject(subject);
        message.setText(content);

        mailSenderService.send(message);
    }
}


@Configuration
@ComponentScan
public class SpringConfiguration {
   @Bean
    public PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
        PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        propertyPlaceholderConfigurer.setLocations(new ClassPathResource("some_property.file"));
        propertyPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
        return propertyPlaceholderConfigurer;
    }
}


编辑(树)

x - src/main/java
  x -- package_1
    x - Class1
  x -- package_2
    x - Class2 (ImplClass)

最佳答案

使用@ComponentScan({"Package1", "Package2"})

09-26 04:23