[英]@Autowired @Bean from other class
@Configuration
public class Class1 {
@Bean
public JavaMailSenderImpl mailSender() {
....
}
}
@Component
public class Class2 {
@Autowired
JavaMailSenderImpl mailSender;
And I still get: 我仍然得到:
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)}
The Class1 bean location is in Package1
and the Class2 location is in Package2
. Class1的豆位置是在
Package1
和Class2中的位置是在Package2
。
Why my bean is not found? 为什么找不到我的豆子? Help
救命
(Also tried this link but didn't helped me) (也试过这个链接,但没有帮助我)
EDIT 编辑
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;
}
}
EDIT (TREE) 编辑(树)
x - src/main/java
x -- package_1
x - Class1
x -- package_2
x - Class2 (ImplClass)
Can you please try with using different names. 你可以尝试使用不同的名字。 Reference Link : https://dzone.com/articles/playing-sround-with-spring-bean-configuration
参考链接: 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;
}
使用@ComponentScan({"Package1", "Package2"})
you can use @ComponentScan
or 你可以使用
@ComponentScan
或
ctx = new AnnotationConfigApplicationContext();
ctx.register(Config.class); // config
ctx.refresh();
// and try
Implclass nsi = ctx.getBean(Implclass.class);
AnnotationConfigApplicationContext can take multiple configuration classes in the constructor. AnnotationConfigApplicationContext可以在构造函数中使用多个配置类。 Can you try passing all the configuration files in the constructor as Step-1.
您可以尝试将构造函数中的所有配置文件作为Step-1传递。 Then it can be further debugged to better the solution.
然后可以进一步调试以更好地解决问题。
AnnotationConfigApplicationContext(Class<?>... annotatedClasses)
eg 例如
ConfigurableApplicationContext applicationContext = new AnnotationConfigApplicationContext(Class1.class, Class2.class, SpringConfiguration.class);
Also, where is your ImplClass, posting a tree structure would have been very helpful. 此外,您的ImplClass在哪里,发布树结构将非常有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.