The understanding of bean and @bean in spring

Source: Internet
Author: User

Beans are ubiquitous in spring and SPRINGMVC, it's important to put this concept into context, and here's what I think:

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 to convert the object you want to instantiate into a bean, Put in the IOC container, and when you want to use it, it will be with the above @autowired, @Resource together, the object, properties, methods of the perfect assembly.

Iii. What is @Bean?

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

Indicates that a method produces a bean to being managed by the Spring container.   

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:

Package com.edu.fruit;     Define an interface public interface fruit<t>{//No method}/** define two subclass */package Com.edu.fruit;     @Configuration public class Apple implements fruit<integer>{//to constrain the Apple class to an Integer type}package com.edu.fruit; @Configuration public class Ginseng implements fruit<string>{//constrains the ginseng class to the String type}/** business logic class */package com.edu.       Service          @Configuration public class Fruitservice {@Autowired private Apple Apple;    @Autowired Private Ginseng Ginseng; Defines a method that generates a Bean @Bean (name= "getapple") public fruit<?> getapple () {System.out.println (APPLE.GETCLA         SS (). GetName (). hashcode);       System.out.println (Ginseng.getclass (). GetName (). hashcode); return new Apple ();}} /** Test class */@RunWith (Blockjunit4classrunner.class) public class Config {public Config () {Super ("Classpath: Spring-fruit.xml ");}                         @Testpublic void Test () {Super.getbean ("getapple");//Where the bean comes from, from the method below @bean, to return                                 is an Apple class instance object}} 

The above example also confirms what I have summed up above:

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

The understanding of bean and @bean in spring

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.