๐SPRING: Annotation-based (@Component, @Autowired ...) configuration versus Java's configuration(@Configuration + @Beans) ?
๐SPRING: Annotation-based (@Component, @Autowired ...) configuration versus Java's configuration(@Configuration + @Beans) ?
By Josh Long, Java Champion, and Spring Developer Advocate.
Transcript:
Vincent asked:
"๐ซ๐ ๐ฐ ๐๐๐๐๐๐๐๐๐ ๐จ๐๐๐๐๐๐๐๐๐-๐๐๐๐๐ ๐๐๐๐๐๐๐๐๐๐๐๐๐ (๐๐๐๐ @๐ช๐๐๐๐๐๐๐๐, @๐จ๐๐๐๐๐๐๐๐ ...) ๐๐ ๐ฑ๐๐๐'๐ ๐๐๐๐๐๐๐๐๐๐๐๐๐ (๐๐๐๐ @๐ช๐๐๐๐๐๐๐๐๐๐๐๐, ๐๐๐ ๐๐๐ ๐๐๐ @๐ฉ๐๐๐ ๐๐ ๐๐)?"
Humm, for your own components, ๐ฒ๐จ๐ฎ ๐ฌ๐ก๐จ๐ฎ๐ฅ๐ ๐๐ฅ๐ฆ๐จ๐ฌ๐ญ ๐ง๐๐ฏ๐๐ซ ๐ฎ๐ฌ๐ @๐๐ฎ๐ญ๐จ๐ฐ๐ข๐ซ๐๐, maybe in your tests, that's it!
There's no reason for it anymore. You don't need it, you could have a constructor which defines parameters of types that are in your context, and Spring might call that and satisfy those parameters.
So there is no reason for @Autowired.
Definitely, don't do it on fields, @Autowired fields is hack and disgusting! ๐Don't do that.
But ๐๐จ๐ซ ๐ฒ๐จ๐ฎ๐ซ ๐จ๐ฐ๐ง ๐๐จ๐ฆ๐ฉ๐จ๐ง๐๐ง๐ญ๐ฌ, ๐ฅ๐ข๐ค๐ ๐ฒ๐จ๐ฎ๐ซ ๐๐ฎ๐ฌ๐ญ๐จ๐ฆ ๐ฌ๐๐ซ๐ฏ๐ข๐๐๐ฌ, ๐ฒ๐จ๐ฎ๐ซ ๐ซ๐๐ฉ๐จ๐ฌ๐ข๐ญ๐จ๐ซ๐ข๐๐ฌ, ๐๐ง๐ ๐ฐ๐ก๐๐ญ๐๐ฏ๐๐ซ, ๐ฎ๐ฌ๐ @๐๐๐ซ๐ฏ๐ข๐๐, ๐ฎ๐ฌ๐ @๐๐จ๐ฆ๐ฉ๐จ๐ง๐๐ง๐ญ, @๐๐๐ฉ๐จ๐ฌ๐ข๐ญ๐จ๐ซ๐ฒ ๐ฐ๐ข๐ญ๐ก ๐ข๐ฆ๐ฉ๐ฎ๐ง๐ข๐ญ๐ฒ.
๐๐ฎ๐ญ ๐๐จ๐ซ ๐ข๐ง๐๐ซ๐๐ฌ๐ญ๐ซ๐ฎ๐๐ญ๐ฎ๐ซ๐, ๐ฅ๐ข๐ค๐ ๐ฒ๐จ๐ฎ๐ซ ๐๐๐ญ๐ ๐ฌ๐จ๐ฎ๐ซ๐๐๐ฌ, ๐๐ง๐ ๐ฒ๐จ๐ฎ๐ซ ๐ญ๐๐ฆ๐ฉ๐ฅ๐๐ญ๐๐ฌ, ๐๐ง๐ ๐ฐ๐ก๐๐ญ๐๐ฏ๐๐ซ, ๐๐ง๐ ๐ฌ๐จ๐ฆ๐๐ญ๐ก๐ข๐ง๐ ๐ฒ๐จ๐ฎ ๐๐จ๐ง'๐ญ ๐จ๐ฐ๐ง ๐ญ๐ก๐ ๐๐จ๐๐ ๐ฒ๐จ๐ฎ ๐๐๐ง ๐๐ก๐๐ง๐ ๐, ๐ญ๐ก๐๐ง ๐ฎ๐ฌ๐ @๐๐๐๐ง.
Right! And make it match, you can inject things fine in your @Service, @Component, you can inject it in your bean methods.
And vice versa, you can inject the constructors of your @Service components, using @Bean.
#spring #configuration #java #youtube