Week 7 Spring Rest Study Flashcards

1
Q

What are Spring Projects

A

Spring Projects are a collection of projects that complement and extend the Spring Framework.

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

What is the Spring Framework?

A

The Spring Framework is a Java platform that provides infrastructure support for developing java applications so that the developer can focus more on the business logic part of the application.

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

What is the Spring Framework

A

The Spring Framework is a lightweight solution for building enterprise-level applications that supports business logic.

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

What are some features of the Spring Framework

A

1.) It is modular, allowing you to use only the modules from the framework that you need for your application without depending on the whole framework.

2.) All Spring Modules share the same release version as the Framework. They are also a part of the same project.

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

What is the main idea of the Spring Framework?

A

The Spring Framework includes an Inversion of Control (IoC) container that manages the Java objects

(the zookeeper of the application’s “ecosystem” that conducts these spring modules and the rest of our application to function harmoniously together).

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

What is IoC and what does the IoC container do?

A

Inversion of Control, or IoC, is a principle in software engineering by which the control of objects or portions of a program is transferred to a container or framework. The main goal of Spring’s IoC container is a separation of concerns. It decouples the execution of a task from its implementation.

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

What is the IoC container responsible for?

A

The IoC container is responsible for instantiating, configuring, and assembling the beans of the application.

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

The container gets its instructions on what / how?

A

The container gets its instruction on what objects to instantiate, configure, and assemble.

It does this by reading configuration metadata stored in the XML/YML/properties file.

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

What are some advantages of IoC?

A

Decoupling of the program from the infrastructure.
Making it easier to switch between different implementations.
Greater modularity of a program.
Greater eas in testing a program by isolating a component or mocking its dependencies and allowing components to communicate through contracts.

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

What is dependency injection?

A

Dependency injection is a specific style of IoC.

DI = more specific IoC
IoC = a generic pattern of control flow between app implementation.

Dependency Injection: implementations are passed into an object through constructors, setters, service lookups, which the object will ‘depend’ on in order to behave correctly.

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

What are some benefits of Dependency Implementation?

A

Reduced dependencies, and reduced dependency carrying.

Reduced dependencies reduces the vulnerability of an object from changes that might occur from a dependency changing.

Reduced dependency carrying is when an object takes a parameter in one of its methods that it doesn’t need itself, but is needed by one of the objects it calls to carry out its work. This can make it harder to maintain or test code.

More reusable code

More testable code = DI makes it possible to inject mock implementations of dependencies.

More readable code

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

What types of dependency injection does Spring support?

A

Constructor Injection: Dependency Injection accomplished when the container invokes a constructor with arguments to instantiate a bean in which each argument of said constructor represents a dependency.

Setter Injection: Dependency Injection accomplished when the container calls a setter methods on a bean after invoking a no-argument constructor to instantiate a bean.

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

What are the differences between Constructor injection and Setter injection?

A

Constructor injection is more secure since dependencies are required to create an object, you are guaranteed to have each dependency populated. It also enables the implementation of immutable objects.

Setter injections allow for partial dependencies since Constructor injection requires all properties to be established upon bean instantiation.

Setter injections can easily change values, and does not create new bean instances making it more flexible than constructor injection.

Setter injection can resolve circular references. ( i.e. If object A and object B are dependent on each other, a setter injection can be used to resolve this, whereas a constructor injection would throw a BeanCurrentlyInCreationException).

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

What is a Field Injection?

A

A field injection is a type of injection that is possible only in the annotation based approach due to the fact that it is not really a new injection type - under the hood spring uses reflection to set these values.

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

What are some advantages of Field Injections?

A

It is easy to use as no constructors or setters are required.

It can be easily combined with the constructor and/or setter approach

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

What are some disadvantages of Field Injections?

A

Less control over object instantiation. In order to instantiate the object of a class for a test, you will need either a Spring container configured or mock library - depending on the test being written.

A number of dependencies can reach dozens until you notice that something went wrong in the design.

No immutability - the same as for setter injection.

17
Q

What is a Java Bean?

A

It is a regular Java class, except it follows certain conventions:

All properties are private (use getters/setters)
A public no-argument constructor
Implements Serializable.

Also, there is no syntactic difference between a JavaBean and another class – a class is a JavaBean if it follows the standards.

18
Q

What is a BeanFactory?

A

A BeanFactory is like a factory class that contains a collection of beans. The BeanFactory is the actual container which instantiates, configures, and manages a number of beans.

19
Q

What is the ApplicationContext?

A

The ApplicationContext is the central interface within a Spring application that is used for providing configuration information to the application. It’s created when the application starts running.

20
Q

What are some differences between a BeanFactory, and ApplicationContext? Which one eagerly instantiates your beans?

A

ApplicationContext eagerly instantiates beans.

BeanFactory uses lazy instantiation (on-demand loading) while ApplicationContext uses eager instantiation (at-startup loading).

ApplicationContext enhances BeanFactory in a more framework-oriented style. It provides:
-messaging functionality
-event publication functionality
-annotation based dependency injection
-easy integration with Spring AOP (aspect-oriented programming)

21
Q

What is the Spring Bean lifecycle?

A

-Instantiate
-Populate Properties
-Call setBeanName of BeanNameAware
-Call setBeanFactory of BeanFactoryAware
-Call setApplicationContext of ApplicationContextAware
-Pre-initialization (BeanPostProcessors)
-afterPropertiesSet of Initializing Beans
-Custom init Method
-Post Initialization (BeanPostProcessors)
-Bean Ready to Use
-Container Shuts Down/Bean is being torn down manually
-Call destroy of DisposableBean
-Custom Destroy Method (Teardown)
-End of Bean LifeCycle

22
Q

What is a Spring Bean?

A

A Spring Bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.

23
Q

What is bean wiring?

A

Bean wiring is the process of combining beans with Spring container through proper configurations.

24
Q

Explain two ways one could wire a Bean.

A

Explicit wiring: The programmer needs to instruct the IoC container through tagging in a XML configuration (XML element tags of property, construct-args) how the container will do the dependency injection).

Autowiring: Without any programmer-defined instructions, Spring automatically detects and injects dependencies by sniffing out any @autowired annotations in a given application.

25
Q

What are the different ways that Spring can wire beans?

A

There are many ways Spring ca wire beans. 5 modes can be used with XML-configuration-based autowiring functionality.

  1. no (default in XML-confic-based) = no autowiring at all; bean references are defined via ref element.
  2. byName = autowiring by property name; looks for bean named exactly the same as the property which needs to be autowired (setter dependency injection - having a @Autowired over the setter methods).
  3. byType = autowiring by property class type; can only be wired if there is only one bean with that same type. If there are multiple an error will be thrown. (setter dependency injection - having a @autowired over the setter methods).
  4. constructor = The constructor mode injects the dependency by calling the constructor out of the class.
  5. autodetect (deprecated).
26
Q

What are some scopes of Spring beans?

A

singleton (default): This scopes the bean definition to a single instance per Spring IoC container.

prototype: This scopes the single bean definition to have any number of object instances.

request: This scopes a bean definition to an HTTP request.

session: This scopes a bean definition to an HTTP session (server-side).

global-session (only available in Portlet applications): This scopes a bean definition to a global HTTP session.

27
Q

What is a concept of component scanning?

A

Using component scan is one method of asking Spring to detect Spring-managed components. Spring needs the information to locate and register all the Spring components with the application context when the application starts. Spring can auto scan, detect, and instantiate components from pre-defined project packages.

28
Q

How would you set up component scanning?

A

With Spring, we use the @ComponentScan annotation along with the @Configuration annotation to specify the packages that we want to be scanned. @ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages.

29
Q

What are the benefits of Java configuration?

A

Java is type safe. Compiler will report issues if you are configuring right bean class qualifiers.

XML based on a configuration can quickly grow big, while Java-based configuration allows for cleaner code with any annotation change.

Search is much simpler, refactoring will be easier. Finding a bean definition will also be easier.

30
Q

What are some limitations of Java configuration?

A

coupling between the PJO and its behavior

Not centralized, spread throughout the application

Integration of different configurations with the same application is harder to do when using annotations vs an XML file

31
Q

What is POJO?

A

POJO stands for Plain Old Java Object. It is an ordinary Java object, not bound by any special restriction other than those forced by the Java Language Specification and not requiring any classpath. POJOs are used for increasing the readability and re-usability of a program.

32
Q

What does the @Configuration annotation do?

A

@Configuration annotation indicates that a class decalres one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service request for those beans at runtime.

33
Q

What does @Bean annotation do?

A

@Bean is is a method-level annotation and a direct analog of the XML element. When Java encounters this method it will execute that method and register the return value as a bean within a BeanFactory. By default the bean name will be the same as the method name.

34
Q

What is @Value used for?

A

@Value is a Java annotation that is used at the field or method/constructor parameter level and it indicates a default value for the affected argument. It is commonly used for injecting values into configuration variables.