简体   繁体   English

spring 需要了解@Bean 的“lite”模式

[英]Need to know about "lite" mode of @Bean in spring

Full @Configuration vs “lite” @Bean mode?完全@Configuration 与“精简版”@Bean 模式? In Spring documentation they have mentioned something about @Bean annotated method without @Configuration annotated class.在 Spring 文档中,他们提到了一些关于 @Bean 注释方法而没有 @Configuration 注释类的内容。 Can anyone please help me to get perfect example and where can it useful(in which case)?谁能帮我找到完美的例子,它在哪里有用(在这种情况下)?

https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-standard-annotations (Concept: 1.12.1) https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-standard-annotations (概念:1.12.1)

When a @Bean method is declared within a @Component annotated class or a plain class, it is not proxied and behaves as a normal java method - so it does not return an instance from the spring container but rather creates a new instance.当 @Bean 方法在 @Component 注释的类或普通类中声明时,它不会被代理并且表现为普通的 java 方法 - 因此它不会从 spring 容器返回实例,而是创建一个新实例。

This article contains an explanation with examples - https://irena-sayv.medium.com/spring-bean-annotation-what-how-and-why-24f7abe20f83本文包含示例说明 - https://irena-sayv.medium.com/spring-bean-annotation-what-how-and-why-24f7abe20f83

It is not useful and should be avoided.它没有用,应该避免。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM