Week 7 Flashcards
What are Spring Framework Modules?
- the spring framework consists of 20 modules that focus on a particular class, and are grouped into 8 layers:
1. Core Container - provides the basic framework for the IoC container and dependency injection (Core & Beans, Context, and SpEL (Spring Expression Language)
2. Data Access/Integration - provides support for database management or layers of abstraction (JDBC, ORM, OXM (Object/XML Mapping), JMS (Java Messaging System)
3. Messaging - provides extensive support for integrating with messaging systems
4. Web - provides basic web integration features (Web-Servlet, WebSocket, Web-Portlet)
5. AOP (Aspect Oriented Programming) - aims to decouple code from functionality that is independent from the core functionality of that code
6. Aspects - when enabled, allows for the use of the AspectJ supported style of declaring and autoproxying beans based on whether the bean is being advised by one or more aspects
7. Instrumentation - provides support for instrumentation (the process of planning, installing, monitoring and maintaining systems) and classloader implementation
8. Test - provides support for integration and best practice unit testing as well as mocking info
Explain the 5 Spring Projects?
- Spring Boot - an open source Spring Framework project used to rapidly create Java based, production-grade applications utilizing Spring Framework’s IOC and module integrations; simplifies the process of project startup and framework integration by applying highly opinionated auto-configurations (ie webmvc, data, security, messaging, etc)
- Spring Cloud - provides tools for developers to quickly build some of the common patterns in distributed systems; works well in any distributed environment
- Spring Data - provides standard implementation for common DAO methods allowing for the removal of the DAO implementation and only requiring the definition of the DAO interface methods
- Spring Security - focuses on providing comprehensive and extensible support for both authentication and authorization to Java applications; protects against attacks (ie session fixation, clickjacking, cross site request forgery, etc); servlet API integration
- Spring HATEOAS - provides some APIs to ease creating REST representations that follow the HATEOAS principle when working with Spring and especially Spring MVC; core problem it tries to address is link creation and representation assembly
- What is IOC and what does the IOC Container do?
- IOC (Inversion of Control) - a design principle in which control over certain parts of object-oriented design is inverted to achieve loose coupling (EX - you are in control of your car when you drive, but when you take a taxi the control goes to the taxi driver)
- IOC Container - responsible for instantiating, configuring and assembling objects known as beans. It does this by getting information from the XML file and assembling the objects accordingly
- 2 types: BeanFactory and ApplicationContext
What is dependency injection and what are some of the benefits of using dependency injection?
- a design pattern that removes dependencies of a program by providing the configuration in an external source, such as an XML file
- 2 methods: Constructor Injection and Setter Injection
- benefits - loosely coupled design then makes code easier to test, and implement in a wider variety of environments
What 2 methods of dependency injection does Spring support?
- Constructor Injection: accomplished when the container invokes a constructor with arguments to instantiate a bean in which each argument of said constructor represents a dependency; more secure; enables the implementation of immutable objects
- Setter Injection: accomplished when the container calls setter methods on a bean after invoking a no-argument constructor to instatiate a bean; allows for partial dependencies, can easily change values and doesn’t create new bean instances (more flexible); can resolve circular references
What are some differences between BeanFactory and ApplicationContext? Which one eagerly instantiates your beans?
- Bean Factory - root interface for accessing a Spring bean container; instantiates, configures, and manages a number of beans that collaborate with one another and have dependencies between themselves; interface is implemented by the objects that hold a number of bean definitions(by a String name)
- ApplicationContext - interface designed on top of the BeanFactory; the advanced container that enhances BeanFactory functionality;
- BeanFactory – in org.springframework.beans.factory.xml.package, ApplicationContext – in org.springframework.context package
- BeanFactory loads beans on-demand (LAZY), while ApplicationContext loads all beans at startup (EAGER)
What is the Spring Bean lifecycle (12 steps)?
- Instantiation
- Populate properties
- BeanNameAware’s setBeanName() - Informs Bean Factory of Bean Name
- BeanFactoryAware’s setBeanFactory() - Informs bean of their owning bean factory
- Pre-initialization BeanPostProcessors - - Method called before bean initialization callback methods are invoked, but after bean has been populated with property values
- afterPropertiesSet() - Method invoked by the owner bean factory after all bean properties have been set and satisfied Aware methods
- Custom init() method - Custom defined method (public void init())
- Post-initialization BeanPostProcessors - Callback which allows post processor to decide whether to apply either the FactoryBean, created object or both through corresponding instanceof FactoryBean checks
- Bean ready to use
- IOC container is shutdown
- DisposableBean’s destroy() method
- Custom destroy() method - Custom defined method (public void destroy())
What is bean wiring? What about autowiring?
- wiring of Spring beans can be done by both Java classes and XML configuration
- wiring - the process of combining beans within the Spring container; you should tell the container what beans are needed and how the container should use dependency injection to tie them together
- autowiring - enables you to inject the object dependency implicitly; internally uses setter or constructor injection; can’t be used to inject primitive and string values
What are the different ways that Spring can wire beans?
- 2 ways:
- manually - by declaring the beans using Dependency Injection
- autowiring - let the PSring container wire the required beans
What are the 6 scopes of Spring beans? Which is default?
- Singleton Scope (DEFAULT) - Each bean definition is scoped to a single object instance per Spring IoC Container.
- Prototype Scope - A single bean definition is scoped to a number of object instances.
- Request Scope - A single bean definition is scoped to the lifecycle of a single HTTP request. This means the HTTP request has its own instance of a bean created from a single bean definition.
- Session Scope - A single bean definition is scoped to the lifecycle of an HTTP Session.
- Gloabl Session Scope - A single bean definition is scoped to the lifecycle of a global HTTP session. Generally only valid when used in a portlet context.
- Application Scope - Scopes a single bean definition to the lifecycle of a ServletContext.
- Only valid with a web-aware ApplicationContext: Request, Session, Global Session, Application
Describe the purpose of the Spring framework
Spring is a powerful framework that allows us to jump right in to enterprise level application programming. Spring inverts control, helps us achieve loose coupling, and offers robust abstractions/solutions for common needs such as persistence and servlets.
What version of Spring are you comfortable working with? Do you know the latest major version released?
The latest major version is version 5, we are currently using spring boot 2.5.6 which includes spring core 5.3.12.
What is dependency injection?
this is a technique where an object receives other objects it depends on, in the most basic form using constructors and setters
what is inversion of control?
This is a paradigm where program execution and control is handed off to some framework, and our code is invoked by that framework. This inverts the traditional idea of our program beginning with the main method, foolowing our execution, and invoking library methods.
How is dependency injection achieved within the Spring framework?
Spring offers DI by providing the dependencies on demand. The dependencies are beans described to a BeanFactory (an IoC Container) with either XML files, or programatically. Spring will autowire these dependencies in as needed.
What is the primary IoC container in Spring?
BeanFactory, which was supplanted by a class that extends it: ApplicationContext. ApplicationContext also has been extended for more specific use, like WebAwareApplicationContext. There are others as well like AnnotationConfigApplicationContext.
What are some differences between the ApplicationContext and the BeanFactory in Spring?
ApplicationContext extends BeanFactory and contains additional features like messaging and event publication. Also BeanFactory is lazy and ApplicationContext is eager by default.
Are beans loaded eagerly or lazily within the ApplicationContext? How can you change this?
By default they are loaded eagerly. This behavior can be changed with the @Lazy annotation.
What are some ways that a bean registry can be provided to Spring?
We can supply bean descriptions with XML files, or by scanning for properly annotated classes.
What is component-scanning in Spring?
A component is a bean, and component scanning is a method of describing beans where Spring scans through specified packages of classes looking for properly annotated classes that describe beans.
What are the 5 Spring stereotype annotations?
@Component - a generic stereotype used to declare an objects as a bean, which will be injected into other classes/beans at some point in time
@Controller - marks a class as a Spring MVC Controller which allow the use of handler mapping annotations. Classes annotated with @Controller are autodeteced through classpath scanning. when used in comination with @RequestMapping it allows for quick configurations of a web application controller
@Repository - marks a class to be used as a for use with storing data within a repository or database. also provides benefits for objects that would otherwise be utilized as a DAO
@Service - marks a class as a Service for an application
@RestController - a convenience stereotype annotation which combines the features of @RequestMapping with the @ResponseBody annotations. This allows the use of HTTP method specific Mapping annotations which automatically produce an XML, JSON or other response
What is the difference between manual bean wiring and autowiring?
The @Autowired annotation is used to have Spring grab necessary dependencies from the IoC container and inject them.
Manual wiring is requesting a bean by type or name yourself and assigning it to a reference.
What is the standard lifecycle of a bean within the ApplicationContext?
Instantiation, awareness, initialization (including custom init methods), post-init-processing, in-use, destruction (including custom destory methods), garbage collection.
What are the scopes of a Spring bean? What is the default?
Singleton (DEFAULT) and Prototype are the basic ones. There are also other used for specific ApplicationContexts, like request, session, and global session scopes.
What are the 5 autowiring modes Spring uses to resolve autowired dependencies?
- autodetect
- byName
- ByType
- constructor
- no (as in no autowiring)
What are 3 forms of dependency injection supported by Spring?
- Constructor
- Setter
- Field (field autowiring is a big no-no, don’t do it.)
If using Java-class configuration, what is the default name given to a bean? How can you change this?
The default name of a bean is the name of the method which retruns it.
We can change the name with the @Qualifier annotation: @Qualifier(“myBeanName”)
If using component-scanning, what is the default name given to a bean? How can you change this?
The default name given is the class name with the first letter lowercased.
We can change the default name by giving a new name as a string to the @Bean or the stereotype annotation @Component(“myNamedComponent”)