Heritage is slowing the startup of my SPRING BOOT application

Asked

Viewed 120 times

2

I’m working on an automatic text messaging system, in my class hierarchy I have an abstract class at the top called TaskNFSBusiness which sets the rules for messages that are not in real time, it is annotated with @Component as you can see below:

@Component
public abstract class TaskNFSBusiness {

Soon after I have a class that inherits from this class and defines the rules of messages that will be sent in real time its declaration is being made as follows:

public abstract class NFSInstantanea extends TaskNFSBusiness implements PageQueryConstants {

Just below the hierarchy I have 2 classes that inherit from NFSInstantanea and define the rules of instant messaging, they are stated as follows:

@Component
@Qualifier("nfs13")
public class NFS13 extends NFSInstantanea {

@Component
@Qualifier("nfs12")
public class NFS12 extends NFSInstantanea  {

My problem is that these last two classes are increasing Spring boot time too much because they both inherit from NFSInstantanea and both are marked with @Component, this is generating a timeout at the time of uploading the application to the local test server. The strange thing is that if I remove the @Component of the classes or if I make them inherit from TaskNFSBusiness boot time goes back to normal, only I didn’t want to do it because then I wouldn’t be able to instill NFS12 and the NFS13 with @Autowired, and if I put the real-time methods in TaskNFSBusiness I will end up having a series of useless code in messages that are not instant, plus all the conflicts that this would cause.


To make it simpler to understand the hierarchy I’ll leave the modeling right below inserir a descrição da imagem aqui

1 answer

1


To prevent a component from initializing during startup (it got weird to write this), you can use the annotation @Lazy as follows:

@Lazy
@Component
@Qualifier("nfs13")
public class NFS13 extends NFSInstantanea {
...
}

To work, you must inject it using obligatory @Autowired as follows:

@Autowired
@Qualifier("nfs13")
private NFSInstantanea minhaInstancia;

Or declaring as a @Bean:

@Lazy
@Bean
public NFSInstantanea nfs13() {
    return new NFS13();
}

More information on documentation


Edit:

I forgot to mention, problems during application startup may be related to poorly optimized routines, which are executed during instance creation.

Optimizing routines is highly recommended, rather than "using a technical outline" to make the application work.

  • I managed to get the performance back by annotating the class NFSInstantanea with @Component. for some reason at Spring (which I don’t know how to say) when announcing the parent class with @Component children also need to be noted (even if this class does not involve anything within it, as in the case of NFSInstantanea) and then the boot performance is stable. only that honestly it was smelling me to gambiarra, I will test this your method and if it works keep it in my implementation and I will mark the question as answered, thank you.

  • 1

    performed the tests and your method actually works @nullptr

Browser other questions tagged

You are not signed in. Login or sign up in order to post.