After studying about @Bean, I wondered if @Bean's class generally covers classes with fields and gettersetters.If so, what are the benefits of declaring an object without using @Bean as shown in the code below?
TestForm testForm=newTestForm();
testForm.setHp("123");
testForm.setName("Tanaka");
After studying about @Bean, I wondered if @Bean's class generally covers classes with fields and gettersetters.
No. If anything, I think something like POJO is less likely to be generated by @Bean
.
The reason for this is that, as the question says, it's no different to new
yourself (on the contrary, the more DI containers are involved, the more complex the structure is).
If there are specific examples of the ideas mentioned in the questionnaire, I think I can answer them more specifically.
If you give @Bean to create an object, it is using the DI container mechanism of spring.
DI containers are typically used for classes such as FormRegistrationService, which have only one instance and only one field of content, rather than for objects representing values as shown in the example.
In that case, I think it would be better to inject the constructor as shown below instead of releasing the field.
Example:
class FormRegistrationService{
private CharacterDao characterDao;
publicFormRegistrationService(CharacterDao){
This.characterDao =characterDao;
}
public void registerCharacter(String name, Stringhp) {
// Implementation code omitted
// You can use characterDao internally.
// Data are registered.
}
}
© 2024 OneMinuteCode. All rights reserved.