It’s a good practice to have POJO settings class as a bean?

huangapple go评论71阅读模式
英文:

Is it a good practice to have POJO settings class as a bean?

问题

  1. 每次调用需要它的方法时创建这个POJO。
  2. 将其设置为单例bean,创建一次,然后自动装配它。
英文:

I have a simple POJO class with few fields. This POJO serves the role of settings and it is passed to one method which actually executes some logic. Fields from this POJO are taken from the app.properties and they will not change.
What is a better pratice?

  1. Create this POJO every time we want to call the method which needs it.
  2. Make it a singleton bean, create it once and then autowire it?

答案1

得分: 2

Spring Boot提供了 @ConfigurationProperties 机制 来自动化这个过程。通常情况下,最好避免直接依赖于 MyServiceProperties,而是在 @Bean 方法中进行注入,但 MyServiceProperties 实例可作为上下文Bean使用。

英文:

Spring Boot provides the @ConfigurationProperties mechanism specifically to automate this for you. In general, it's best to avoid direct dependencies on MyServiceProperties and to do the injection in an @Bean method, but the MyServiceProperties instance is available as a context bean.

huangapple
  • 本文由 发表于 2020年8月13日 22:48:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63397645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定