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)
True or False
@Profile(…) can be used on both bean class and bean method definitions
True
What is a bean?
A bean is an object managed by the Spring IOC container (ApplicationContext)
Should a bean method definition return an interface type or a class type?
Interface type
Always remember, “depend on abstractions, not concretions”
What is a Project Object Model?
POM is an xml file containing all the information Maven needs to build an application
BeanPostProcessor
- What does it do?
- How can you implement your own?
- BeanPostProcessor can modify an instantiated bean before and/or after initialization
- Define your own BeanPostProcessor bean class
Which bean is responsible for creating bean proxies?
BeanPostProcessor
What does @Autowired do?
Injects dependencies by type
Describe how “Lite” mode works
@Configuration(proxyBeanMethods = false)
Lite mode is when a configuration class is not CGLIB proxied and, therefore, bean method definitions cannot call other bean method definitions to facilitate dependency injection. Normal dependency injection must be conducted
@ComponentScan
Should you be as specific as possible when defining which packages to scan for?
Yes. This will avoid unnecessary scanning
@PostConstruct and @PreDestroy
- What access modifier can be used?
- What must they return?
- What parameters must they define?
@Component public class MyBean { @PostConstruct [any] void init() {...} @PreDestroy [any] void cleanup() {...} }
@PostConstruct and @PreDestroy
When do they execute?
- @PostConstruct executes at startup after all dependencies are injected
- @PreDestroy executes at shutdown prior to bean destruction
True or False
An ApplicationContext cannot be created in any environment
False
True or False
Singleton beans should be used as stateful beans and prototype beans should be used as stateless beans
False
When a singleton bean uses a prototype bean as a dependency, what is the behavior of that prototype bean?
Only a single instance of the prototype bean will ever be created
What are the 2 primary bean scopes?
- Singleton = 1 instance
- Prototype = new instance per use
True or False
Bean overriding can occur in any environment
False. Only in test environments
Is @Configuration
CGLIB or JDK proxied?
CGLIB proxied. This is because CGLIB is used for classes that don’t implement an interface
Create a custom annotation composed of both @Service and @Transactional
@Target(ElementType.TYPE)
@RetentionPolicy(RetentionPolicy.RUNTIME)
@Transactional(timeout=60)
@Service
public @interface MyTransactionalService {…}
What does the following code mean?
@Value("${user.age : 18")
If the user.age property does not have a value, use 18 as default
What are the 3 different ways to implement bean post-construct behavior?
- @Bean(initMethod=”methodName”)
- @PostConstruct on bean class method
- Bean class implements InitializingBean
What are the 3 different ways to implement bean pre-destroy behavior?
- @Bean(destroyMethod=”methodName”)
- @PreDestroy on class bean method
- Class bean implements DisposableBean
When using both XML and Java configuration at the same time, which one is processed first?
Java configuration is processed first. This means XML configuration will override Java configuration
Which annotation can be used to define multiple bean aliases?
@Bean(name={"name1", "name2", ...})
Which annotation can be used to import XML configuration files into a Java configuration class?
@ImportResource(locations=String[])
How does the Spring IoC Container work?
The container takes in your POJOs and configuration metadata and creates a fully configured application ready for use
Are POM dependencies added to the classpath?
Yes