Spring framework Flashcards

1
Q

What is Spring?

A

Spring is an open source development framework for Enterprise Java. Spring framework targets to make Java EE development easier to use and promote good programming practice by enabling a POJO-based programming model.

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

What are benefits of Spring Framework?

A

• Lightweight: Spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 2MB. • Inversion of control (IOC): Loose coupling is achieved in Spring, with the Inversion of Control technique. The objects give their dependencies instead of creating or looking for dependent objects. • Aspect oriented (AOP): Spring supports Aspect oriented programming and separates application business logic from system services. • Container: Spring contains and manages the life cycle and configuration of application objects. • MVC Framework: Spring’s web framework is a well-designed web MVC framework, which provides a great alternative to web frameworks. • Transaction Management: Spring provides a consistent transaction management interface that can scale down to a local transaction and scale up to global transactions (JTA). • Exception Handling: Spring provides a convenient API to translate technology-specific exceptions (thrown by JDBC, Hibernate, or JDO) into consistent, unchecked exceptions.

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

Which are the Spring framework modules?

A

• Core module• Bean module• Context module• Expression Language module• JDBC module• ORM module• OXM module• Java Messaging Service(JMS) module• Transaction module• Web module• Web-Servlet module• Web-Struts module• Web-Portlet module

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

Explain the Core Container (Application context) module

A

This is the basic Spring module, which provides the fundamental functionality of the Spring framework. BeanFactory is the heart of any spring-based application. Spring framework was built on the top of this module, which makes the Spring container.

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

BeanFactory - BeanFactory implementation example

A

A BeanFactory is an implementation of the factory pattern that applies Inversion of Control to separate the application’s configuration and dependencies from the actual application code.The most commonly used BeanFactory implementation is the XmlBeanFactory class.

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

XMLBeanFactory

A

The most useful one is org.springframework.beans.factory.xml.XmlBeanFactory, which loads its beans based on the definitions contained in an XML file. This container reads the configuration metadata from an XML file and uses it to create a fully configured system or application.

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

Explain the AOP module

A

The AOP module is used for developing aspects for our Spring-enabled application. Much of the support has been provided by the AOP Alliance in order to ensure the interoperability between Spring and other AOP frameworks. This module also introduces metadata programming to Spring.

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

Explain the JDBC abstraction and DAO module

A

With the JDBC abstraction and DAO module we can be sure that we keep up the database code clean and simple, and prevent problems that result from a failure to close database resources. It provides a layer of meaningful exceptions on top of the error messages given by several database servers. It also makes use of Spring’s AOP module to provide transaction management services for objects in a Spring application.

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

Explain the object/relational mapping integration module

A

Spring also supports for using of an object/relational mapping (ORM) tool over straight JDBC by providing the ORM module. Spring provides support to tie into several popular ORM frameworks, including Hibernate, JDO, and iBATIS SQL Maps. Spring’s transaction management supports each of these ORM frameworks as well as JDBC.

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

Explain the web module

A

The Spring web module is built on the application context module, providing a context that is appropriate for web-based applications. This module also contains support for several web-oriented tasks such as transparently handling multipart requests for file uploads and programmatic binding of request parameters to your business objects. It also contains integration support with Jakarta Struts.

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

Explain the Spring MVC module

A

MVC framework is provided by Spring for building web applications. Spring can easily be integrated with other MVC frameworks, but Spring’s MVC framework is a better choice, since it uses IoC to provide for a clean separation of controller logic from business objects. With Spring MVC you can declaratively bind request parameters to your business objects.

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

Spring configuration file

A

Spring configuration file is an XML file. This file contains the classes information and describes how these classes are configured and introduced to each other.

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

What is Spring IoC container?

A

The Spring IoC is responsible for creating the objects,managing them (with dependency injection (DI)), wiring them together, configuring them, as also managing their complete lifecycle.

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

What are the benefits of IOC?

A

IOC or dependency injection minimizes the amount of code in an application. It makes easy to test applications, since no singletons or JNDI lookup mechanisms are required in unit tests. Loose coupling is promoted with minimal effort and least intrusive mechanism. IOC containers support eager instantiation and lazy loading of services.

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

What are the common implementations of the ApplicationContext?

A

The FileSystemXmlApplicationContext container loads the definitions of the beans from an XML file. The full path of the XML bean configuration file must be provided to the constructor. The ClassPathXmlApplicationContext container also loads the definitions of the beans from an XML file. Here, you need to set CLASSPATH properly because this container will look bean configuration XML file in CLASSPATH. The WebXmlApplicationContext: container loads the XML file with definitions of all beans from within a web application.

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

What is the difference between Bean Factory and ApplicationContext?

A

Application contexts provide a means for resolving text messages, a generic way to load file resources (such as images), they can publish events to beans that are registered as listeners. In addition, operations on the container or beans in the container, which have to be handled in a programmatic fashion with a bean factory, can be handled declaratively in an application context. The application context implements MessageSource, an interface used to obtain localized messages, with the actual implementation being pluggable.

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

What does a Spring application look like?

A

• An interface that defines the functions.• The implementation that contains properties, its setter and getter methods, functions etc., • Spring AOP • The Spring configuration XML file. • Client program that uses the function

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

What is Dependency Injection in Spring?

A

Dependency Injection, an aspect of Inversion of Control (IoC), is a general concept, and it can be expressed in many different ways.This concept says that you do not create your objects but describe how they should be created. You don’t directly connect your components and services together in code but describe which services are needed by which components in a configuration file. A container (the IOC container) is then responsible for hooking it all up.

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

What are the different types of IoC (dependency injection)?

A

• Constructor-based dependency injection: Constructor-based DI is accomplished when the container invokes a class constructor with a number of arguments, each representing a dependency on other class. • Setter-based dependency injection: Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.

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

Which DI would you suggest Constructor-based or setter-based DI?

A

You can use both Constructor-based and Setter-based Dependency Injection. The best solution is using constructor arguments for mandatory dependencies and setters for optional dependencies.

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

What are Spring beans?

A

“The Spring Beans are Java Objects that form the backbone of a Spring application. They are instantiated, assembled, and managed by the Spring IoC container. These beans are created with the configuration metadata that is supplied to the container, for example, in the form of XML definitions. Beans defined in spring framework are singleton beans. There is an attribute in bean tag named ““singleton”” if specified true then bean becomes singleton and if set to false then the bean becomes a prototype bean. By default it is set to true. So, all the beans in spring framework are by default singleton beans.”

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

What does a Spring Bean definition contain?

A

A Spring Bean definition contains all configuration metadata which is needed for the container to know how to create a bean, its lifecycle details and its dependencies.

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

How do you provide configuration metadata to the Spring Container?

A

There are three important methods to provide configuration metadata to the Spring Container: • XML based configuration file. • Annotation-based configuration • Java-based configuration

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

How do you define the scope of a bean?

A

When defining a in Spring, we can also declare a scope for the bean. It can be defined through the scope attribute in the bean definition. For example, when Spring has to produce a new bean instance each time one is needed, the bean’s scope attribute to beprototype. On the other hand, when the same instance of a bean must be returned by Spring every time it is needed, the the bean scope attribute must be set to singleton.

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

Explain the bean scopes supported by Spring

A

There are five scoped provided by the Spring Framework supports following five scopes: • In singleton scope, Spring scopes the bean definition to a single instance per Spring IoC container. • In prototype scope, a single bean definition has any number of object instances. • In request scope, a bean is defined to an HTTP request. This scope is valid only in a web-aware Spring ApplicationContext. • In session scope, a bean definition is scoped to an HTTP session. This scope is also valid only in a web-aware Spring ApplicationContext. • In global-session scope, a bean definition is scoped to a global HTTP session. This is also a case used in a web-aware Spring ApplicationContext. The default scope of a Spring Bean is Singleton.

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

Are Singleton beans thread safe in Spring Framework?

A

No, singleton beans are not thread-safe in Spring framework.

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

Explain Bean lifecycle in Spring framework

A

• The spring container finds the bean’s definition from the XML file and instantiates the bean. • Spring populates all of the properties as specified in the bean definition (DI). • If the bean implements BeanNameAware interface, spring passes the bean’s id tosetBeanName() method. • If Bean implements BeanFactoryAware interface, spring passes the beanfactory tosetBeanFactory() method. • If there are any bean BeanPostProcessors associated with the bean, Spring callspostProcesserBeforeInitialization() method. • If the bean implements IntializingBean, its afterPropertySet() method is called. If the bean has init method declaration, the specified initialization method is called. • If there are any BeanPostProcessors associated with the bean, their postProcessAfterInitialization() methods will be called. • If the bean implements DisposableBean, it will call the destroy() method.

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

Which are the important beans lifecycle methods? Can you override them?

A

There are two important bean lifecycle methods. The first one is setup which is called when the bean is loaded in to the container. The second method is the teardown method which is called when the bean is unloaded from the container. The bean tag has two important attributes (init-method and destroy-method) with which you can define your own custom initialization and destroy methods. There are also the correspondive annotations(@PostConstruct and @PreDestroy).

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

What are inner beans in Spring?

A

When a bean is only used as a property of another bean it can be declared as an inner bean. Spring’s XML-based configuration metadata provides the use of element inside the or elements of a bean definition, in order to define the so-called inner bean. Inner beans are always anonymous and they are always scoped as prototypes.

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

How can you inject a Java Collection in Spring?

A

Spring offers the following types of collection configuration elements: • The type is used for injecting a list of values, in the case that duplicates are allowed. • The type is used for wiring a set of values but without any duplicates. • The type is used to inject a collection of name-value pairs where name and value can be of any type. • The type can be used to inject a collection of name-value pairs where the name and value are both Strings.

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

What is bean wiring?

A

Wiring, or else bean wiring is the case when beans are combined together within the Spring container. When wiring beans, the Spring container needs to know what beans are needed and how the container should use dependency injection to tie them together.

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

What is bean auto wiring?

A

The Spring container is able to autowire relationships between collaborating beans. This means that it is possible to automatically let Spring resolve collaborators (other beans) for a bean by inspecting the contents of the BeanFactory without using and elements.

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

Explain different modes of auto wiring?

A

The autowiring functionality has five modes which can be used to instruct Spring container to use autowiring for dependency injection: • no: This is default setting. Explicit bean reference should be used for wiring. • byName: When autowiring byName, the Spring container looks at the properties of the beans on which autowire attribute is set to byName in the XML configuration file. It then tries to match and wire its properties with the beans defined by the same names in the configuration file. • byType: When autowiring by datatype, the Spring container looks at the properties of the beans on which autowire attribute is set to byType in the XML configuration file. It then tries to match and wire a property if its type matches with exactly one of the beans name in configuration file. If more than one such beans exist, a fatal exception is thrown. • constructor: This mode is similar to byType, but type applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised. • autodetect: Spring first tries to wire using autowire by constructor, if it does not work, Spring tries to autowire by byType.

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

Are there limitations with autowiring?

A

Limitations of autowiring are: • Overriding: You can still specify dependencies using and settings which will always override autowiring. • Primitive data types: You cannot autowire simple properties such as primitives, Strings, and Classes. • Confusing nature: Autowiring is less exact than explicit wiring, so if possible prefer using explicit wiring.

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

Can you inject null and empty string values in Spring?

A

Yes, you can.

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

What is Spring Java-Based Configuration? Give some annotation example.

A

Java based configuration option enables you to write most of your Spring configuration without XML but with the help of few Java-based annotations. An example is the @Configuration annotation, that indicates that the class can be used by the Spring IoC container as a source of bean definitions. Another example is the@Beanannotated method that will return an object that should be registered as a bean in the Spring application context.

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

What is Annotation-based container configuration?

A

An alternative to XML setups is provided by annotation-based configuration which relies on the bytecode metadata for wiring up components instead of angle-bracket declarations. Instead of using XML to describe a bean wiring, the developer moves the configuration into the component class itself by using annotations on the relevant class, method, or field declaration.

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

How do you turn on annotation wiring?

A

Annotation wiring is not turned on in the Spring container by default. In order to use annotation based wiring we must enable it in our Spring configuration file by configuring element.

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

@Required annotation

A

This annotation simply indicates that the affected bean property must be populated at configuration time, through an explicit property value in a bean definition or through autowiring. The container throws BeanInitializationException if the affected bean property has not been populated.

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

@Autowired annotation

A

The @Autowired annotation provides more fine-grained control over where and how autowiring should be accomplished. It can be used to autowire bean on the setter method just like @Required annotation, on the constructor, on a property or pn methods with arbitrary names and/or multiple arguments

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

@Qualifier annotation

A

When there are more than one beans of the same type and only one is needed to be wired with a property, the @Qualifier annotation is used along with @Autowired annotation to remove the confusion by specifying which exact bean will be wired.

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

How can JDBC be used more efficiently in the Spring framework?

A

When using the Spring JDBC framework the burden of resource management and error handling is reduced. So developers only need to write the statements and queries to get the data to and from the database. JDBC can be used more efficiently with the help of a template class provided by Spring framework, which is the JdbcTemplate (example here).

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

JdbcTemplate class provides many convenience methods for doing things such as converting database data into primitives or objects, executing prepared and callable statements, and providing custom database error handling.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q
  1. Spring DAO support
A

The Data Access Object (DAO) support in Spring is aimed at making it easy to work with data access technologies like JDBC, Hibernate or JDO in a consistent way. This allows us to switch between the persistence technologies fairly easily and to code without worrying about catching exceptions that are specific to each technology

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q
  1. What are the ways to access Hibernate by using Spring?
A

There are two ways to access Hibernate with Spring:Inversion of Control with a Hibernate Template and Callback.Extending HibernateDAOSupport and Applying an AOP Interceptor node.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q
  1. ORM’s Spring support
A

Spring supports the following ORM’s: Hibernate, iBatis, JPA(Java Persistence API), TopLink, JDO (Java Data Objects), OJB

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q
  1. How can we integrate Spring and Hibernate using HibernateDaoSupport?
A

Use Spring’s SessionFactory called LocalSessionFactory. The integration process is of 3 steps:Configure the Hibernate SessionFactoryExtend a DAO Implementation from HibernateDaoSupportWire in Transaction Support with AOP

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q
  1. Types of the transaction management Spring support
A

Spring supports two types of transaction management:Programmatic transaction management: This means that you have managed the transaction with the help of programming. That gives you extreme flexibility, but it is difficult to maintain.Declarative transaction management: This means you separate transaction management from the business code. You only use annotations or XML based configuration to manage the transactions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q
  1. What are the benefits of the Spring Framework’s transaction management?
A

It provides a consistent programming model across different transaction APIs such as JTA, JDBC, Hibernate, JPA, and JDO.It provides a simpler API for programmatic transaction management than a number of complex transaction APIs such as JTA.It supports declarative transaction management.It integrates very well with Spring’s various data access abstractions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q
  1. Which Transaction management type is more preferable?
A

Most users of the Spring Framework choose declarative transaction management because it is the option with the least impact on application code, and hence is most consistent with the ideals of a non-invasive lightweight container. Declarative transaction management is preferable over programmatic transaction management though it is less flexible than programmatic transaction management, which allows you to control transactions through your code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q
  1. Explain AOP
A

Aspect-oriented programming, or AOP, is a programming technique that allows programmers to modularize crosscutting concerns, or behavior that cuts across the typical divisions of responsibility, such as logging and transaction management.

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

The core construct of AOP is the aspect, which encapsulates behaviors affecting multiple classes into reusable modules. It ia a module which has a set of APIs providing cross-cutting requirements. For example, a logging module would be called AOP aspect for logging. An application can have any number of aspects depending on the requirement. In Spring AOP, aspects are implemented using regular classes annotated with the @Aspect annotation (@AspectJ style).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q
  1. What is the difference between concern and cross-cutting concern in Spring AOP
A

The Concern is behavior we want to have in a module of an application. A Concern may be defined as a functionality we want to implement.The cross-cutting concern is a concern which is applicable throughout the application and it affects the entire application. For example, logging, security and data transfer are the concerns which are needed in almost every module of an application, hence they are cross-cutting concerns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q
  1. Join point
A

The join point represents a point in an application where we can plug-in an AOP aspect. It is the actual place in the application where an action will be taken using Spring AOP framework.

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

The advice is the actual action that will be taken either before or after the method execution. This is actual piece of code that is invoked during the program execution by the Spring AOP framework.Spring aspects can work with five kinds of advice:before: Run advice before the a method execution.after: Run advice after the a method execution regardless of its outcome.after-returning: Run advice after the a method execution only if method completes successfully.after-throwing: Run advice after the a method execution only if method exits by throwing an exception.around: Run advice before and after the advised method is invoked.

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

The pointcut is a set of one or more joinpoints where an advice should be executed. You can specify pointcuts using expressions or patterns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q
  1. What is Introduction?
A

An Introduction allows us to add new methods or attributes to existing classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q
  1. What is Target object?
A

The target object is an object being advised by one or more aspects. It will always be a proxy object. It is also referred to as the advised object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q
  1. What is a Proxy?
A

A proxy is an object that is created after applying advice to a target object. When you think of client objects the target object and the proxy object are the same.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q
  1. What are the different types of AutoProxying?
A

BeanNameAutoProxyCreatorDefaultAdvisorAutoProxyCreatorMetadata autoproxying

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q
  1. What is Weaving? What are the different points where weaving can be applied?
A

Weaving is the process of linking aspects with other application types or objects to create an advised object.Weaving can be done at compile time, at load time, or at runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
62
Q
  1. Explain XML Schema-based aspect implementation?
A

In this implementation case, aspects are implemented using regular classes along with XML based configuration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
63
Q
  1. Explain annotation-based (@AspectJ based) aspect implementation
A

This implementation case (@AspectJ based implementation) refers to a style of declaring aspects as regular Java classes annotated with Java 5 annotations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
64
Q
  1. What is Spring MVC framework?
A

Spring comes with a full-featured MVC framework for building web applications. Although Spring can easily be integrated with other MVC frameworks, such as Struts, Spring’s MVC framework uses IoC to provide a clean separation of controller logic from business objects. It also allows to declaratively bind request parameters to business objects.

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

The Spring Web MVC framework is designed around a DispatcherServlet that handles all the HTTP requests and responses.

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

The WebApplicationContext is an extension of the plain ApplicationContext that has some extra features necessary for web applications. It differs from a normal ApplicationContextin that it is capable of resolving themes, and that it knows which servlet it is associated with.

67
Q
  1. What is Controller in Spring MVC framework?
A

Controllers provide access to the application behavior that you typically define through a service interface. Controllers interpret user input and transform it into a model that is represented to the user by the view. Spring implements a controller in a very abstract way, which enables you to create a wide variety of controllers.

68
Q
  1. @Controller annotation
A

The @Controller annotation indicates that a particular class serves the role of a controller. Spring does not require you to extend any controller base class or reference the Servlet API.

69
Q
  1. @RequestMapping annotation
A

@RequestMapping annotation is used to map a URL to either an entire class or a particular handler method.

70
Q

Dependency Injection

A

application classes should be as independent as possible of other Java classes to increase the possibility to reuse these classes and to test them independently of other classes while doing unit testing. Dependency Injection helps in gluing these classes together and same time keeping them independent.What is dependency injection exactly? Let’s look at these two words separately. Here the dependency part translates into an association between two classes. For example, class A is dependent on class B. Now, let’s look at the second part, injection. All this means is that class B will get injected into class A by the IoC.Dependency injection can happen in the way of passing parameters to the constructor or by post-construction using setter methods. As Dependency Injection is the heart of Spring Framework, so I will explain this concept in a separate chapter with a nice example.

71
Q

what is the core container of spring and what are the elements that make it up

A

The Core module provides the fundamental parts of the framework, including the IoC and Dependency Injection features.The Bean module provides BeanFactory which is a sophisticated implementation of the factory pattern.The Context module builds on the solid base provided by the Core and Beans modules and it is a medium to access any objects defined and configured. The ApplicationContext interface is the focal point of the Context module.The SpEL module provides a powerful expression language for querying and manipulating an object graph at runtime.

72
Q

the Data access/integration layer and what is it made up of

A

The JDBC module provides a JDBC-abstraction layer that removes the need to do tedious JDBC related coding.The ORM module provides integration layers for popular object-relational mapping APIs, including JPA, JDO, Hibernate, and iBatis.The OXM module provides an abstraction layer that supports Object/XML mapping implementations for JAXB, Castor, XMLBeans, JiBX and XStream.The Java Messaging Service JMS module contains features for producing and consuming messages.The Transaction module supports programmatic and declarative transaction management for classes that implement special interfaces and for all your POJOs.

73
Q

what is the web layer and what is it made up of

A

The Web module provides basic web-oriented integration features such as multipart file-upload functionality and the initialization of the IoC container using servlet listeners and a web-oriented application context.The Web-MVC module contains Spring’s model-view-controller (MVC) implementation for web applications.The Web-Socket module provides support for WebSocket-based, two-way communication between client and server in web applications.The Web-Portlet module provides the MVC implementation to be used in a portlet environment and mirrors the functionality of Web-Servlet module.

74
Q

Explain MVC

A

Model: Contains business logic and custom objectsView: Generates output representations for the userController: Sends commands to the model and the view and passes objects around where required

75
Q

What is Spring Web MVC?

A

“Spring’s web framework Implements the Front Controller pattern (Java EE pattern)All requests are addressed to a central servletThe servlet delegates requests to controller classesControllers do the request handling workIn Spring, this ““central servlet”” is known as the DispatcherServletFacilitates working with Servlets and JSPNo need to create a Servlet for each command”

76
Q

Why use Spring Web MVC?

A

Encourages use of best practices and SOLIDSeparation of concerns:-Each role (controller, validator, servlet, view, model objects, etc.) can be fulfilled by a specialized object.Simplification:-Easy to configure.-Far fewer servlet mappings.Form binding:-Allows us to bind an object to a Spring form directly.-Spring populates the object with form data automatically, instead of having to retrieve form parameters one by one.

77
Q

Spring MVC Architecture

A

1.DispatcherServlet receives the request.2.DispatcherServlet dispatches the task of selecting an appropriate controller to HandlerMapping. HandlerMapping selects the controller which is mapped to the incoming request URL and returns the (selected Handler) and Controller to DispatcherServlet.3.DispatcherServlet dispatches the task of executing of business logic of Controller to HandlerAdapter.4.HandlerAdapter calls the business logic process of Controller.5.Controller executes the business logic, sets the processing result in Model and returns the logical name of view to HandlerAdapter.6.DispatcherServlet dispatches the task of resolving the View corresponding to the View name to ViewResolver. ViewResolver returns the View mapped to View name.7.DispatcherServlet dispatches the rendering process to returned View.8.View renders Model data and returns the response

78
Q

DispatcherServlet

A

is a Spring-provided class that extends HttpServlet. Like any servlet, it must be mapped in web.xml

79
Q

@Controller @RequestMapping

A

DispatcherServlet delegates request handling to regular Java classes:@Controller marks these classes@RequestMapping maps a URL to a methodvalue:specifies mapped URL method:specifies HTTP methodMethods return a String (logical name of the view to respond with)

80
Q

How do we usually access a variable that has been submitted through a form?

A

“Usually we get form parameters from the HttpServletRequest object: req.getParameter(““paramName””);”

81
Q

How do we usually access a variable that has been submitted through a form with Spring MVC?

A

“Spring provides an alternate way using @RequestParam:@Controllerpublic class HomeController { @RequestMapping(““/register””) public String goToRegisterPage(@RequestParam String username){return ““register/”” + username; }}”

82
Q

@PathVariable

A

Binds a variable in the URL (a placeholder in the request mapping) to a method parameterUser-submitted data (such as a search term) can be displayed on the URL

83
Q

Spring Forms

A

Spring Forms is a feature that allows us to bind an object directly to a form on a view.

84
Q

Spring Form on a JSP

A

To use Spring Form custom tags on a JSP (e.g. registration.jsp), these taglib directives are required:Additionally, the JSP must be passed the modelAttribute object (to which to bind the form) when it is first loaded!

85
Q

Explain MVC

A

Model: Contains business logic and custom objectsView: Generates output representations for the userController: Sends commands to the model and the view and passes objects around where required

86
Q

What is Spring Web MVC?

A

“Spring’s web framework Implements the Front Controller pattern (Java EE pattern)All requests are addressed to a central servletThe servlet delegates requests to controller classesControllers do the request handling workIn Spring, this ““central servlet”” is known as the DispatcherServletFacilitates working with Servlets and JSPNo need to create a Servlet for each command”

87
Q

Why use Spring Web MVC?

A

Encourages use of best practices and SOLIDSeparation of concerns:-Each role (controller, validator, servlet, view, model objects, etc.) can be fulfilled by a specialized object.Simplification:-Easy to configure.-Far fewer servlet mappings.Form binding:-Allows us to bind an object to a Spring form directly.-Spring populates the object with form data automatically, instead of having to retrieve form parameters one by one.

88
Q

Spring MVC Architecture

A

1.DispatcherServlet receives the request.2.DispatcherServlet dispatches the task of selecting an appropriate controller to HandlerMapping. HandlerMapping selects the controller which is mapped to the incoming request URL and returns the (selected Handler) and Controller to DispatcherServlet.3.DispatcherServlet dispatches the task of executing of business logic of Controller to HandlerAdapter.4.HandlerAdapter calls the business logic process of Controller.5.Controller executes the business logic, sets the processing result in Model and returns the logical name of view to HandlerAdapter.6.DispatcherServlet dispatches the task of resolving the View corresponding to the View name to ViewResolver. ViewResolver returns the View mapped to View name.7.DispatcherServlet dispatches the rendering process to returned View.8.View renders Model data and returns the response

89
Q

DispatcherServlet

A

is a Spring-provided class that extends HttpServlet. Like any servlet, it must be mapped in web.xml

90
Q

@Controller @RequestMapping

A

DispatcherServlet delegates request handling to regular Java classes:@Controller marks these classes@RequestMapping maps a URL to a methodvalue:specifies mapped URL method:specifies HTTP methodMethods return a String (logical name of the view to respond with)

91
Q

How do we usually access a variable that has been submitted through a form?

A

“Usually we get form parameters from the HttpServletRequest object: req.getParameter(““paramName””);”

92
Q

How do we usually access a variable that has been submitted through a form with Spring MVC?

A

“Spring provides an alternate way using @RequestParam:@Controllerpublic class HomeController { @RequestMapping(““/register””) public String goToRegisterPage(@RequestParam String username){return ““register/”” + username; }}”

93
Q

@PathVariable

A

Binds a variable in the URL (a placeholder in the request mapping) to a method parameterUser-submitted data (such as a search term) can be displayed on the URL

94
Q

Spring Forms

A

Spring Forms is a feature that allows us to bind an object directly to a form on a view.

95
Q

Spring Form on a JSP

A

To use Spring Form custom tags on a JSP (e.g. registration.jsp), these taglib directives are required:Additionally, the JSP must be passed the modelAttribute object (to which to bind the form) when it is first loaded!

96
Q

What is inversion of control?

A

Inversion of control refers to a design in which custom software receives flow of control from a generic framework.

97
Q

What is Spring?

A

Spring is simply an inversion of control container. It was designed to reduce complexity of JEE, and allows developers to focus on business needs, testability, and scalability, rather than configuration code.

98
Q

What is the template method pattern in Spring?

A

“A design pattern in which methods common to several implementations are encapsulated in a common abstract class as a ““template”” that can be extended and further specialized.”

99
Q

What is a Java servlet?

A

A Java servlet is a Java program written to extend the capabilities of a server. Most commonly, they implement applications hosted on web servers

100
Q

What is a web container?

A

A web container is the part of a web server that interacts with servlets.

101
Q

What is MVC?

A

MVC stands for Model-View-Controller that is a pattern for implementing user interfaces. The Model describes the business logic independent of the user interface. The view can be any output representation of information. The controller accepts input and converts that input into commands regarding the model or view.

102
Q

What do we consider models to be in Spring?

A

Models are usually encapsulated classes of objects with fields and methods

103
Q

What is Spring MVC?

A

A web framework built around the principles of Spring. Just like Spring, it is POJO- and interface-driven. It includes support for RESTful services and Annotation-based configuration.

104
Q

How does the request/response lifecycle work with Spring?

A

A request hits our front controller, which then hands off the request to the correct controller. The correct controller handles the request via interaction via the back-end. The back-end hands back a representation (or model) of the data and gives it to the controller. The corresponding controller passes that data to the front controller, which delegates it to a view template to render the response. This response is handed back to the front controller which then delivers it to the browser

105
Q

What is the DispatcherServlet?

A

The entry/configuration point for our Spring MVC application

106
Q

What is a Controller in Spring MVC

A

It’s simply a POJO that handles a request and routes it to the proper view

107
Q

What is RequestMapping?

A

The uri and CRUD method that the handler method handles

108
Q

What is a Bean?

A

A bean is a POJO instantiated by Spring and configured using configuration meta-data that we provide

109
Q

What is a tiered application?

A

A tiered application is one that has concerns encapsulated into different tiers. A common tiered layer is a presentation layer, a business logic layer and a data(database) layer.

110
Q

Why is having a tiered application useful?

A

Encapsulation of concerns, reusability of layers, ease of refactoring layers without refactoring the entirety of the application.

111
Q

What are the layers of a Spring MVC application

A

The back-end consists of model objects often are part of a databases. The controller is the middle layer and often contains business logic for proper communication with the model or view. The view is the presentation-layer (or front-end) layer.

112
Q

What do controllers do?

A

They simply handle requests and responses and direct them to the proper handler. THEY DO NOT CONTAIN BUSINESS LOGIC. It is annotated with ‘@Controller’ and works with Services that do handle business logic. It DOES handle exceptions and routes view accordingly.

113
Q

What do services do?

A

Services often describe verbs or actions. All business logic should be here. It is where transactions begins and ensure data objects are in valid state. Annotated with @Service

114
Q

What is a repository?

A

A repository describes the nouns of the system. Focus on persisting and interacting with database. 1:1 mapping with objects and often database table.

115
Q

Describe some of the layers of the Spring Framework

A

The Spring Framework contains modules such that developers can pick and choose which to use in their Spring Application. Module categories include the core modules, the test module, data access/integration modules, web modules, and modules pertaining to aspects and AOP, instrumentation, and messaging

116
Q

Describe the Core and Beans Modules

A

The Core module provides the key features of inversion of control and dependency injection. The Bean module contains the BeanFactory, an implementation of the factory pattern that decouples object configuration from program logic.

117
Q

Describe the Context Module

A

The context modules builds on the core and beans modules and allows access to objects in a framework-style manner using the ApplicationContext interface.

118
Q

Describe the Expression Module

A

It provides an expression language for querying and manipulating object graphs at runtime.

119
Q

What is AOP and how do the AOP and Aspect modules of Spring help us?

A

AOP is Aspect-Oriented Programming in which aspects are the encapsulated base unit of an application. Aspects include concerns that cross multiple layers of the application, such as transaction management and security. The AOP module implements AOP functionality and the Aspects module allows integration with AspectJ, an AOP framework

120
Q

Describe terminology around AOP

A

Aspects are encapsulated concerns that play roles across classes. Aspects provide target objects (often proxies) with advice (actions). These actions often occur at very specific points in the program execution (e.g. at a specific method call) called join points. A target can be advised by one or more aspects. Particular advice is often associated with a point cut expression and runs at a join point that matches the point cut.

121
Q

Describe modules in the Data Access/Integration container

A

The JDBC module provides a JDBC abstraction layer, the transactions module provides support for transactions management, the ORM module provides integration layers for object relational mapping API’s such as Hibernate and JPA, the OXM module provides integration layers for object/XML mapping APIs, and the Java Messaging Service (JMS) module provides functionalities to produce and consume messages.

122
Q

Describe the WebLayer Container of Modules

A

The Web Module provides basic web integration features and the WebMVC module provides Spring’s MVC and RESTful web services implementation.

123
Q

Describe the Test Module

A

The test module provides unit testing and integration testing using JUnit

124
Q

What’s a bean?

A

A bean is an object that is instantiated, assembled and managed by a Spring container based on configuration metadata that we provide. Required configuration metadata is how to created the bean, its lifecycle, and its dependencies.

125
Q

What is a bean’s scope?

A

“A bean’s scope defines how it is created. For example, the instantiate a new instance of a bean each time one is created, use the ““prototype”” scope. There is also the singleton scope, the request scope for an HTTPRequest, the session scope for an HTTPSession scope, and a global-session scope.”

126
Q

What are initmethod and destroy-method with regards to Spring Beans

A

When beans are created, there may be some initialization steps required to get the bean into a useable state. The initmethod accomplishes this. Similarly, when a bean is not longer needed and requires cleanup, the destroy-method is run once the bean has been removed from the container.

127
Q

What is REST?

A

REST is an architectural style known as Representational State Transfer. Rather than providing direct access to back-end objects, a request can be responded to by a representation of data.

128
Q

What are the key aspects of a RESTful Web API?

A

Client-Server: a RESTful API should keep client and server concerns separate.Stateless: the server should not need to remember the state of the client.Layered: a RESTful API should have multiple layersUniform Interface: Interactions are interface- not implementation-based.The last point is what distinguishes REST from many other architectures

129
Q

How is the Uniform Interface achieved?

A

Through abstractions such as resources, representations, URI’s and HTTP methods

130
Q

What is a resource?

A

Basically anything. It could be a video, image, some lines of text, etc.

131
Q

@SpringBootApplication Combines which other annotations?

A

Combines @Configuration@EngableAutoConfiguration@ComponentScan( CLASS )

132
Q

@EnableAutoConfiguration

A

Allows Spring to guess the configuration based on class path( CLASS )

133
Q

@Controller

A

Marks the class as a web controller, capable of handling requests( CLASS )

134
Q

@RestController

A

A combination of @Controller and @ ResponseBody, marks the class as a web controller capable of handling requests( CLASS )

135
Q

@ResponseBody

A

Makes spring bind the methods return value to the web response body(CLASS OR METHOD )

136
Q

@RequestMapping

A

Specify on the method in thecontroller, to map a HTTP request to the URL to this method.( METHOD )

137
Q

@RequestParam

A

Binds HTTP parameters into method arguments( PARAMETER )

138
Q

@PathVariable

A

Binds placeholder from the URI to the method parameter( PARAMETER )

139
Q

@ComponenetScan

A

Allows Spring to scan the package for the @Configuration classes( CLASS )

140
Q

@Confirguration

A

Marks a class a source of Bean definitions( CLASS )

141
Q

@Bean

A

Indicates that a method produces a bean to be managed b the Spring container ( METHOD )

142
Q

@Component

A

Turns the class into a Spring bean at the time auto-scan is run ( CLASS )

143
Q

@Service

A

Specialized version of the @Component, has no encapsulated state( CLASS )

144
Q

What is a URI? How is it different from the URL?

A

A URI is a uniform resource identifier and denotes a resource. A URI can contain a location, a body, or both. URL’s are a subset of URI’s that contain a location.

145
Q

@Autowired

A

Spring dependency injection wires an appropriate bean into the marked class member.(CLASS, FIELD, OR METHOD)

146
Q

@Lazy

A

Makes @Bean or @ Component be initialized on demand rather than eagerly( CLASS OR METHOD )

147
Q

@Qualifier

A

Filters what beans should used to @Autowire a field or parameter( CLASS FIELD OR METHOD )

148
Q

@Value

A

Indicates a default value expression for the field or parameter( CLASS FIELD OR METHOD )

149
Q

@Repository

A

All data base logic should be in DAO classes( CLASS )

150
Q

@ModelAttribute

A

Indicates that the argument should be retrieved from the model. If Not present in the model, the argument should be instantiated first then added( PARAMETER )

151
Q

What are two characteristics of HTTP requests can or can’t have?

A

Safety - safe methods are those that don’t change the state of the server.Idempotency - if a request is made once, the server state will not change if the same request is made multiple times afterwards.

152
Q

What are URI templates and how are they designated?

A

URI templates provide a common pattern for structuring URIs and are denoted with curly brackets {}. The part of the URI within the brackets is a path variable.

153
Q

@Required

A

Fail the configurations if the dependency cannot be injected(CLASS FIELD OR METHOD )

154
Q

What is the GET HTTP method? How does it differ from the HEAD method?

A

The GET method gets resource metadata AND its representation. The HEAD method only gets metadata and is often simply used to check it a resource is present.

155
Q

What is the DELETE method

A

The delete method requests that a resource be deleted.

156
Q

What is the difference between PUT and PATCH?

A

PUT does not require that the entire representation of the resource be included as part of the request, but PATCH does.

157
Q

Explain how Spring implements AOP

A

Aspects are encapsulated concerns that are weaved into other objects. Aspects are designated to target beans. When a target method on a bean is called, a proxy created by Spring intercepts the call, applies the aspect logic and the then invokes the target bean method.

158
Q

What does each component of Spring MVC do?

A

The model provides the data or state of the application. The view provides the visual representation of the model. The controller handles user inputs and converts them to model changes, and vice versa.

159
Q

What is often the difference between a server and a repository in a Spring Application?

A

A service provides the business logic and computations of an application. It is annotated with a service annotation. A repository is an implementation of the persistence layer responsible for offering data access operations to the service layer. There are other implementations of a service layer such as JDBC.

160
Q

What is a Data Access Object (DAO)?

A

A data access object is an object or interface that provides access to representations of resources in a persistence layer like a database.

161
Q

What is the difference between a DAO and a Repository?

A

While both provide an abstraction for accessing the persistence layer, the DAO is an abstraction that is closer to persistence and might be considered table-centric. It is supposed to map to the underlying persistence layer. Repository is an abstraction of a collection of objects. They are basically similar, but not quite identical, patterns used to achieve the data access layer. A repository is like a collection but with a querying ability. The DAO gets an object state, and a repository can use a DAO to get the business object.

162
Q

What is a stereotype?

A

A stereotype denotes the responsibilities of a class or interface.

163
Q

What are some of the attributes of the @RequestMapping annotation

A

The value defines the endpoint that will be mapped to the handler. The method designates that the handler method may only perform specific HTTP request methods (if none is specified, the default is GET). Produces and Consumes narrows media type to a particular type produced or consumed.

164
Q

What is advice and what are the kinds of advice?

A

Advice is an action that is taken before or after a method is executed. It is a piece of code that is invoked during the programs execution.Types : Before - Runs advice before method executionAfter - Runs advice after execution, regardless of outcomeAfter-Returning - Runs advice after a method execution only if method completes successfullyAfter-Throwing - Runs advice after a methods execution only if method exits by throwing an exceptionAround - Run advice before and after the method is invoked