Spring from A to ?
Introduction to Spring Framework
Spring Framework is a Java platform that provides comprehensive infrastructure
support for developing Java applications. Spring handles the infrastructure so you
can focus on your application.
SOLID
 Single responsibility principle
 Open/closed principle
 Liskov Substitution Principle
 Interface Segregation Principle
 Dependency inversion principle
Modules
Dependency Injection and Inversion of
Control
 IoC is also known as dependency
injection (DI).
 Dependency injection (DI) is a process
whereby objects define their dependencies
 In Spring, the objects that form the
backbone of your application and that are
managed by the Spring IoC container are
called beans.
 Interface org.springframework.context.Ap
plicationContext represents the Spring IoC
containe
Bean overview
Property Explained in...
class Ex: com.tcbs.OrderService
name
camel-cased from then on. Examples of such names would be (without
quotes) 'accountManager','accountService', 'userDao', 'loginController'
scope singleton, prototype, request, session, global session
constructor arguments
properties
autowiring mode Name,type, constructor
lazy-initialization mode
ApplicationContext implementations eagerly create and configure all singleton beans as part of
the initialization process
initialization method
destruction method
Bean scopes
Scope Description
singleton
(Default) Scopes a single bean definition to a single
object instance per Spring IoC container.
prototype
Scopes a single bean definition to any number of object
instances.
request
Scopes a single bean definition to the lifecycle of a
single HTTP request; that is, each HTTP request has its
own instance of a bean created off the back of a single
bean definition. Only valid in the context of a web-
aware Spring ApplicationContext.
session
Scopes a single bean definition to the lifecycle of an
HTTP Session. Only valid in the context of a web-aware
Spring ApplicationContext.
global session
Scopes a single bean definition to the lifecycle of a
global HTTP Session. Typically only valid when used in a
portlet context. Only valid in the context of a web-
aware Spring ApplicationContext.
Autowiring collaborators
Mode Explanation
no
(Default) No autowiring. Bean references must be defined via a ref element. Changing the default
setting is not recommended for larger deployments, because specifying collaborators explicitly gives
greater control and clarity. To some extent, it documents the structure of a system.
byName
Autowiring by property name. Spring looks for a bean with the same name as the property that needs
to be autowired. For example, if a bean definition is set to autowire by name, and it contains
a master property (that is, it has a setMaster(..) method), Spring looks for a bean definition
named master, and uses it to set the property.
byType
Allows a property to be autowired if exactly one bean of the property type exists in the container. If
more than one exists, a fatal exception is thrown, which indicates that you may not
use byType autowiring for that bean. If there are no matching beans, nothing happens; the property
is not set.
constructor
Analogous to byType, but applies to constructor arguments. If there is not exactly one bean of the
constructor argument type in the container, a fatal error is raised.
AOP Concepts
 Aspect – a standard code/feature that is scattered
across multiple places in the application and is
typically different than the actual Business Logic
(for example, Transaction management). Each
aspect focuses on a specific cross-cutting
functionality
 Joinpoint – it’s a particular point during execution
of programs like method execution, constructor
call, or field assignment
 Advice – the action taken by the aspect in a
specific joinpoint
 Pointcut – a regular expression that matches a
joinpoint. Each time any join point matches a
pointcut, a specified advice associated with that
pointcut is executed
 Weaving – the process of linking aspects with
targeted objects to create an advised object
Proxying mechanisms
 Spring AOP uses either JDK dynamic
proxies
 or CGLIB to create the proxy for a given
target object
 All of the interfaces implemented by the
target type will be proxied. If the target
object does not implement any interfaces
then a CGLIB proxy will be created.
Spring Expression Language (SpEL)
Spring annotations
Spring annotations
 @Service: Annotate all your service classes with @Service. All your business logic
should be in Service classes.
 @Repository: Annotate all your DAO classes with @Repository. All your database
access logic should be in DAO classes.
 @Component: Annotate your other components (for example REST resource
classes) with @Component.
 @Autowired: Let Spring auto-wire other beans into your classes using @Autowired
annotation.
 @Transactional: Configure your transactions with @Transactional spring
annotation.
 @Scope: As with Spring-managed components in general, the default and most
common scope for autodetected components is singleton. To change this default
behavior, use @Scope spring annotation.
Spring annotations
 @Controller: Annotate your controller classes with @Controller.
 @RequestMapping: spring annotation to map URLs onto an entire class or a
particular handler method.
 @PathVariable: spring annotation on a method argument to bind it to the value of
a URI template variable.
 @RequestParam: You can bind request parameters to method variables using
spring annotation @RequestParam.
 @ModelAttribute: An @ModelAttribute on a method argument indicates the
argument should be retrieved from the model.
 @SessionAttributes: spring annotation declares session attributes. This will
typically list the names of model attributes which should be transparently stored
in the session, serving as form-backing beans between subsequent requests.
Spring annotations
 @Bean(scope=DefaultScopes.PROTOTYPE): The DefaultScopes class
provides string constants for each of these four scopes. SINGLETON is
the default, and can be overridden by supplying the scope attribute to
@Bean annotation
 @Qualifier: Qualifie with the name of the specific implementation.
Springboot
 https://start.spring.io/

Spring from a to Z

  • 1.
  • 2.
    Introduction to SpringFramework Spring Framework is a Java platform that provides comprehensive infrastructure support for developing Java applications. Spring handles the infrastructure so you can focus on your application.
  • 3.
    SOLID  Single responsibilityprinciple  Open/closed principle  Liskov Substitution Principle  Interface Segregation Principle  Dependency inversion principle
  • 4.
  • 5.
    Dependency Injection andInversion of Control  IoC is also known as dependency injection (DI).  Dependency injection (DI) is a process whereby objects define their dependencies  In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans.  Interface org.springframework.context.Ap plicationContext represents the Spring IoC containe
  • 6.
    Bean overview Property Explainedin... class Ex: com.tcbs.OrderService name camel-cased from then on. Examples of such names would be (without quotes) 'accountManager','accountService', 'userDao', 'loginController' scope singleton, prototype, request, session, global session constructor arguments properties autowiring mode Name,type, constructor lazy-initialization mode ApplicationContext implementations eagerly create and configure all singleton beans as part of the initialization process initialization method destruction method
  • 7.
    Bean scopes Scope Description singleton (Default)Scopes a single bean definition to a single object instance per Spring IoC container. prototype Scopes a single bean definition to any number of object instances. request Scopes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web- aware Spring ApplicationContext. session Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext. global session Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid in the context of a web- aware Spring ApplicationContext.
  • 8.
    Autowiring collaborators Mode Explanation no (Default)No autowiring. Bean references must be defined via a ref element. Changing the default setting is not recommended for larger deployments, because specifying collaborators explicitly gives greater control and clarity. To some extent, it documents the structure of a system. byName Autowiring by property name. Spring looks for a bean with the same name as the property that needs to be autowired. For example, if a bean definition is set to autowire by name, and it contains a master property (that is, it has a setMaster(..) method), Spring looks for a bean definition named master, and uses it to set the property. byType Allows a property to be autowired if exactly one bean of the property type exists in the container. If more than one exists, a fatal exception is thrown, which indicates that you may not use byType autowiring for that bean. If there are no matching beans, nothing happens; the property is not set. constructor Analogous to byType, but applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised.
  • 9.
    AOP Concepts  Aspect– a standard code/feature that is scattered across multiple places in the application and is typically different than the actual Business Logic (for example, Transaction management). Each aspect focuses on a specific cross-cutting functionality  Joinpoint – it’s a particular point during execution of programs like method execution, constructor call, or field assignment  Advice – the action taken by the aspect in a specific joinpoint  Pointcut – a regular expression that matches a joinpoint. Each time any join point matches a pointcut, a specified advice associated with that pointcut is executed  Weaving – the process of linking aspects with targeted objects to create an advised object
  • 10.
    Proxying mechanisms  SpringAOP uses either JDK dynamic proxies  or CGLIB to create the proxy for a given target object  All of the interfaces implemented by the target type will be proxied. If the target object does not implement any interfaces then a CGLIB proxy will be created.
  • 11.
  • 12.
  • 13.
    Spring annotations  @Service:Annotate all your service classes with @Service. All your business logic should be in Service classes.  @Repository: Annotate all your DAO classes with @Repository. All your database access logic should be in DAO classes.  @Component: Annotate your other components (for example REST resource classes) with @Component.  @Autowired: Let Spring auto-wire other beans into your classes using @Autowired annotation.  @Transactional: Configure your transactions with @Transactional spring annotation.  @Scope: As with Spring-managed components in general, the default and most common scope for autodetected components is singleton. To change this default behavior, use @Scope spring annotation.
  • 14.
    Spring annotations  @Controller:Annotate your controller classes with @Controller.  @RequestMapping: spring annotation to map URLs onto an entire class or a particular handler method.  @PathVariable: spring annotation on a method argument to bind it to the value of a URI template variable.  @RequestParam: You can bind request parameters to method variables using spring annotation @RequestParam.  @ModelAttribute: An @ModelAttribute on a method argument indicates the argument should be retrieved from the model.  @SessionAttributes: spring annotation declares session attributes. This will typically list the names of model attributes which should be transparently stored in the session, serving as form-backing beans between subsequent requests.
  • 15.
    Spring annotations  @Bean(scope=DefaultScopes.PROTOTYPE):The DefaultScopes class provides string constants for each of these four scopes. SINGLETON is the default, and can be overridden by supplying the scope attribute to @Bean annotation  @Qualifier: Qualifie with the name of the specific implementation.
  • 16.

Editor's Notes

  • #5 https://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/overview.html