Week 7 Spring Framework Overview Flashcards

1
Q

What are the goals of the Spring Framework?

A

Lightweight Development with POJOs (Plain Java Objects)

Dependency Injection

- Promotes loose coupling
- Loose coupling: Basically, when you're developing, you might use a bunch of different dependencies within a certain class, and you might want to be able to switch out different implementations, so we can utilize DI to achieve this to inject objects that we want to have our IoC container manage
        o "Switching out dependencies"
        o Not having to hardcode the instantiation of objects

Minimization of boiler code

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

What does IoC stand for?

A

Inversion of Control

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

What does IoC do?

A

Inverts control of object creation and application flow to a framework

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

What is Dependency Injection?

A

Dependency Injection (DI) is a pattern to implement IoC

"behavior is injected into your classes"
Achieves "Loose Coupling"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Describe Dependency Injection

A

Implementation of inversion of control

  • Dependency: some object another object needs in order to function properly
  • Injection: passing dependency to a dependent object
  • When a Spring bean is instantiated, any dependencies that it requires will be provided (given that those dependencies are also configured as Spring beans)
  • Decouples configuration from implementation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Describe Spring Bean

A

Any object whose lifecycle is managed by Spring (the IoC container)

We specify through configuration what should be a Spring Bean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a Core container?

A

Core container: The core of Spring for creating beans, and also for managing bean dependencies

Beans: spring-beans module
Core: spring-core module
Context: spring-context module
SpEL: Spring Expression Language module
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a BeanFactory?

A

BeanFactory is an implementation of the factory design pattern, which removes the need to create programmatic singletons and enables the decoupling of configuration and specification of dependencies from the program logic itself.

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

What do the Core and Bean modules provide?

A

The Core and Beans modules provide the fundamental parts of the Spring framework, such as IoC (inversion of control) and DI (dependency injection).

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

What does the Context build on top of?

A

The Context builds on top of the Core and Beans modules and allows access to objects in a framework-style manner. Context is what provides us with ApplicationContext, which extends BeanFactory.

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

List some modules.

A

Additional modules:

Web module
AOP module
ORM module
Test module
JDBC module
etc
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are Spring Projects?

A

Spring projects are built on top of different modules that make up Spring framework. These are “boiler-plate” projects that help us to easily build Spring applications.

Spring Boot
Spring Data
Spring Security
Etc
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the similarity between a BeanFactory and ApplicationContext?

A

Both represent an IoC container, which manages the lifecycle of Spring beans

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

What are some features of BeanFactory?

A

Older
It lazily instantiates Spring Beans
Must provide a resource object configured for our beans.xml

Example implementation: XmlBeanFactory

XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(“beans.xml”));

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

What are some features of ApplicationContext?

A

Newer

It eagerly instantiates Spring Beans

Provides support for annotations

Naming convention changed from beans.xml to applicationContext.xml

Sub-interface of BeanFactory (meaning this interface extends the BeanFactory interface)

Easier integration with Spring AOP

Event publication

Internationalization features

Support for application-layer specific contexts such as WebApplicationContext

Example implementations:

ClassPathXmlApplicationContext
FileSystemXmlApplicationContext
XmlWebApplicationContext
Etc.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Where is the XML configuration defined? Can you specify your beans inside of the XML file?

A

Our configuration is defined in applicationContext.xml

We can specify our beans inside of this file, whether we want component scanning (annotation scanning), etc.
17
Q

How do you configure Annotation?

A

Enable “Component Scanning” in our applicationContext.xml

18
Q

What are some Annotation classes?

A

Annotate classes w/ “stereotype annotations”

@Component: general Spring bean
@Repository: for our DAOs
@Controller: for our HTTP controllers
@Service: for various services in our service layer
19
Q

Where can we specify beans that we would like for Spring to instantiate and populate inside of?

A

a special configuration bean

20
Q

How would you handle Java Configuration?

A

We can utilize the @Configuration annotation in order to designate this class as our configuration class

We then would use @Bean on each method inside of this class that should be instantiating a bean

- the method name is the bean name itself
- @Bean is a special annotation that will actually intercept this method call, check to see if the bean already exists or not inside of the container, and if not, actually instantiate the object inside the method and return that new object. Otherwise, the new SomeObject() logic inside the method will not even run at all, and the already existing bean will simply be returned to us.
21
Q

What do Scopes determine?

A

Scopes determine how many instances of a particular Spring bean we should create and have at any particular time. This depends on the context in which they are used, which is why this is referred to as “scope”.

22
Q

What are the Universal Scopes?

A

Singleton (default): There is only 1 instance of the Bean

Prototype: There will be a new Bean instantiated each time it is called for

23
Q

What are the Web-Aware Scopes?

A

Request: One Per HTTP request
Session: One Per HTTP session
New as of Spring 5
- Application: One per ServletContext (Per Web Container)
- WebSocket: One per websocket
Now deprecated as of Spring 5
- Global session: was used for Portlets for global session

24
Q

What is Bean Wiring?

A

Bean wiring is the process of connecting our beans using dependency injection (DI)

25
Q

What are the Dependency Injection Types?

A

Setter Injection
Constructor Injection
Field Injection

26
Q

Describe the Setter injection.

A

Uses setter methods to provide dependencies

Does not ensure DI because an instance could be created without configuring a particular field

  • no issue would be raised if we did not successfully provide a dependency to this object, because in order to make use of the setter, you would have already had to have instantitated this bean
  • one way around this issue could be to use the @Required annotation to make sure that there is actually a dependency provided
    - Would prevent NullPointerException issues down the road
27
Q

Describe the Constructor Injector

A

Uses a constructor (this is what we saw already with Angular)

We provide to the constructor the necessary dependencies as parameters

The bean cannot be instantiated without the proper dependencies being injected
28
Q

Describe Field Injector

A

@Autowired directly on top of the field itself (property)

-example:
@Autowired
private IMyService myService;

Utilizes reflection under the hood, not constructors or setters

29
Q

What is Autowiring?

A

Autowiring in general uses the @Autowired annotation. You can place this on fields directly, in which case we would be utilizing field injection, or on constructors, setters for constructor/setter injection.