NOTE
How to Load Beans A and B in Order in Spring
Controller and Service examples examine loading relationships between Spring beans and ways to control their initialization order.
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...
2. References
Drag to pan, scroll to zoom, and click a node to expand or collapse it. Large maps start with deeper levels collapsed; click nodes to expand them.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub