Skip to content

Expand explanation for the "lite" bean approach #29957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
manueljordan opened this issue Feb 10, 2023 · 1 comment
Closed

Expand explanation for the "lite" bean approach #29957

manueljordan opened this issue Feb 10, 2023 · 1 comment
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task
Milestone

Comments

@manueljordan
Copy link

In the Basic Concepts: @Bean and @Configuration section exists the following Full @Configuration vs “lite” @Bean mode? block with the following first paragraph:

When @Bean methods are declared within classes that are not annotated with @Configuration, 
they are referred to as being processed in a “lite” mode. Bean methods declared in a @Component or
even in a plain old class are considered to be “lite”, with a different primary purpose of the 
containing class and a @Bean method being a sort of bonus there. For example, service components may 
expose management views to the container through an additional @Bean method on each applicable 
component class. In such scenarios, @Bean methods are a general-purpose factory method mechanism.

... more

That big block does not contain none information about:

  1. Why the lite bean approach exists?
  2. When the lite bean approach is mandatory over the full approach? (Here should appear the differences between them)

Now about sample/snippet code:

The full approach is:

@Configuration
class XConfig {

   @Bean
   X defineX(){
      ...
      return x;
   }

}

It is the classic and most known approach. About the lite approach, first its introduction:

Introduction

When @Bean methods are declared within classes that are not annotated with @Configuration

And according with the big block shown above exists two ways

One

Bean methods declared in a @Component

Therefore

@Component/@Service
class XSomething {

   ...

   @Bean
   X defineX(){
      ...
      return x;
   }

}

Until here the XSomething class can be detected and managed by Spring thanks to the combination of @ComponentScan together with @Component/@Service, therefore @Bean can be used by Spring too.

Two

or even in a plain old class

Here I am assuming the following:

//No annotation
class XSomething {

   ...

   @Bean
   X defineX(){
      ...
      return x;
   }

}

Therefore the current documentation does not cover "two" through some snippet code the answer of the following question:

  1. How does Spring apply here the "lite" bean approach through a "plain old class"?

If the @Bean is totally isolated or is not declared with neither @Configuration nor @Component. So How @Bean can be used by Spring?

Please clarify two because is not clear in the current documentation - there is neither a snippet nor sample code about this approach, perhaps I misinterpreted the or even in a plain old class part.

I already wrote a question on SO about the question 3 at:

But I think is valuable for the community put in that block - or create a dedicated section - explaining these 3 questions together.

Thanks in advance for your understanding.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 10, 2023
@rstoyanchev rstoyanchev added type: documentation A documentation task in: core Issues in core modules (aop, beans, core, context, expression) labels Feb 14, 2023
@rstoyanchev rstoyanchev changed the title Documentation: Expand explanation for the "lite" bean approach Expand explanation for the "lite" bean approach Feb 14, 2023
@snicoll
Copy link
Member

snicoll commented Oct 3, 2023

perhaps I misinterpreted the or even in a plain old class part.

Yes. The purpose of plain old class was to refer to the fact that the target class does not need to be annotated with @Component as long as it is a bean. That bit could have been a bit more explicit but, on the other hand, the container does not do anything with "plain class". If you don't register it one way or the other (@Bean, @Import, class path scanning, programmatic registration, etc), then we won't touch it at all. If it's not a bean, it does not exist for the purpose of configuring the context.

The section is pretty explicit that lite mode is a "bonus" and has less features than regular configuration classes. I'll see what we can do to revisit that section to make it more obvious.

@snicoll snicoll removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 3, 2023
@snicoll snicoll added this to the 6.1.x milestone Oct 3, 2023
@snicoll snicoll self-assigned this Oct 24, 2023
@snicoll snicoll modified the milestones: 6.1.x, 6.1.0-RC2 Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

4 participants