Understanding of beans in Spring and the role of @beans

Bean s are ubiquitous in Spring and SpringMVC. It is important to internalize this concept. Here are my thoughts:


1, What is Bean

1. Java is object-oriented. Objects have methods and attributes, so object instances are required to call methods and attributes (that is, instantiation);


2. All classes with methods or attributes need to be instantiated, so that they can be visualized to use these methods and attributes;


3. Rule: all subclasses and classes with methods or attributes should be annotated to register beans to Spring IoC;


4. Understand a Bean as a proxy or spokesman 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. In Spring, if you identify an @ symbol, Spring will look at it and get a Bean or give a Bean from here

2, 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 through 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.


3, What is Bean?


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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 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, and what method - generate a Bean method and hand it over to the Spring container for management; From this, we can see why @Bean is placed on the method annotation, because it clearly tells the annotated method that you will generate a Bean for me, and then give it to the Spring container. Leave the rest

2. Remember that @Bean is placed on the method, which is to generate a Bean. Are you confused again, because you have added @configuration and other annotations to the classes you define to register beans? Why use @Bean? I don't know. Let me give you an example and discuss it together:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 package com.edu.fruit;   //Define an interface     public interface Fruit<T>{         //No method }   /* *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 the @Bean above, return                                                           Is a Apple Class instance object               } }

The above example also confirms the above summary:

1. All subclasses and classes with attributes and methods are registered in Spring and managed by it;

2. @Bean is used in the method to tell the Spring container that you can get a Bean from the following method

Posted by nootkan on Tue, 31 May 2022 13:42:17 +0530