Java Configuration Flashcards
What does @Configuration do?
It tells spring it has bean-definitions (methods) in the class.
What does @Bean mean
The @Bean annotation is used to indicate that a method instantiates, configures, and initializes a new object to be managed by the Spring IoC container
True or false? The bean name is the same as the @Bean methodName()
True
How can you access a Bean?
get the application context and then .getBean(<Methodname>) and cast it to right reference type.</Methodname>
e.g.
ApplicationContext context = SpringApplication.run(<className>)</className>
<AClassName> className = (<AclassName>) context.getBean(<method/beanName>);
</AclassName></AClassName>
True/False - Spring configuration is completely decoupled from business logic?
True
In what order does the Application Context
- dataSource (the database)
- the repository
- the service
In What environment can you create the spring context?
Basically in any context
e.g.
- Standalone application
- Web application
- Junit test
How can you create the application context from multiple configurations?
When a configuration class become to big you can split it up. You can again combine them by using @Import.
e.g.
@Configuration
@Import({configOne.class, configTwo.class})
public class InfraConfiguaration {
….
}
What is the default scope of a bean?
A singleton (design pattern)
How can you change the scope of the bean?
by adding @Scope to the bean
What are the 4 most used bean scopes?
- Singleton: a single instance is used
- prototype: a new instance is created each time a bean is referenced
- session : a new instance is create once per user session (web app only)
- Request: a new instance is created once per request (webapp only)