Week 7 Spring Rest Study Flashcards
What are Spring Projects
Spring Projects are a collection of projects that complement and extend the Spring Framework.
What is the Spring Framework?
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.
What is the Spring Framework
The Spring Framework is a lightweight solution for building enterprise-level applications that supports business logic.
What are some features of the Spring Framework
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.
What is the main idea of the Spring Framework?
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).
What is IoC and what does the IoC container do?
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.
What is the IoC container responsible for?
The IoC container is responsible for instantiating, configuring, and assembling the beans of the application.
The container gets its instructions on what / how?
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.
What are some advantages of IoC?
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.
What is dependency injection?
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.
What are some benefits of Dependency Implementation?
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
What types of dependency injection does Spring support?
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.
What are the differences between Constructor injection and Setter injection?
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).
What is a Field Injection?
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.
What are some advantages of Field Injections?
It is easy to use as no constructors or setters are required.
It can be easily combined with the constructor and/or setter approach
What are some disadvantages of Field Injections?
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.
What is a Java Bean?
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.
What is a BeanFactory?
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.
What is the ApplicationContext?
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.
What are some differences between a BeanFactory, and ApplicationContext? Which one eagerly instantiates your beans?
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)
What is the Spring Bean lifecycle?
-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
What is a Spring Bean?
A Spring Bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container.
What is bean wiring?
Bean wiring is the process of combining beans with Spring container through proper configurations.
Explain two ways one could wire a Bean.
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.