Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Presentation By V AIBHAV S AHARAN Web-enHanced Information Management COMS E6125.

Similar presentations


Presentation on theme: "A Presentation By V AIBHAV S AHARAN Web-enHanced Information Management COMS E6125."— Presentation transcript:

1 A Presentation By V AIBHAV S AHARAN Web-enHanced Information Management COMS E6125

2 What is Spring? A Web Application Framework Multi-tier full stack solution Development within the J2EE guidelines Simple programming model, complex layer of tools Essence of Spring: provide services to reusable POJOs One stop shop for all enterprise requirements while still maintaining modularity Lightweight framework: less complexity, more interoperability Follows the Model-View-Controller architecture

3 Why Spring? Non-invasive Framework Consistent programming model, usable in any environment Promotes code reuse Promotes good programming practices, program to interfaces Facilitates extraction of config values into property\XML files Spring is consistent across the framework Promotes architectural choice Promotes pluggability

4 Spring Features Inversion of Control (IoC) container Aspect-Oriented Programming (AOP) Framework Data access abstraction Significant JDBC simplification Transaction management MVC Web Framework Simplification of working with JNDI, JTA etc. Support for Java Messaging Service (JMS) Support for comprehensive testing at every phase

5 Spring Modules

6 “Don't call us, we’ll call you” Inversion of Control – The Dependency Injection flavor Framework code invokes application code and coordinates overall workflow Callback interfaces to allow clean acquisition and release of resources Application classes expose their dependencies, as methods that the framework can call at runtime “Push” config: dependencies pushed on application objects Setter, Constructor and Method injection Allows a layer of indirection: facilitates hot swapping No dependency on container APIs

7 IoC Example – Setter Injection public interface WeatherService { Double getHistoricalHigh(Date date); } public class WeatherServiceImpl implements WeatherService { private WeatherDAO weatherDao; public void setWeatherDao(WeatherDAO weatherDao) { this.weatherDao = weatherDao; } public Double getHistoricalHigh(Date date) { WeatherData wd = weatherDao.find(date); if (wd != null) return new Double(wd.getHigh()); return null; } public class StaticDataWeatherDAOImpl implements WeatherDAO {... }

8 IoC Example – Setter Injection Add setter information to configuration file: applicationContext.xml <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

9 IoC Example – Constructor Injection public interface WeatherService { Double getHistoricalHigh(Date date); } public class WeatherServiceImpl implements WeatherService { private final WeatherDao weatherDao; public WeatherServiceImpl(WeatherDao weatherDao) { this.weatherDao = weatherDao; } public Double getHistoricalHigh(Date date) { WeatherData wd = weatherDao.find(date); if (wd != null) return new Double(wd.getHigh()); return null; } public class StaticDataWeatherDAOImpl implements WeatherDAO {... }

10 IoC Example – Setter Injection Add constructor information to configuration file: applicationContext.xml

11 The new OOP? Aspect-Oriented Programming Enables us to think about concerns or aspects of the system Concerns like transaction management, logging and failure monitoring Traditional OOP problems: code duplication, loose typing, intrusive special-purpose frameworks AOP facilitates cross-cutting modules applied declaratively with no tradeoffs Spring has a proxy-based approach to AOP Proxies maintain a chain of advice applying to methods Spring AOP provides an extensible pointcut model It is the future of middleware with a fixed set of services applied flexibly to POJOs

12 AOP Example public class AccountManagerImpl implements AccountManager { private MailSender mailSender; private SimpleMailMessage message; private AccountDao accountDao; public void setMailSender(MailSender mailSender) { this.mailSender = mailSender; } public void setMessage(SimpleMailMessage message) { this.message = message; } public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } private void sendMail(Exception ex) { SimpleMailMessage msg = new SimpleMailMessage(this.message); msg.setText("Encountered exception " + ex.getMessage()); this.mailSender.send(msg); }...

13 AOP Example public Account getAccount(String accountId) throws AccountNotFoundException, DataAccessException { try{ return this.accountDao.findAccount(accountId); } catch(AccountNotFoundException ex){ sendMail(ex); throw ex; } catch (DataAccessException ex) { sendMail(ex); throw ex; }...

14 AOP Example public void createAccount(Account account) throws DataAccessException, { try{ if (isInvalid(account)) { throw new InvalidAccountException(account); } this.accountDao.saveAccount(account); } catch (IOException ex) { sendMail(ex); throw ex; } catch (DataAccessException ex) { sendMail(ex); throw ex; } }

15 AOP Example An aspect class that implements the exception notification policy public class EmailNotificationThrowsAdvice implements ThrowsAdvice { private MailSender mailSender; private SimpleMailMessage message; public void setMailSender(MailSender mailSender) { this.mailSender = mailSender; } public void setMessage(SimpleMailMessage message) { this.message = message; } public void afterThrowing(Exception ex) throws Throwable { SimpleMailMessage msg = new SimpleMailMessage(this.message); msg.setText("Encountered exception " + ex.getMessage()); this.mailSender.send(msg); } }

16 AOP Example Now apply the advice to multiple objects managed by the Spring container: <bean id="emailNotificationThrowsAdvice" class="com.mycompany.EmailNotificationThrowsAdvice"> <bean id="accountManagerBeanNameProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> accountManagerTarget emailNotificationThrowsAdvice

17 AOP Example The same class now updated: public class AccountManagerImpl implements AccountManager { private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } public Account getAccount(String accountId) throws AccountNotFoundException, DataAccessException { return this.accountDao.findAccount(accountId); } public void createAccount(Account account) throws DataAccessException { this.accountDao.saveAccount(account); }

18 DAO Support and JDBC Spring provides abstract DAO base classes These facilitate access to database resources Provides a layer of abstraction over JDBC Focus is on connection setup and exception handling Spring provides an exception hierarchy for data access methods Hierarchy common to both JDBC and O/R mapping Promotes uniform exceptions across the application Facilitates portability

19 View Technologies Spring does not favor a particular view technology: view agnostic Supports myriad out of the box options as well as custom views All that is required: link logical view name to link resolver and create the resource Support for the ubiquitous JavaServer Pages Support for FreeMarker and Velocity templating tools Support for Tiles for more complex views with considerable layout Support for XML and XSLT based layouts Support for document-based views like Excel and PDFs

20 Thank You


Download ppt "A Presentation By V AIBHAV S AHARAN Web-enHanced Information Management COMS E6125."

Similar presentations


Ads by Google