gpt4 book ai didi

java - 来自其他类的@Autowired @Bean

转载 作者:行者123 更新时间:2023-12-02 04:37:48 25 4
gpt4 key购买 nike

@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 中。

为什么我的bean没有找到?帮助

(也尝试过这个 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"})

关于java - 来自其他类的@Autowired @Bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56540009/

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