@Bean: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
* https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Bean.html
=Internal=
=Internal=


* [[Spring_Dependency_Injection_and_Inversion_of_Control_Container_Concepts#Beans|Spring Dependency Injection and Inversion of Control Container Concepts]]
* [[Spring_Dependency_Injection_and_Inversion_of_Control_Container_Concepts#.40Configuration.2F.40Bean|Spring Dependency Injection and Inversion of Control Container Concepts]]


=Overview=
=Overview=


The @Bean annotation indicates that the object returned by the methods it annotates are [[Spring_Dependency_Injection_and_Inversion_of_Control_Container_Concepts#Beans|beans]] and they should be added to the [[Spring_Dependency_Injection_and_Inversion_of_Control_Container_Concepts#Application_Context|application context]]. By default, their respective [[Spring_Dependency_Injection_and_Inversion_of_Control_Container_Concepts#Bean_ID|bean IDs]] will be the same as the names of the methods that define them.
{{Internal|Spring_Dependency_Injection_and_Inversion_of_Control_Container_Concepts#.40Configuration.2F.40Bean|@Bean in Spring Dependency Injection and Inversion of Control Container Concepts}}
 
=Register a Bean under a Specific Name=
 
The following registers a MyComponent singleton under the name "blue":
 
<syntaxhighlight lang='java'>
@Configuration
public class ... {
 
    @Bean("blue")
    public MyComponent getMyComponent() {
      return new MyComponent();
    }
}
</syntaxhighlight>

Latest revision as of 01:35, 6 December 2018

External

Internal

Overview

@Bean in Spring Dependency Injection and Inversion of Control Container Concepts

Register a Bean under a Specific Name

The following registers a MyComponent singleton under the name "blue":

@Configuration
public class ... {

    @Bean("blue")
    public MyComponent getMyComponent() {
      return new MyComponent();
    }
}