Beans and @ Bean s in spring

Posted by brainardp on Tue, 08 Feb 2022 06:16:55 +0100

I Spring bean definition

  the objects managed by the Spring IoC container are called beans. Beans are instantiated, assembled, and managed through the Spring IoC container. The bean configuration information defines the implementation and dependencies of beans. The Spring container establishes the bean definition registry inside the container according to various forms of bean configuration information, then loads and instantiates beans according to the registry, and establishes the dependencies between beans and beans. Finally, these ready beans are put into the bean cache pool for external applications to call.

Beans are spring managed objects

II Understanding of bean s

1.Java is object-oriented. If an object has methods and properties, you need an object instance to call methods and properties (i.e. instantiation);

2. All classes with methods or attributes need to be instantiated in order to visualize the use of these methods and attributes;

All subclasses or methods with IOC class annotation should be added to IOC class;

4. Understand Bean as the agent or spokesperson of a class (in fact, it is implemented through reflection and proxy), so that it can represent that the class has what it should have

5. We have all @ so and so on the microblog. The other party will give priority to seeing this information and give you feedback. Then in Spring, you identify an @ symbol, and Spring will come and see it, and get a Bean or give a Bean from here.

III Annotations fall into two categories

1. One is to use beans, that is, to use beans already configured in xml files to complete the assembly of attributes and methods; For example, @ Autowired, @ Resource can obtain beans by byTYPE (@ Autowired) and byNAME (@ Resource);

2. One is to register beans, @ component, @ repository, @ controller, @ service, @ configuration. These annotations convert the object you want to instantiate into a Bean and put it in the IoC container. When you need to use it, it will be combined with @ Autowired and @ resource above to perfectly assemble objects, attributes and methods.

IV@ What is Bean?

1. What is the principle? Let's take a look at some of the contents of the source code:

Indicates that a method produces a bean to be managed by the Spring container.
 
 <h3>Overview</h3>
 
 <p>The names and semantics of the attributes to this annotation are intentionally
 similar to those of the {@code <bean/>} element in the Spring XML schema. For
 example:
 
 <pre class="code">
     @Bean
     public MyBean myBean() {
         // instantiate and configure MyBean obj
         return obj;
    }</pre>

   it means that @ Bean clearly indicates a method. What method is it - the method of generating a Bean and handing it over to the Spring container for management; From this, we can see why @ Bean is placed on the annotation of the method, because it clearly tells the annotated method that you generate a Bean for me and give it to the Spring container, and leave the rest.

2. Remember, @ Bean is placed on the method to generate a Bean. Are you confused because @ configuration and other annotations for registered beans have been added to the class you define? Why use @ Bean? I don't know about this either. Let me give you an example and discuss it together:

package com.edu.fruit;
  //Define an interface
    public interface Fruit<T>{
        //No way
}
 
/*
*Define two subclasses
*/
package com.edu.fruit;
     @Configuration
     public class Apple implements Fruit<Integer>{//Constraining an Apple class to an Integer type
 
}
 
package com.edu.fruit;
     @Configuration
     public class GinSeng implements Fruit<String>{//Constraining the GinSeng class to a String type
 
}
/*
*Business logic class
*/
package com.edu.service;
       @Configuration
       public class FruitService {
          @Autowired
          private Apple apple;
          @Autowired
          private GinSeng ginseng;
    //Define a method to generate beans
       @Bean(name="getApple")
       public Fruit<?> getApple(){
       System.out.println(apple.getClass().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");
    }
    @Test
    public void test(){
        super.getBean("getApple");//Where does this Bean come from? From the methods below @ Bean above, it returns an Apple class instance object
    }
}

The above example also confirms the content of my above summary:

1. All subclasses and classes with attributes and methods are registered in Spring and managed by Bean;
2.@Bean is used in the method. Tell the Spring container that you can get a Bean from the following method

Topics: Java Back-end