来自其他类的@Autowired @Bean

时间:2019-06-11 08:57:48

标签: java spring annotations

@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 bean位置在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)

4 个答案:

答案 0 :(得分:0)

能否请您尝试使用其他名称。 参考链接:https://dzone.com/articles/playing-sround-with-spring-bean-configuration

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

@Component
public class Class2 {
@Autowired
JavaMailSenderImpl mailSender;
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您可以使用@ComponentScan

 ctx = new AnnotationConfigApplicationContext();
 ctx.register(Config.class); // config 
 ctx.refresh();
// and try 
 Implclass nsi = ctx.getBean(Implclass.class);

答案 3 :(得分:0)

AnnotationConfigApplicationContext可以在构造函数中采用多个配置类。您可以尝试将构造器中的所有配置文件作为步骤1传递吗?然后可以对其进行进一步调试,以改善解决方案。

AnnotationConfigApplicationContext(Class<?>... annotatedClasses)

例如

ConfigurableApplicationContext applicationContext = new AnnotationConfigApplicationContext(Class1.class, Class2.class, SpringConfiguration.class);

另外,您的ImplClass在哪里,发布树形结构将非常有帮助。