Monday, August 24, 2020

Spring - AutoWiring

Spring automatically resolves the dependencies between the collaborating beans by inspecting the contents of the BeanFactory. This is called Autowiring.

Spring has 5 modes of autowiring:

1. no
    - default
    - means no autowiring
    - we have to use explicit bean reference for wiring

2. byName
    - autowiring by property name
    - The IoC container looks at the properties of the beans on which autowire attribute is set to byName in the XML configuration file

3. byType
    - autowiring by property datatype
    - The IoC container 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 bean exists, a fatal exception is thrown

4. constructor
    - similar to byType but type applies to constructor arguments.

5. autodetect
    - spring first tries to autowire by constructor, if it fails, spring tries to autowire by type.

Limitations of autowiring

- overriding possibility 
    dependencies can still be specified using <constructor-arg> and <property> setting which will override autowiring
- primitive datatypes
    we cannot autowire primitive datatypes
- less exact
    autowiring is less exact than explicit wiring

Sunday, August 23, 2020

Spring - The IoC Container

The Spring IoC Container creates the objects, wire them together, configure them and manage their complete lifecycle from creation till destruction.

There are two types of IoC containers:

1. Bean Factory Container
    - simplest container providing basic support for DI
    - proffered where resources are limited like mobile devices or applet based applications
    - e.g. XmlBeanFactory (this container reads the configuration metadata from an XML file)

2. Application Context Container
    - has more enterprise specific functionalities such as resolving textual messages from properties file and publishing application events to interested event listeners
    - e.g.
    - FileSystemXmlApplicationContext - load bean definition and configuration from an XML file. Need to provide full path of the xml file to the constructor.
    - ClassPathXmlApplicationContext - load bean definition and configuration from an XML file. The xml file should be present in the CLASSPATH
    - WebXmlApplicationContext - loads the XML file with definition of all beans from within a web application.

Spring Boot - Asynchronus Messaging (AMPQ)

Asynchronus messaging can reduce strain on the system
Spring uses RabbitMQ which is efficient for sending and receiving asynchronous messages.
Property based configuration
follows standard Rabbit consumption

Producer:
    Spring provides Rabbit Template
    Default configuration with spring boot
    Provide the exchange and queue name
    Allows to post messages as Object or String


Consumer:
    Spring provides listener implementations
    Response to messages in the queue and execute some logic

Spring - Boot - Data

Spring Data provides rich support for RDBMS and NoSQL databases.

Certain databases like H2/HSQLDB autoconfigure an embedded database

Leverage common scripts to prime embedded database





Saturday, August 22, 2020

Spring - Boot

Spring Boot 
    - supports rapid development
    - removes boilerplate of most of the application setup
 
Key components
    - Embedded Tomcat
    - Auto configuration of Application context
    - Automatic servlet mapping
    - Embedded database support and hibernate/JPA dialect
    - Automatic controller mapping

Spring - Aspects

Spring Aspects are blocks of code that can be injected into the application at runtime

example logging, transaction management, caching, security.


- Spring uses AspectJ (byte code modification - run time interweaving) for aspecting.


Parts of spring Aspect

- Aspect - a class that implements enterprise application concerns that cut across multiple classes.                         Aspects can be a normal class configured through Spring XML configuration or we can use                     Spring AspectJ integration to define a class as Aspect using @Aspect annotation.

- Join Point - the place or specific point in the application code flow where the aspect code will be applied

- Point Cut - a selection criteria to select the 'Join Point' to apply the aspect code.

- Advice - The advice is the aspect class method that will be applied at the 'Join Point' selected by the 'Point Cut'


Point Cut:

Syntax: designator("r p.c.m(arg)")

r- return type

p- package

c-class

m-method

arg-arguments

designator-there are several designators provided by spring which can be used to implement an aspect.

e.g. execution - for matching method execution

        within-for matching within certain types

        target-for matching a specific type

        @annotation-for matching a specific annotation



Wednesday, August 19, 2020

Spring Lifecycle

The three phases of lifecycle:

    1. Initialization

    - begins when the application context is created.

    - can be broken down in to further two phases

        a) BeanFactory Initialization

        b) Bean Initialization and Instantiation

    2. Use


    3. Destruction

    - begins when close is called on the application context.



Some examples of lifecycle methods:

@PostConstruct

a method which should be executed after construction of the object.


@PreDestroy

a method which should be called before marking the object for garbage collection.



Spring - AutoWiring

Spring automatically resolves the dependencies between the collaborating beans by inspecting the contents of the BeanFactory. This is called...