我的Spring Boot应用程序完全使用注释配置(没有XML上下文文件)。根类看起来像这样:
package uk.org.sehicl.website;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import uk.org.sehicl.website.users.EmailSender;
import uk.org.sehicl.website.users.UserDatastore;
import uk.org.sehicl.website.users.UserManager;
import uk.org.sehicl.website.users.impl.RedisDatastore;
import uk.org.sehicl.website.users.impl.SendgridSender;
@SpringBootApplication
public class Application
{
public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
}
@Bean
public UserDatastore userDatastore()
{
return new RedisDatastore(System.getenv("REDIS_URL"));
}
@Bean
public EmailSender emailSender()
{
return new SendgridSender();
}
@Bean
public UserManager userManager()
{
return new UserManager();
}
}
UserManager
bean包含@Autowired
对其他两个的依赖:
@Autowired
private UserDatastore datastore;
@Autowired
private EmailSender emailer;
当我运行应用程序时,一切都很好;但datastore
bean的emailer
和UserManager
字段为null
。
我确定我必须遗漏一些非常明显的东西,但我如何让这些自动连接的依赖项成为自动装配?