Spring Framework Flashcards

1
Q

What is Spring?

A

Spring is a framework for building Java applications

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How can a configuration class import another configuration class?

A

@Import({a.class, b.class})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the default scope of a bean?

A

Singleton

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can you change the scope of a bean?

A

@Scope(...)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Describe how a prototype bean works

A

A new bean is created anytime it is referenced

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can you autowire a dependency only if it exists?

A

@Autowired(required=false)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

For non-Spring Boot applications, if @Component is used, what other annotation must be used on the configuration class?

A

@ComponentScan(…)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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 {...}
A

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 {...}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. Where can @Value be used?
  2. Is @Value implemented by BeanFactoryPostProcessor or BeanPostProcessor?
A
  1. Fields and parameters
  2. Both, its value is resolved by BeanFactoryPostProcessor and injected by BeanPostProcessor for fields and setter methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. What is the difference between standard beans and @Lazy beans?
A
  1. Standard beans are instantiated eagerly at application startup and @Lazy beans are instantiated only when they are referenced (e.g. ApplicationContext.getBean(…) is used)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the 6 stereotype annotations?

A
  1. @Component
  2. @Configuration
  3. @Controller
  4. @RestController
  5. @Repository
  6. @Service
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

True or false:

ApplicationContext is a subtype of BeanFactory

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Describe the 5 different phases of a Spring bean’s lifecycle

A
  1. Load and process bean definitions through BeanFactoryPostProcessor
  2. Instantiate beans
  3. Initialize bean through BeanPostProcessor
  4. Use beans through @Autowired
  5. Destroy beans through ApplicationContext.close()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. When using a method to define a bean, how is its type and name determined?
  2. When using a class to define a bean, how is its type and name determined?
A
  1. Type = method return type
    Name = method name
  2. Type = class
    Name = class name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

The Environment bean has access to the 3 following property sources…

A
  1. JVM system properties
  2. System environment properties
  3. Property files (if appropriate configuration is used)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the 2 different ways to access property values inside a configuration class?

@PropertySource(...)
@Configuration
public class MyConfig {...}
A
  1. Environment Bean
  2. @Value(…)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

When might you need to use @PropertySource(…)?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

True or False

@Profile(…) can be used on both bean class and bean method definitions

19
Q

What is a bean?

A

A bean is an object managed by the Spring IOC container (ApplicationContext)

20
Q

Should a bean method definition return an interface type or a class type?

A

Interface type

Always remember, “depend on abstractions, not concretions”

21
Q

What is a Project Object Model?

A

POM is an xml file containing all the information Maven needs to build an application

22
Q

BeanPostProcessor

  1. What does it do?
  2. How can you implement your own?
A
  1. BeanPostProcessor can modify an instantiated bean before and/or after initialization
  2. Define your own BeanPostProcessor bean class
23
Q

Which bean is responsible for creating bean proxies?

A

BeanPostProcessor

24
Q

What does @Autowired do?

A

Injects dependencies by type

25
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
26
# @ComponentScan Should you be as specific as possible when defining which packages to scan for?
Yes. This will avoid unnecessary scanning
27
# @PostConstruct and @PreDestroy 1. What access modifier can be used? 2. What must they return? 3. What parameters must they define?
``` @Component public class MyBean { @PostConstruct [any] void init() {...} @PreDestroy [any] void cleanup() {...} } ```
28
# @PostConstruct and @PreDestroy When do they execute?
1. @PostConstruct executes at startup after all dependencies are injected 2. @PreDestroy executes at shutdown prior to bean destruction
29
# True or False An ApplicationContext cannot be created in any environment
False
30
# True or False Singleton beans should be used as stateful beans and prototype beans should be used as stateless beans
False
31
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
32
What are the 2 primary bean scopes?
1. Singleton = 1 instance 2. Prototype = new instance per use
33
# True or False Bean overriding can occur in any environment
False. Only in test environments
34
Is `@Configuration` CGLIB or JDK proxied?
CGLIB proxied. This is because CGLIB is used for classes that don't implement an interface
35
Create a custom annotation composed of both @Service and @Transactional
@Target(ElementType.TYPE) @RetentionPolicy(RetentionPolicy.RUNTIME) @Transactional(timeout=60) @Service public @interface MyTransactionalService {...}
36
# What does the following code mean? `@Value("${user.age : 18")`
If the user.age property does not have a value, use 18 as default
37
What are the 3 different ways to implement bean post-construct behavior?
1. @Bean(initMethod="methodName") 2. @PostConstruct on bean class method 3. Bean class implements InitializingBean
38
What are the 3 different ways to implement bean pre-destroy behavior?
1. @Bean(destroyMethod="methodName") 2. @PreDestroy on class bean method 3. Class bean implements DisposableBean
39
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
40
Which annotation can be used to define multiple bean aliases?
`@Bean(name={"name1", "name2", ...})`
41
Which annotation can be used to import XML configuration files into a Java configuration class?
`@ImportResource(locations=String[])`
42
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
43
Are POM dependencies added to the classpath?
Yes