Monday, November 12, 2018

JTD-DesignerSeries-12-SpringBasics-101


A Brief on JEE applications
Enterprise Applications can be categorized as large scale distributed, transactional, highly available applications designed to support mission critical business functions.
JEE applications are made of modules & components deployed in their appropriate containers that provide the execution environment along with management & control services.
Architecturally speaking, Distributed Applications are layered across multiple tiers [Web Tier, Application/Service Tier, Database Tier] and primarily based on Model 2 Model, View, Controller [MVC] pattern.

Out of necessity, Rod Johnson in 2003, releases modular spring framework under Apache 2.0 license to reduce the complexity of application development for J2EE developers.


Beans & Core modules apply principles of DI / IOC and delegates the responsibility of object creation to the BeanFactory. Context modules allows you to access the created objects based on configuration files.

Data Access modules helps you abstract lower level JDBC connection protocols & allows an easy management of complex features like transactions, ORM.

Spring Web modules provides implementation of Dispatcher Servlet, View Resolver, model objects, Request Mapping & Controller classes which can configured to implement complex web applications.


A Brief on IOC / DI
Generally, loose coupling is enabled by defining dependencies as Interfaces & delegating the initialization of dependent objects to a factory class. Factory class can then abstract the implementation based on configuration and enabling complex features like transactions, cacheing, logging etc. Let's say for example Service Layer [TransferServiceImpl] need access to DAO layer for database functions. [findAccount, updateAccount]. TransferServiceImpl can then define AccountRepository interface as a dependent object and can delegate the Spring factory to instantiate an appropriate implementation like JDBCAccountRepository or HibernateAccountRepository based on configuration.

public class TransferServiceImpl implements TransferService {
private AccountRepository acctRepository;
acctRepository = (AccountRepository) BeanFactory.getBean("repoClassImpl")
}

Spring Container, thus creates & manages bean lifecycle based on the configuration file and injects them as dependencies as needed. This pattern of giving responsibility to the container to create objects & injecting its dependencies based on the configuration files is referred as Inversion of Control / Dependency Injection.

beans.xml
<bean id="accountRepository" class="JDBCAccountRepository">
<constructor-arg ref="basicDataSource">
</bean>
<bean id="transferService" class="TransferServiceImpl">
<property name="accountRepository" ref="accountRepository">
</bean>

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml);
TransferService transferSrv = (TransferServiceImpl) ctx.getBean(""transferService");
transferSrv.transfer(fromAcct, toAcct, amount);

Spring container can inject dependencies either through constructor args, or with setter methods. Mandatory dependencies like datasources use constructor based injector approach whereas optional notification features can be injected as setter based dependencies.


A Brief on Maven
Maven, is a software project management tool based on the POM configuration file (pom.xml). It helps you easily manage a build lifecycle of software projects by running commands like mvn build, mvn package. Maven helps you pull dependencies from central repositories into your project by specifying groupId:artifactId:version. Typical Maven file structure:

Maven is based around a central concept of build lifecycle, and lifecycle is composed of build phases, and each phase can be an order set of instructions defined in terms goals executed with a plug-in execution engine. Some of the common build phases of default lifecycle are [validate, compile, test, package, verify, install, deploy]

No comments:

Post a Comment