Spring Framework Flashcards
What is Spring?
Spring is a framework for building Java applications
How can a configuration class import another configuration class?
@Import({a.class, b.class})
What is the default scope of a bean?
Singleton
How can you change the scope of a bean?
@Scope(...)
Describe how a prototype bean works
A new bean is created anytime it is referenced
How can you autowire a dependency only if it exists?
@Autowired(required=false)
For non-Spring Boot applications, if @Component is used, what other annotation must be used on the configuration class?
@ComponentScan(…)
What problem occurs in the following code and how can you fix it?
@Component("myService") class MyServiceImpl implements MyService { @Autowired MyServiceImpl(MyRepository myRepository) {...} } --------------------------------------------------------------------------- @Component("jdbcMyRepository") class JdbcMyRepository implements MyRepository {...} --------------------------------------------------------------------------- @Component("jpaMyRepository") class JpaMyRepository implements MyRepository {...}
Ambiguity problem since MyServiceImpl(MyRepository ...)
does not know which implementation to use. This can be solved by:
MyServiceImpl(@Qualifier("jpa") MyRepository myRepository) {...} ----------------------------------------------------------------------------------------- @Component("jpaMyRepository") @Qualifier("jpa") // or @Primary class JpaMyRepository implements MyRepository {...}
- Where can @Value be used?
- Is @Value implemented by BeanFactoryPostProcessor or BeanPostProcessor?
- Fields and parameters
- Both, its value is resolved by BeanFactoryPostProcessor and injected by BeanPostProcessor for fields and setter methods
- What is the difference between standard beans and @Lazy beans?
- Standard beans are instantiated eagerly at application startup and @Lazy beans are instantiated only when they are referenced (e.g. ApplicationContext.getBean(…) is used)
What are the 6 stereotype annotations?
- @Component
- @Configuration
- @Controller
- @RestController
- @Repository
- @Service
True or false:
ApplicationContext is a subtype of BeanFactory
True
Describe the 5 different phases of a Spring bean’s lifecycle
- Load and process bean definitions through BeanFactoryPostProcessor
- Instantiate beans
- Initialize bean through BeanPostProcessor
- Use beans through @Autowired
- Destroy beans through ApplicationContext.close()
- When using a method to define a bean, how is its type and name determined?
- When using a class to define a bean, how is its type and name determined?
- Type = method return type
Name = method name - Type = class
Name = class name
The Environment bean has access to the 3 following property sources…
- JVM system properties
- System environment properties
- Property files (if appropriate configuration is used)
What are the 2 different ways to access property values inside a configuration class?
@PropertySource(...) @Configuration public class MyConfig {...}
- Environment Bean
- @Value(…)
When might you need to use @PropertySource(…)?
When you need to access the properties of a file (Spring Framework)
When you need to access the properties of a file that is not located in application.properties (Spring Boot)