Skip to content

Expand explanation for the "lite" bean approach #29957

@manueljordan

Description

@manueljordan

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.

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: documentationA documentation task

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions