In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What this article shares with you is about the difference between @ Configuration and @ Component as a configuration class. The editor thinks it is very practical, so I share it with you. I hope you can learn something after reading this article. Let's take a look at it with the editor.
@ Configuration annotated class:
/ * * @ Description Test configuration Class * @ Author Brother * @ CreateTime 14:35 on 2019-6-18 * / @ Configurationpublic class MyBeanConfig {@ Bean public Country country () {return new Country ();} @ Bean public UserInfo userInfo () {return new UserInfo (country ());}}
@ Component annotated class:
/ * * @ Description Test configuration Class * @ Author Brother * @ CreateTime 14:36 on 2019-6-18 * / @ Componentpublic class MyBeanConfig {@ Bean public Country country () {return new Country ();} @ Bean public UserInfo userInfo () {return new UserInfo (country ());}}
Test:
@ RunWith (SpringRunner.class) @ SpringBootTestpublic class DemoTest {@ Autowired private Country country; @ Autowired private UserInfo userInfo; @ Testpublic void myTest () {boolean result = userInfo.getCountry () = country; System.out.println (result? "one country": "different country";}}
If it is printed by @ Configuration, it is the same country,@Component but a different country. Why?
@ Target ({ElementType.TYPE}) @ Retention (RetentionPolicy.RUNTIME) @ Documented@Componentpublic @ interface Configuration {@ AliasFor (annotation = Component.class) String value () default ";}
If you click on @ Configuration, you will find that it is also modified by @ Component, so either context:component-scan/ or @ ComponentScan can handle the classes annotated by @ Configuration.
The @ Configuration tagged class must meet the following requirements:
The configuration class must be provided as a class (not an instance returned by the factory method), which allows enhancement at run time by generating subclasses (cglib dynamic proxy).
The configuration class cannot be a final class (cannot be dynamically proxied).
Configuration annotations are usually used to generate Spring container-managed classes through @ Bean annotations
The configuration class must be non-local (that is, it cannot be declared in the method, it cannot be private).
Any nested configuration class must be declared as static.
The @ Bean method may not in turn create a further configuration class (that is, the returned bean if with
@ Configuration will not be treated as a special treatment, only as a normal bean).
But when the spring container starts, it has a class that specifically deals with @ Configuration, which enhances the @ Configuration-modified class cglib dynamic proxy, which is part of the reason why @ Configuration needs to meet the above requirements, and what exactly will it enhance?
Here is the idea of personal arrangement. If there is anything wrong, please give me some advice.
Country () is called in userInfo (), because it is the method that must country () to generate a new new contry (), so the dynamic proxy addition will judge it. If the method called in userInfo still has the @ Bean modifier, it will directly call the country instance in the spring container instead of country (), which must be an object, because the bean in the spring container is a singleton by default. Do not understand, such as bean configured by xml
Here scope is a singleton by default.
The above is a personal understanding. For more information on source code analysis, please see https://www.jb51.net/article/153430.htm.
But if I just want to use @ Component, what if classes without @ Component don't have dynamic proxies?
/ * * @ Description Test configuration Class * @ Author Brother * @ CreateTime 14:36 on 2019-6-18 * / @ Componentpublic class MyBeanConfig {@ Autowired private Country country; @ Bean public Country country () {return new Country ();} @ Bean public UserInfo userInfo () {return new UserInfo (country);}}
This ensures that it is the same Country instance.
This is the difference between @ Configuration and @ Component as a configuration class. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.