Application/Spring

The first version was written by Rod Johnson who released in October 2002. The framework was first released under the Apache 2.0 license in June 2003. The Spring 1.2.6 framework won a Jolt productivity award and a JAX Innovation Award in 2006. It is open source and works with Java and .NET
== Spring Framework ==
Web-application typically has three layers
# User interface layer
# Business Logic Layer
# Data Access Layer
Layers depend on each other, especially UI on BL, and BL on DA. Typically these applications are tightly coupled, and very interdependent, which is considered bad thing. Spring Framework provides the opertunity to simply "inject" dependency, so that they are just loosly coupled, and the impact of of change in one component on another will be minimalized. 
BAckbones of Spring Framework are beans, bean is an object that is created, and managed by the Spring Framework.
== Spring Boot ==
MAkes developing web-applications and microservices with spring boot faster and easier via three ways:
What are Microservices? Is a design pattern in which each microservice is just one small piece of a bigger overall system. Each microservice performs a specific and limited scope task that contributed to the end-result. Key is to design system by planning distinct sub-tasks.
Autoconfiguration, applications are initialized with pre-set dependencies that don't have to be configured manually, but based on settings. Concerns underlying Spring FrameWork and third party packages.
Optinionated Approach, following its own judgement, Spring Boot chooses to add and configure starting dependencies, which packages to install and default values to use, based on needs of project
Standalone applications, embeds webserver such as TomCat and Netty into app during initialization process, removes neccesity to rely on external web-server.
== Bean Factory VS Application Context ==
=== Application Context extends BeanFactory ===
The Spring Framework comes with two IOC containers – BeanFactory and ApplicationContext. The BeanFactory is the most basic version of IOC containers, and the ApplicationContext extends the features of BeanFactory.
==== Lazy Loading via BeanFactory (on demand) ====
==== Eager Loading With ApplicationContext (load all the beans instantly) ====
== Dependency Injection ==
Dependency Injection is a design pattern: In object-oriented programming (OOP) software design, dependency injection is the process of supplying a resource that a given piece of code requires. The required resource, which is often a component of the application itself, is called a dependency.
=== IoC ===
What Is Inversion of Control? Inversion of Control is a principle in software engineering which transfers the control of objects or portions of a program to a container or framework. We most often use it in the context of object-oriented programming.
=== IoC Containers ===
The IoC container creates an object of the specified class and also injects all the dependency objects through a constructor, a property or a method at run time ...
== Spring HTTP ==
You can either use "RequestMapping" and specify a request method, or you can specify it by using the right annotation
=== Alternative A: RequestMapping ===
@Requestmapping(path = "/<path>", method = "RequestMethod.<GET,POST,DELETE,UPDATE>")
=== Alternative B:<Get,Post,Delete,Update>Mapping ===
@<Get,Post,Delete,Update>Mapping(path = "path")
== Data Access Exceptions ==
DataAccessException related to all exceptions thrown in SpringApplications
==Annotations==
===Beans===
====@Bean annotation====
The @Bean annotation defines a String bean with the id clientRepository. 
JpaClientRepository is the implementation class of the bean. 
The data source is injected (annotation based) and is not declared (file based) in this class.
==== Bean Injection ====
Different types of bean injection are: Constructor & Setter
==== Autowiring ====
byName, ByType, constructor, autodetect
===PostConstruct===
* The method annotated with @PostConstruct is called before the afterPropertiesSet callback method of the InitializingBean interface
* It was embedded by Spring Framework
===Ressource===
Embedded by Spring Framework
===PreDestroy===
Embedded by Spring Framework
===TransactionConfiguration===
Used to set the transaction manager if transactional support in the Spring Module is needed
depricated
==Application context==
# In a web application, the ContextLoaderListener is in charge to create an WebApplicationContext.
# In an integration test based on Spring, the SpringJUnit4ClassRunner creates the application context for you. The @ContextConfiguration annotation allows to specified application context configuration files.
# In a main method, you have to instanciated a class implementing the ApplicationContext interface (examples:ClassPathXmlApplicationContext or FileSystemXmlApplicationContext)
== Controller ==
Is an interface
==Spring FUnctionalities==
=== Modules ===
* AOP (Aspect ORiented Programming)
* O/R Integration
* Spring MVC (model View Controller), request based framework
Not:
* HTML/JSP
===Features===
* Resource management
* Exception handling
* Transaction participation
* Resource unwrapping
==Spring MVC==
Model View Controller: You create a Controller Class, link it to a Frontend Interface, and when a button is clicked or another event happens, you can specify in the controller what to execute)
Request based framework is a web framework that gets user's request then determine what the system should do and give back the response back to the user. So the flow is pretty much linear. You're thinking in actions: what does user want (request) -> what user will get back (response). Spring MVC is a request based framework
== Proxy ==
static or dynamic Proxy (JDC Dynamic Proxy or CGLIB Dynamic Proxy)
==Spring AOP==
AOP is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does this by adding additional behavior to existing code without modifying the code itself. Instead, we can declare the new code and the new behaviors separately.
In my words: You have a Business Object, and an Aspect. You want to extend the Business Object by new functionalities (e.g. logging, security, or more) without having to change the underlying business logic or class itself. You can do this by weaving
===Keywords===
====Business Object ====
A business object is a normal class that has a normal business logic. This class is a normal class with business logic, without any Spring-related annotations.
====Aspect====
An aspect is a modularization of a concern that cuts across multiple classes (example: Unified logging). It is a standard class, free of any Spring annotations.
====Pointcut====
A Pointcut is a predicate that helps match an Advice to be applied by an Aspect at a particular JoinPoint.
We often associate the Advice with a Pointcut expression, and it runs at any Joinpoint matched by the Pointcut.
==== Advice ====
An Advice is an action taken by an aspect at a particular Joinpoint. Different types of advice include “around,” “before,” and “after.”
In Spring, an Advice is modelled as an interceptor, maintaining a chain of interceptors around the Joinpoint.
====Weaving====
In Aspect oriented programming, linking Aspects with the other application types creates an Advised object. This process is known as Weaving. Without Weaving, we just have definition of Aspects. Weaving makes use realize full potential of the AOP.
Weaving can be done at Compile Time, Classload Time, and Runtime
=====How to Weave=====
Done: 1) Two beans, one for Business Object, one for Aspect (with id and class) -> asp:config -> aop:aspect -> point-cut , define excution/executed method(s) -> advice after-returning, defines method, returning, & point-cut-reference
===Proxies===
There are two types of proxies in Spring Boot: Dynamic and Static
====Static Proxy====
Exist before runtime
====Dynamic Proxy====
Dynamic proxies are a type of proxy that are created at runtime, based on an interface or set of interfaces. They allow you to create an object that implements a given interface, without having to write a concrete implementation of that interface yourself.
In the context of Spring Boot, dynamic proxies are often used to implement aspects, which are cross-cutting concerns that are applied to multiple parts of an application. Aspects can be used to add logging, security, or other functionality to an application without having to modify the core business logic.
There are two dynamic proxies in Spring Boot: JDC Dynamic Proxy or CGLIB Dynamic Proxy
==Main advantage of using interfaces when designing business services==
# With modern mock API like Mockito or EasyMock, interfaces are not mandatory for mocking or stubbing the service. But using interface remains easier when you have to manually mock the service in unit test. 
# Auto-injection is possible with class. Spring uses CGLIB.
# Dependency checking is an advantage of dependencies injection.
==Spring Test Module==
===Integration Tests===
====Why to use Spring Framework when writing Integration Tests?==== 
Spring ContextConfiguration can be inherited from the super class
# The Spring context is cached across tests unless you use @Dirties Context annotation
# With the Spring test module, dependency injection is available in test case. So you may autowired the bean to test
# By default, a @ContextConfiguration annoted class inherits the spring context configuration file locations defined by an annotated superclass. The inheritLocations of this attribute allows to change this default behavior.
# If no context configuration file is provided to the @ContextConfiguration annotation, Spring use a file convention naming. It try to load a file named with the test class name and suffices by the "-context.xml" suffice (i.e. MyDaoTest-context.xml)
====Advantages====
# Reuse Spring configuration files of the application
# Be able to use the rollback after the test pattern
# Use dependency injection
Not: Mocking or Stubbing, because that's more of a Unit Test thing, and Spring does not include any implementation or abstraction of Mock framework.
===Unit Tests===
====Advantages====
Main one: Reuse Spring configuration files of the application
False answers:
# You don't need Spring container to writer unit test
# Refer to the answer number 1.
# The org.springframework.mock package provides mock classes like MockHttpSession or MockHttpContext. They could be helpful for unit test in the presentation layer and when you don't use any mock framework such as Mockity or EasyMock.
==Live Environment==
* Constuctor and properties autowiring in the same bean are not compatible
* A bean should have a default or a no-args constructor
* The constructor-arg tag could take type, name and index to reduce ambiguity