Does annotating every parameter with @Lazy on component constructor equals?


Michiel Haisma:

In Spring, consider a @Serviceclass with the following autowired constructor:

public DogService(@Lazy CatService catService, @Lazy MouseService mouseService) {
  this.catService = catService;
  this.mouseService = mouseService;
}

Does this equal?

@Lazy
public DogService(CatService catService, MouseService mouseService) {
  this.catService = catService;
  this.mouseService = mouseService;
}
David:

Yes, this is equivalent.

The @Lazyjavadoc states:

In addition to its role in component initialization, this annotation can also be placed on injection points marked with org.springframework.beans.factory.annotation.Autowiredor javax.inject.Inject: in this case, it can create a lazy-resolving proxy for all affected dependencies to replace using org.springframework.beans.factory.ObjectFactoryor javax.inject.Provider.

The important parts are:

It causes a lazy solution proxy to be created for all affected dependencies

As far as dependencies go, DogServicein any case your bean has two auto-associations: CatService catServiceand MouseService mouseService.
So annotating the constructor or all parameters separately will yield the same result: both dependencies will be lazy loaded.

Note: I've tested them and the behavior is exactly the same in both cases.

Related


Component constructor parameter fxml javafx

mud First, I give you my code. Components public class Schuiver extends VBox{ private final SchuiverCompanion companion; public Schuiver(String text) { try { FXMLLoader loader = new FXMLLoader( Schuiver.class.getResource("nuleensc

Component constructor parameter fxml javafx

mud First, I give you my code. Element public class Schuiver extends VBox{ private final SchuiverCompanion companion; public Schuiver(String text) { try { FXMLLoader loader = new FXMLLoader( Schuiver.class.getResource("nuleenschui

Component constructor parameter fxml javafx

mud First, I give you my code. Components public class Schuiver extends VBox{ private final SchuiverCompanion companion; public Schuiver(String text) { try { FXMLLoader loader = new FXMLLoader( Schuiver.class.getResource("nuleensc

Preserve constructor when annotating fields

Nason: I have the following class. public class StatusCategory { @JsonProperty("key") private final String m_key = null; public String getKey() { return(m_key); } } What -keepoptions are there to ensure that Proguard doesn't delete the c

Preserve constructor when annotating fields

Nason I have the following class. public class StatusCategory { @JsonProperty("key") private final String m_key = null; public String getKey() { return(m_key); } } What -keepoptions are there to ensure that Proguard doesn't delete the co

Preserve constructor when annotating fields

Nason I have the following class. public class StatusCategory { @JsonProperty("key") private final String m_key = null; public String getKey() { return(m_key); } } What -keepoptions are there to ensure that Proguard doesn't delete the co

Preserve constructor when annotating fields

Nason: I have the following class. public class StatusCategory { @JsonProperty("key") private final String m_key = null; public String getKey() { return(m_key); } } What -keepoptions are there to ensure that Proguard doesn't delete the c

Constructor parameter does not exist exception

username I am trying to inject two objects based on an interface into a class constructor by using Unity. I am currently getting the following error when unit testing: Result message: Test method TestProject.TFStests.Check_Interface_CheckOut_Method threw excep

Custom component in Java does not define constructor

Jmul I'm trying to replace my Jlabels with a custom JComponent called PhotoComponent. So far I have followed all the steps outlined in my instructor video on how to create a component. However, trying to replace the desired Jlabel with the following two errors

Custom component in Java does not define constructor

Jmul I'm trying to replace my Jlabels with a custom JComponent called PhotoComponent. So far I have followed all the steps outlined in my instructor video on how to create a component. However, trying to replace the desired Jlabel with the following two errors

Does ReactJS component class need a constructor

Lambert I am using ReactJS with Typescript. Do I need the "constructor" code below? It works just fine without it, I looked at the javascript for turn heap and it still seems to add it automatically. interface myProps { children?: any; } class MyButton exte

Custom component in Java does not define constructor

Jmul I'm trying to replace my Jlabels with a custom JComponent called PhotoComponent. So far I have followed all the steps outlined in my instructor video on how to create a component. However, trying to replace the desired Jlabel with the following two errors

Does React need a hook for every component?

Hexaquark Problem Description I've run into this problem many times, but not sure what's the best way to do it. Suppose I have more than 3 components, how can I set up a state tracker for each component? Do I need n useState()hooks for components n? In this ex

How does this constructor code take a pointer as a parameter?

Ozymandias The following code has three parameters of type double*. The code is part of the Arduino PID library, and the user usually takes the reading of an analog port or some kind of sensor as the parameter for the "input" item, and some fixed value or user