NOTE
How to Load Beans A and B in Order in Spring
1. Use @DependsOn 2. References
This is a historical learning note and may contain outdated or incomplete understanding.
1. Use the @DependsOn Annotation
- TestController
@DependsOn("testService")
@Service
public class TestController
{
@Autowired
private TestService testService;
public TestController()
{
System.out.println(this.getClass().getSimpleName() + " initializing...");
}
public void test()
{
String byId = testService.getById(1);
System.out.println(byId);
}
}
- TestService
@Service
public class TestService
{
public TestService()
{
System.out.println(this.getClass().getSimpleName() + " initializing...");
}
public String getById(Integer id)
{
return "" + id;
}
}
- Output during startup
TestService initializing...
TestController initializing...
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub