Week 7 Flashcards

1
Q

What are Spring Framework Modules?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain the 5 Spring Projects?

A
  1. 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)
  2. Spring Cloud - provides tools for developers to quickly build some of the common patterns in distributed systems; works well in any distributed environment
  3. 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
  4. 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
  5. 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. What is IOC and what does the IOC Container do?
A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is dependency injection and what are some of the benefits of using dependency injection?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What 2 methods of dependency injection does Spring support?

A
  1. 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
  2. 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A
  • 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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the Spring Bean lifecycle (12 steps)?

A
  1. Instantiation
  2. Populate properties
  3. BeanNameAware’s setBeanName() - Informs Bean Factory of Bean Name
  4. BeanFactoryAware’s setBeanFactory() - Informs bean of their owning bean factory
  5. Pre-initialization BeanPostProcessors - - Method called before bean initialization callback methods are invoked, but after bean has been populated with property values
  6. afterPropertiesSet() - Method invoked by the owner bean factory after all bean properties have been set and satisfied Aware methods
  7. Custom init() method - Custom defined method (public void init())
  8. 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
  9. Bean ready to use
  10. IOC container is shutdown
  11. DisposableBean’s destroy() method
  12. Custom destroy() method - Custom defined method (public void destroy())
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is bean wiring? What about autowiring?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the different ways that Spring can wire beans?

A
  • 2 ways:
    • manually - by declaring the beans using Dependency Injection
    • autowiring - let the PSring container wire the required beans
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the 6 scopes of Spring beans? Which is default?

A
  1. Singleton Scope (DEFAULT) - Each bean definition is scoped to a single object instance per Spring IoC Container.
  2. Prototype Scope - A single bean definition is scoped to a number of object instances.
  3. 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.
  4. Session Scope - A single bean definition is scoped to the lifecycle of an HTTP Session.
  5. 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.
  6. 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Describe the purpose of the Spring framework

A

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.

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

What version of Spring are you comfortable working with? Do you know the latest major version released?

A

The latest major version is version 5, we are currently using spring boot 2.5.6 which includes spring core 5.3.12.

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

What is dependency injection?

A

this is a technique where an object receives other objects it depends on, in the most basic form using constructors and setters

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

what is inversion of control?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How is dependency injection achieved within the Spring framework?

A

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.

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

What is the primary IoC container in Spring?

A

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.

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

What are some differences between the ApplicationContext and the BeanFactory in Spring?

A

ApplicationContext extends BeanFactory and contains additional features like messaging and event publication. Also BeanFactory is lazy and ApplicationContext is eager by default.

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

Are beans loaded eagerly or lazily within the ApplicationContext? How can you change this?

A

By default they are loaded eagerly. This behavior can be changed with the @Lazy annotation.

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

What are some ways that a bean registry can be provided to Spring?

A

We can supply bean descriptions with XML files, or by scanning for properly annotated classes.

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

What is component-scanning in Spring?

A

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.

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

What are the 5 Spring stereotype annotations?

A

@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

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

What is the difference between manual bean wiring and autowiring?

A

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.

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

What is the standard lifecycle of a bean within the ApplicationContext?

A

Instantiation, awareness, initialization (including custom init methods), post-init-processing, in-use, destruction (including custom destory methods), garbage collection.

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

What are the scopes of a Spring bean? What is the default?

A

Singleton (DEFAULT) and Prototype are the basic ones. There are also other used for specific ApplicationContexts, like request, session, and global session scopes.

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

What are the 5 autowiring modes Spring uses to resolve autowired dependencies?

A
  1. autodetect
  2. byName
  3. ByType
  4. constructor
  5. no (as in no autowiring)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What are 3 forms of dependency injection supported by Spring?

A
  1. Constructor
  2. Setter
  3. Field (field autowiring is a big no-no, don’t do it.)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

If using Java-class configuration, what is the default name given to a bean? How can you change this?

A

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”)

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

If using component-scanning, what is the default name given to a bean? How can you change this?

A

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”)

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

How can you provide a scalar or literal value for injection into the property of a Spring bean (3 ways)?

A
  • In XML config use the tag with name and value attributes.
  • In component scanning we use the @Value annotation: @Value(“Default String Value Here”)
  • In Java-class config you can inject the value into the constructor.
30
Q

What is Spring MVC? How is it enabled for use within a Spring application?

A

MVC stands for Model View Controller.

In a spring boot application we just include the “spring-boot-starter-web” dependency.

Outside of spring boot we must have the necessary dependencies and we use the annotation @EnableWebMvc.

31
Q

Describe the data flow of a request/response operation within a Spring MVC application

A

Request comes in to front controller, is dispatched to an appropriate controller which fetches the model requested. This bounces back through the front controller to a view template where the model data is woven into the UI view, this view object is then returned to the front controller, and the response is returned to the client.

32
Q

What are some Spring MVC annotations (12 listed)?

A
@Controller
@DeleteMapping
@GetMapping
@PathVariable
@PostMapping
@PutMapping
@RequestBody
@RequestHeader
@RequestMapping
@RequestParam
@ResponseBody
@RestController
33
Q

What is the difference between @RestController and @Controller?

A

@RestController implies both @Controller and @ResponseBody.

@Controller is a stereotype annotation that marks a class as a bean for component scanning.

34
Q

What is the difference between @RequestBody and @ResponseBody?

A

@RequestBody applies to a parameter object and tells Spring to map the serialized payload data onto that object.

@ResponseBody applies to the return type of the method and tells Spring to serialize that object into some test representation like JSON.

35
Q

What is the difference between @RequestParam and @PathVariable?

A

@RequestParam gets a value associated with a key from the request body.

@PathVariable gets a value associated with a key in the URI.

36
Q

What is the DispatcherServlet and what is it used for?

A

DispatcherServlet delegates requests to other servlets. It acts as a front controller.

37
Q

What is an exception handler?

How would you declare one for use within a controller?

A

Describes how the program should handle exceptions.

We declare it with the use of @ExceptionHandler and give a specific exception class as an attribute, if this exception is thrown in any of the methods of the class it will invoke the annotated method.

38
Q

How would you configure contextual sessions between Spring and Hibernate?

A

Prior to Spring 3.0 we would have used Spring’s HibernateTemplate to manage sessions. Starting with 3.0 we can make use of Hibernate contextual sessions directly via the Hibernate SessionFactory.

39
Q

What information is provided to the configuration of the DataSource bean within Spring ORM (5)?

A
Dialect
Driver
Password
URL
username
40
Q

What information is provided to the configuration of the SessionFactory bean within Spring ORM?

A

Hibernate properties, such as: generate-ddl, ddl-auto, enable_lazy_load_no_trans

41
Q

What is the purpose of @Transactional?

Where should this annotation be placed?

How is its use enabled?

A

@Transactional is used to describe transaction propagation strategies.

This should be placed on a class or method.

To enable transaction management in Spring we use the @EnableTransactionManagement annotation, or the XML element: tx:annotation-driven/

42
Q

What are the 7 transaction propagation levels in Spring ORM?

A
  1. MANDATORY
  2. NESTED
  3. NEVER
  4. NOT_SUPPORTED
  5. REQUIRED
  6. REQUIRES_NEW
  7. SUPPORTS
43
Q

What are some examples of JSR-303 (Bean Validator) annotations? 11 listed.

A
@AssertFalse
@AssertTrue
@DecimalMax
@DecimalMin
@Digits
@Future
@Past
@Max
@Min
@NotNull
@Pattern
44
Q

What is the purpose of the RestTemplate class within Spring?What is Spring Boot?

A

RestTemplate allows us to create a client that consumes a remote API inside our Spring application. This feature is depricated as of Spring version 5.

45
Q

What is Spring Boot?

A

Spring boot is a part of the Spring suite which allows us to rapidly begin an enterprise level spring application by eshewing boilerplate configuration in favor of highly opinionated ready-to-go project foundation.

46
Q

Why is Spring Boot said to be “opinionated”?

A

This means that Spring Boot is designed to be used in a specific way, and adhering to those opinions will allow us to fully leverage the benefits of this solution. The more we deviate from these opinions the more difficult it is to utilize the benefits of this solution.

47
Q

What is the Spring Boot starter POM? Why is it useful?

A

is a boilerplate POM.xml file which includes the neseccary dependencies for maven to acquire in order to start our project.

48
Q

What are some notable features in Spring Boot?

A
  • Pre built and configured SpringBootApplication ready to be run.
  • Spring Initializr which generates the boilerplate opinionated foundation project for us.
  • Starter dependencies which include other necessary dependencies, starter-web, starter-data-jpa for instance.
  • Spring Boot Actuator - lets us access API endpoints that can provide info about our program or even invoke behaviors like shutdown.
  • Spring Boot DevTools - change certain features to make development easier (disable caching, automatic restarts)
49
Q

What annotation is used to denote an application as a Spring Boot application?

Where should this be placed?

A

@EnableAutoConfiguration, or @SpringBootApplication.

These are placed on the configuration class (either the class the main method appears in for component scanning, or the class that defines beans for java-class configuration).

50
Q

What other annotations are implied by @SpringBootApplication?

A

The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes. -Spring docs

51
Q

What file formats does Spring Boot support for configuration?

A

.xml, yaml, and .properties to name a few.

52
Q

What is Spring Data JPA?

A

This is Spring’s persistence API, running Hibernate as the default JPA provider.

53
Q

What is a repository interface?

A

An interface we implement in order to expose CRUD functionality.

54
Q

How does Spring Data JPA figure out how to query our data source if it uses interfaces with no explicitly defined implementations?

A

The interfaces are instantiated as beans thanks to the @Repository annotation, and these beans contain concrete implementations built by reflection/annotations.

55
Q

What is the purpose of @Query within Spring Data JPA?What are common interfaces which our custom data repository interfaces should extend?

A

To mark a method as a query method which returns data from a datasource. We can provide the @Query annotation with JPQL or Native (SQL) queries

56
Q

What are common interfaces which our custom data repository interfaces should extend? 4 listed

A

CrudRepository
JpaRepository
PagingAndSortingRepository
SimpleJpaRepository

57
Q

How would you go about testing a Spring MVC controller?

A

We can hit our endpoints with a client like postman, which has the ability to create and run test suites checking different responses from the API.

58
Q

What is AOP?

A

Aspect Oriented Programming, a different way of designing applications where we separate out aspects based on their functionality as they address cross-cutting concerns. This allows for even more loosely coupled code.

59
Q

What are cross-cutting concerns? Provide examples

A

A problem that needs solving throughout our application, not tied to any specific layer, class, or functionality. Security, logging, error/exception handling, data access, etc.

60
Q

What is an aspect?

A

An aspect encapsulates advice. It is a class that contains the advice needed to solve cross-cutting concerns.

61
Q

What is the difference between a join point and a pointcut?

A

A join point is the point in program execution at which the advice is needed, it is targetted by a point cut which advises the program at the join point.

62
Q

What are some different types of pointcut designators?

A

Within, execution, target, this, @args, @Within, @Annotation, @Target

63
Q

What is the difference between the this and target pointcut designators?

A

This targets a bean of the given type, target targets a proxy object of a given type.

64
Q

What is the difference between the * and .. wildcards in pointcut expressions?

A

* is a wildcard for the method signature parts like access modifiers, and return types. .. is the wildcard for 0 or more parameters.

65
Q

What is aspect weaving?

A

The process at compile time where aspects and advice are “woven” or inlined into our class files.

66
Q

What are some different types of aspect weaving?

A

Compile-time, Post-compile-time (binary weaving), load-time.

67
Q

What type of aspect weaving is supported by Spring AOP?

A

Load time weaving where the IoC container proxies beans, and these proxies are woven with advice.

68
Q

What is advice? List the types of advice supported by Spring AOP

A

In Java advice is always a method. Advice is the code we want to execute at some joinpoint, targetted by a pointcut.

@Before, @After, @AfterThrowing, @AfterReturning, @Around

69
Q

What is considered to be the most powerful type of advice? Why?

A

@Around, because we can choose to not even invoke the advised method, and instead return or throw a result ourselves.

70
Q

Can you prevent the execution of a join point when using before advice?

A

No. Only around does this, which is why @Around is considered the most powerful advice.

71
Q

What is AspectJ? How is it enabled for use within a Spring application?

A

AspectJ is an Aspect Oriented Programming extension to the Java language. It is enabled in Spring with the @EnableAspectJAutoProxy annotation.

72
Q

What is the JoinPoint argument used for? What is a ProceedingJoinPoint? When is it used?

A

JoinPoint is an object that holds information about the program state surrounding a join point. It stores information that may be useful when executing advice. ProceedingJoinPoint is similar and also contains the proceed() method which can be used with @Around advice type to choose to or not to proceed to the advised method.