The understanding of beans in spring and the role of @bean

Source: Internet
Author: User

First, what is a bean?

1, Java object-oriented, the object has methods and properties, then you need an object instance to invoke methods and properties (i.e. instantiation);

2. All classes that have methods or attributes need to be instantiated to use these methods and attributes in order to be figurative;

3. Rule: All subclasses and classes with methods or attributes are added to the annotations of the registered bean to spring IOC;

4, the bean is understood as the agent or spokesperson of the class (in fact, it is implemented by reflection, proxy), so that it can represent the class has the possession of the thing

5, we are on the Weibo @ over xxx, the other party will first see this message, and give you feedback, then in spring, you identify an @ symbol, then spring will come to see, and get a bean from here or give a bean

Second, the annotations are divided into two categories:

1, one kind is uses the bean, namely is already in the XML file to configure the bean to use, completes the property, the method assembly, for example @autowired, @Resource, May through Bytype (@Autowired), ByName (@Resource) way to get the bean;

2, a class is registered Bean, @Component, @Repository, @ Controller, @Service, @Configration These annotations are the objects you want to instantiate into a bean, placed in the IOC container, when you need to use , it will @autowired with the above, @Resource together, the object, properties, methods to assemble the perfect.

Iii. What is @Bean?

1. What is the principle? First look at the source of some of the content:

1 indicates that a method produces a bean to being managed by the Spring container.2  34  5<p>the names and semantics of the attributes to Thisannotation is intentionally6Similar to those of the {@code <bean/>} element in the Spring XML schema. for7 Example:8  9<preclass= "Code" >Ten @Bean One       PublicMybean Mybean () { A          //Instantiate and configure Mybean obj -          returnobj; -}</pre>

It means that @bean clearly indicates a method, what method--a method of generating a bean, and handing it over to the spring container management; From here we understand why @bean is on the annotation of the method, because it tells the annotated method very clearly, you give me a bean, And then hand it over to the spring container, and you leave the rest.

2, remember, @Bean put on the method, is to produce a bean, then you are not confused, because already in your definition of the class added @configration and other registered bean annotations, why also use @bean it? I do not know this, let me give an example, let's discuss it together:

1  PackageCom.edu.fruit;2   //define an interface3      Public InterfaceFruit<t>{4         //No way5 }6  7 /*8 * Definition of two sub-classes9 */Ten  PackageCom.edu.fruit; One @Configuration A       Public classAppleImplementsfruit<integer>{//constrain an Apple class to an integer type -   - } the   -  PackageCom.edu.fruit; - @Configuration -       Public classGinsengImplementsfruit<string>{//constrain the Ginseng class to a string type +   - } + /* A * Business Logic class at */ -  PackageCom.edu.service; - @Configuration -         Public classFruitservice { - @Autowired -           PrivateApple Apple; in @Autowired -           PrivateGinseng Ginseng; to     //define a method for generating beans +@Bean (name= "Getapple") -         PublicFruit<?>getapple () { the System.out.println (Apple.getclass (). GetName (). hashcode); * System.out.println (Ginseng.getclass (). GetName (). hashcode); $        return NewApple ();Panax Notoginseng } - } the /* + * Test Class A */ the@RunWith (Blockjunit4classrunner.class) +  Public classConfig { -      PublicConfig () { $         Super("Classpath:spring-fruit.xml"); $     } - @Test -      Public voidTest () { the         Super. Getbean ("Getapple");//where does this bean come from, from the method below @bean, to return - is an Apple class instance objectWuyi           the     } -}

Summarize:

1. All subclasses and classes with attributes and methods are registered beans to spring and administered by them;

2, @Bean used in the method, tell the spring container, you can get a Bean from the following method

Spring helps us to manage beans in two parts, one for registering beans and one for assembling beans. There are three ways to accomplish these two actions, one is to use automatic configuration, one is to use Javaconfig, and the other is to use the XML configuration method. In the auto-configuration mode, use @component to tell Spring that I am a bean, you want to manage me, and then use the @autowired annotations to assemble the bean (the so-called assembly is the direct collaboration of the management object). Then in Javaconfig, the @Configuration is actually telling the spring,spring how to configure the container (how to register the bean and how to handle the relationship between the beans (assembly)). Well, it's good to understand, @Bean means that when I'm going to get this bean, you spring is going to help me get to this bean in this way. The same goes for the way XML is used. The <bean> tag is to tell spring how to get the bean, and the various <ref> is the manual configuration bean relationship.

Reprinted from: https://www.cnblogs.com/bossen/p/5824067.html

The understanding of beans in spring and the role of @bean

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.