java / beginner
Snippet
Defining a Bean with @Bean
In Spring, a 'Bean' is an object managed by the Spring IoC container. By using @Configuration and @Bean, you manually define how a specific object should be created and configured.
snippet.java
1
2
3
4
5
6
7
@Configurationpublic class AppConfig {@Beanpublic MyService myService() {return new MyService();}}
spring
Breakdown
1
@Configuration
Marks the class as a source of bean definitions for the application context.
2
@Bean
Indicates that this method produces a bean to be managed by the Spring container.