After enabling @Autowrid
, Maven project with Spring 4, in approximately: 800 ManegedBeans Spring, 900 Services, 1000 @Component
and 1000 @Repository
, the startup application trhow an OutOfMemoryError
. I increased the parameters -Xms1024m -Xmx4g
in Tomcat and Wildfly, in the application I added the default-lazy-init = "false"
parameter in applicationContext.xml
and at a great cost the application is starting. I would like to know if there is any advantage in removing
@Autowired
private AnyService anyService;
from all classes and use
public void execute() {
AnyService anyService = (AnyService)applicationContext.getBean(AnyService.class);
anyService.execute();
}
within the methods. Does using the local variable instead of the instance can optimize the startup? Brings some benefit to the GC? The call applicationContext.getBean(AnyService.class) may worsen performance of methods?
OutOfMemoryError and slow startup are two different question. I think you don't have to remove your @Autowired annotation.
First you should identify which bean cause slow loading (for example db connection), and add @Lazy at that class , you can reference this article spring-lazy-annotation-use-cases . and some bean should load first Make sure a particular bean gets initialized first
Second, use Spring XML configuration instead of component scan or limit component scan scope may help. Make sure only uses the minimally required components of your app
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.