Talking Points

The following is a collection of talking points that can be covered in the reference manual.

[email protected] method calls are not supported in lite mode. Instead, when one @Bean-method invokes another local @Bean-method, the invocation is a natural Java direct method invocation since Spring does not intercept the invocation. This is analogous to [email protected] method calls where in proxy mode, Spring does not intercept them – Spring does so only in AspectJ mode.

@Beanlite mode is typically sufficient for straight factory methods that simply do not need any inter-bean references, possibly because they are creating leaf components, possibly because the returned component instances use @Inject-based dependency injection themselves anyway. However, Spring does respect the scoping semantics (e.g., singleton and prototype) for container-driven @Bean factory method calls. Furthermore, Spring also respects lifecycle callbacks for a bean created via an @Bean-method in lite mode.

The reference manual should not only discuss the above talking points, but it should also mention some of the undesirable side effects that may occur when naïvely using @Bean-methods in lite mode – for example, the issues encountered in SPR-9051.

Related Documentation

@Bean Javadoc

/**

* <h3>{@code @Bean} <em>Lite</em> Mode</h3>

*

* <p>{@code @Bean} methods may also be declared within classes that are <em>not</em>

* annotated with {@code @Configuration}. For example, bean methods may be declared

* in a {@code @Component} class or even in a <em>plain old class</em>. In such cases,

* a {@code @Bean} method will get processed in a so-called <em>'lite'</em> mode.

*

* <p>Bean methods in <em>lite</em> mode will be treated as plain <em>factory

* methods</em> by the container (similar to {@code factory-method} declarations

* in XML), with scoping and lifecycle callbacks properly applied. The containing

* class remains unmodified in this case, and there are no unusual constraints for

* the containing class or the factory methods.

*

* <p>In contrast to the semantics for bean methods in {@code @Configuration} classes,

* <em>'inter-bean references'</em> are not supported in <em>lite</em> mode. Instead,

* when one {@code @Bean}-method invokes another {@code @Bean}-method in <em>lite</em>

* mode, the invocation is a standard Java method invocation; Spring does not intercept

* the invocation via a CGLIB proxy. This is analogous to inter-{@code @Transactional}

* method calls where in proxy mode, Spring does not intercept the invocation &mdash;

* Spring does so only in AspectJ mode.

*/

@ContextConfiguration Javadoc

/**

* <h3>Annotated Classes</h3>

*

* <p>The term <em>annotated class</em> can refer to any of the following.

Just read through the ref docs and knowing this behaviour, it does stand out as a gotcha in that @Bean would seem to work but without lifecycle. Given timelines, if this is to slip to next release, maybe a short update to the ref docs linking to where @Bean Javadoc covers this would be sufficient..?

Neale Upstone
added a comment - 11/Mar/13 4:45 AM Just read through the ref docs and knowing this behaviour, it does stand out as a gotcha in that @Bean would seem to work but without lifecycle. Given timelines, if this is to slip to next release, maybe a short update to the ref docs linking to where @Bean Javadoc covers this would be sufficient..?