Understanding of Bean and @Bean in Spring

annotation

All classes with subclasses and methods or properties should be annotated to register beans in Spring IoC .

We @ XX on Weibo, the other party will see this information first and give you feedback, then in Spring, if you mark an @ symbol, then Spring will come to see and get a Bean or give a bean

 

Using Bean Annotations

That is to use the beans that have been configured in the xml file to complete the assembly of attributes and methods; for example, @Autowired, @Resource, you can obtain beans through byTYPE (@Autowired), byNAME (@Resource);

 

Annotation for registering beans

@Component , @Repository , @Controller , @Service , @Configration These annotations convert the object you want to instantiate into a Bean and put it in the IoC container. When you want to use it, it will be with the above @Autowired , @Resource works together to perfectly assemble objects, properties, and methods.

 

@Bean Principle

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>

Meaning, @Bean explicitly dictates the way to generate a bean managed by the Spring container .

 

When Spring starts, when it detects @Bean, it will inject a bean named after the method name into the container by default.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326343470&siteId=291194637
Recommended