Return to site

๐ŸƒSPRING: Annotation-based (@Component, @Autowired ...) configuration versus Java's configuration(@Configuration + @Beans) ?

ยท java,spring,video

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