7 (or so) reasons to use spring · • grew from practical experience simple things should be...

112
7 (or so) Reasons to use Spring Arjen Poutsma SpringSource VMware woensdag 6 oktober 2010

Upload: others

Post on 09-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

7 (or so) Reasonsto use Spring

Arjen PoutsmaSpringSource

VMware

woensdag 6 oktober 2010

Page 2: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

About Spring

woensdag 6 oktober 2010

Page 3: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

Aims of Spring

• Reduce the complexity of Java J2EE development

• Simplify without sacrificing power

• Facilitate best practices

• Grew from practical experience

Simple things should be simple.

Complex things should be possible.

Alan Kay

woensdag 6 oktober 2010

Page 4: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

Technical Aims of Spring

• Write POJOs

• Apply Java EE services to POJOs

• Make it transactional

• Expose via JMX

• ...

woensdag 6 oktober 2010

Page 5: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

POJO development

• Plain Old Java Object

• Not bound to any environment

• No environment-specific imports

• Not dependent on lookup mechanism

• Dependencies are injected

• Prolongs life

•Test out of the container

woensdag 6 oktober 2010

Page 6: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

Modular

• Spring is not a “package deal”

• All features can be used independently

• Though they strengthen each other

woensdag 6 oktober 2010

Page 7: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Reason #1Dependency Injection

woensdag 6 oktober 2010

Page 8: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

The Case for Dependency Injection

• Applications consist of multiple components

• How obtain these dependencies?

woensdag 6 oktober 2010

Page 9: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

Pull Configuration

• JNDI lookup

• Properties file

• Service Locator anti-pattern

• Many issues

• Most importantly: hard to test

woensdag 6 oktober 2010

Page 10: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

Solution: Dependency Injection

• Rather than lookup dependencies

• Let something give them to me

• Spring

woensdag 6 oktober 2010

Page 11: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

How Spring Works

SpringApplication Context

woensdag 6 oktober 2010

Page 12: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

How Spring Works

SpringApplication Context

Your Application Classes (POJOs)

woensdag 6 oktober 2010

Page 13: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

How Spring Works

SpringApplication ContextConfiguration

Instructions

Your Application Classes (POJOs)

woensdag 6 oktober 2010

Page 14: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

How Spring Works

SpringApplication ContextConfiguration

Instructions

Your Application Classes (POJOs)

Fully configuredapplication system

Ready for use

Creates

woensdag 6 oktober 2010

Page 15: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Example

woensdag 6 oktober 2010

Page 16: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Examplepublic class TransferServiceImpl implements TransferService { public TransferServiceImpl(AccountRepository ar) { this.accountRepository = ar; } …}

woensdag 6 oktober 2010

Page 17: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Examplepublic class TransferServiceImpl implements TransferService { public TransferServiceImpl(AccountRepository ar) { this.accountRepository = ar; } …}

Needed to perform money transfersbetween accounts

woensdag 6 oktober 2010

Page 18: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Examplepublic class TransferServiceImpl implements TransferService { public TransferServiceImpl(AccountRepository ar) { this.accountRepository = ar; } …}

public class JdbcAccountRepository implements AccountRepository { public JdbcAccountRepository(DataSource ds) { this.dataSource = ds; } …}

Needed to perform money transfersbetween accounts

woensdag 6 oktober 2010

Page 19: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Examplepublic class TransferServiceImpl implements TransferService { public TransferServiceImpl(AccountRepository ar) { this.accountRepository = ar; } …}

public class JdbcAccountRepository implements AccountRepository { public JdbcAccountRepository(DataSource ds) { this.dataSource = ds; } …}

Needed to load accounts from the database

Needed to perform money transfersbetween accounts

woensdag 6 oktober 2010

Page 20: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Example

woensdag 6 oktober 2010

Page 21: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Example<beans>

<bean id=“transferService” class=“app.impl.TransferServiceImpl”> <constructor-arg ref=“accountRepository” /> </bean>

<bean id=“accountRepository” class=“app.impl.JdbcAccountRepository”> <constructor-arg ref=“dataSource” /> </bean>

<bean id=“dataSource” class=“com.oracle.jdbc.pool.OracleDataSource”> <property name=“URL” value=“jdbc:oracle:thin:@localhost:1521:BANK” /> <property name=“user” value=“moneytransfer-app” /> </bean>

</beans>

woensdag 6 oktober 2010

Page 22: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

“Spring Sucks!”

• Spring is XML

• XML is Evil

• Being Evil sucks

• Therefore, Spring sucks

woensdag 6 oktober 2010

Page 23: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

Spring != XML

• @Component

• @Autowired

• JSR-250

• @Resource

• @PostContruct/@PreDestroy

• @Configuration

woensdag 6 oktober 2010

Page 24: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

@Component @Autowired

@Componentpublic class TransferServiceImpl implements TransferService @Autowired public TransferServiceImpl(AccountRepository ar) { this.accountRepository = ar; } …}

woensdag 6 oktober 2010

Page 25: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

Why not use Java EE Dependency Injection?

• Testing, inside IDE

• Java EE only allows injection of JNDI-managed objects

• Spring injects everything

• Primitives (configuration)

woensdag 6 oktober 2010

Page 26: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Reason #2JdbcTemplate

woensdag 6 oktober 2010

Page 27: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

JDBC

• Object/Relational Mapping is popular

• But JDBC continues to be important

• Batch Operations

• Set-based operations

• Stored procedures

woensdag 6 oktober 2010

Page 28: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

20

Redundant, Error Prone Code

public List findByLastName(String lastName) { List personList = new ArrayList(); Connection conn = null; String sql = “select first_name, age from PERSON where last_name=?“; try { DataSource dataSource = DataSourceUtils.getDataSource(); conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, lastName); ResultSet rs = ps.executeQuery(); while (rs.next()) { String firstName = rs.getString(”first_name“); int age = rs.getInt(“age”); personList.add(new Person(firstName, lastName, age)); } } catch (Exception e) { /* ??? */ } finally { try { conn.close(); } catch (SQLException e) { /* ??? */ } } return personList;}

woensdag 6 oktober 2010

Page 29: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission21

Redundant, Error Prone Code

public List findByLastName(String lastName) { List personList = new ArrayList(); Connection conn = null; String sql = "select first_name, age from PERSON where last_name=?"; try { DataSource dataSource = DataSourceUtils.getDataSource(); conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, lastName); ResultSet rs = ps.executeQuery(); while (rs.next()) { String firstName = rs.getString(“first_name”); int age = rs.getInt(“age”); personList.add(new Person(firstName, lastName, age)); } } catch (Exception e) { /* ??? */ } finally { try { conn.close(); } catch (SQLException e) { /* ??? */ } } return personList;}

woensdag 6 oktober 2010

Page 30: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission21

Redundant, Error Prone Code

public List findByLastName(String lastName) { List personList = new ArrayList(); Connection conn = null; String sql = "select first_name, age from PERSON where last_name=?"; try { DataSource dataSource = DataSourceUtils.getDataSource(); conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, lastName); ResultSet rs = ps.executeQuery(); while (rs.next()) { String firstName = rs.getString(“first_name”); int age = rs.getInt(“age”); personList.add(new Person(firstName, lastName, age)); } } catch (Exception e) { /* ??? */ } finally { try { conn.close(); } catch (SQLException e) { /* ??? */ } } return personList;}

The bold matters - the rest is boilerplate

woensdag 6 oktober 2010

Page 31: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission22

JdbcTemplateint count = jdbcTemplate.queryForInt(

“SELECT COUNT(*) FROM CUSTOMER”);

woensdag 6 oktober 2010

Page 32: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission22

JdbcTemplate

• Acquisition of the connection

• Participation in the transaction

• Execution of the statement

• Processing of the result set

• Handling any exceptions

• Release of the connection

int count = jdbcTemplate.queryForInt( “SELECT COUNT(*) FROM CUSTOMER”);

All handled by Spring

woensdag 6 oktober 2010

Page 33: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Querying With SimpleJdbcTemplate

public int getCountOfPersonsOlderThan(int age) { return jdbcTemplate().queryForInt(

“select count(*) from PERSON where age > ?”, age);}

woensdag 6 oktober 2010

Page 34: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Domain Objects

public Person getPerson(int id) { return getSimpleJdbcTemplate().queryForObject( “select first_name, last_name from PERSON where id=?”, new PersonMapper(), id);}

woensdag 6 oktober 2010

Page 35: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Domain Objects

public Person getPerson(int id) { return getSimpleJdbcTemplate().queryForObject( “select first_name, last_name from PERSON where id=?”, new PersonMapper(), id);}

class PersonMapper implements ParameterizedRowMapper<Person> { public Person mapRow(ResultSet rs, int i) { return new Person(rs.getString(1), rs.getString(2)); }}

woensdag 6 oktober 2010

Page 36: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Reason #3Exception Hierarchy

woensdag 6 oktober 2010

Page 37: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission26

public List findByLastName(String lastName) { List personList = new ArrayList(); Connection conn = null; String sql = "select first_name, age from PERSON where last_name=?“; try { DataSource dataSource = DataSourceUtils.getDataSource(); conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, lastName); ResultSet rs = ps.executeQuery(); while (rs.next()) { String firstName = rs.getString(”first_name“); int age = rs.getInt(“age”); personList.add(new Person(firstName, lastName, age)); } } catch (Exception e) { /* ??? */ } finally { try { conn.close(); } catch (SQLException e) { /* ??? */ } } return personList;}

Poor Exception Handling

woensdag 6 oktober 2010

Page 38: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission26

public List findByLastName(String lastName) { List personList = new ArrayList(); Connection conn = null; String sql = "select first_name, age from PERSON where last_name=?“; try { DataSource dataSource = DataSourceUtils.getDataSource(); conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, lastName); ResultSet rs = ps.executeQuery(); while (rs.next()) { String firstName = rs.getString(”first_name“); int age = rs.getInt(“age”); personList.add(new Person(firstName, lastName, age)); } } catch (Exception e) { /* ??? */ } finally { try { conn.close(); } catch (SQLException e) { /* ??? */ } } return personList;}

Poor Exception Handling

What can you do?

woensdag 6 oktober 2010

Page 39: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

What can you do?

• Nothing

• Logging

• Wrapping

• Retry

woensdag 6 oktober 2010

Page 40: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

! } catch (SQLException ex) {! ! if (ex.getErrorCode() == 60) {// check for ORA-00060! ! ! return findByLastName(lastName);! ! } else {! ! ! throw ex;! ! }! }

Dead Lock Loser

woensdag 6 oktober 2010

Page 41: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

Retry

• Ugly code

• Not portable

• JDBC vs JPA

• Oracle vs SQL Server

woensdag 6 oktober 2010

Page 42: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

In Spring

• Spring does exception translation

• Into a rich Exception Hierarchy

• Catch DeadLockLoserException

• Use AOP!

woensdag 6 oktober 2010

Page 43: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

DataAccessException Hierarchy (subset)

DataRetrievalFailureException

DataAccessResourceFailureException

CleanupFailureDataAccessException

InvalidDataAccessApiUsageException

InvalidDataAccessResourceUsageException

IncorrectUpdateSemanticsDataAccessException

TypeMismatchDataAccessException

OptimisticLockingFailureException

ObjectRetrievalFailureException

DataAccessException

UncategorizedDataAccessException

DataIntegrityViolationException

DeadlockLoserDataAccessException

ObjectOptimisticLockingFailureException

woensdag 6 oktober 2010

Page 44: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Reason #4Aspect-Oriented

Programming

woensdag 6 oktober 2010

Page 45: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

What Problem Does AOP Solve?

Aspect-Oriented Programming (AOP) enables modularization of cross-cutting

concerns

woensdag 6 oktober 2010

Page 46: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

What are Cross-Cutting Concerns?

Generic functionality that is needed in many places

in your application

• Logging and Tracing

• Transaction Management Security

• Caching

• Error Handling

• Performance Monitoring

• Custom Business Rules

woensdag 6 oktober 2010

Page 47: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

An Example Requirement

• Perform a role-based security check before every application method

woensdag 6 oktober 2010

Page 48: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

An Example Requirement

• Perform a role-based security check before every application method

A sign this requirement is a cross-cutting concern

woensdag 6 oktober 2010

Page 49: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

Without AOP

• Failing to modularize cross-cutting concerns leads to two things

• Code tangling

• A coupling of concerns

• Code scattering

• The same concern spread across modules

woensdag 6 oktober 2010

Page 50: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Tanglingpublic class RewardNetworkImpl implements RewardNetwork { public RewardConfirmation rewardAccountFor(Dining dining) { if (!hasPermission(SecurityContext.getPrincipal()) { throw new AccessDeniedException(); } Account a = accountRepository.findByCreditCard(… Restaurant r = restaurantRepository.findByMerchantNumber(… MonetaryAmount amt = r.calculateBenefitFor(account, dining); … }}

woensdag 6 oktober 2010

Page 51: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Tanglingpublic class RewardNetworkImpl implements RewardNetwork { public RewardConfirmation rewardAccountFor(Dining dining) { if (!hasPermission(SecurityContext.getPrincipal()) { throw new AccessDeniedException(); } Account a = accountRepository.findByCreditCard(… Restaurant r = restaurantRepository.findByMerchantNumber(… MonetaryAmount amt = r.calculateBenefitFor(account, dining); … }}

Mixing of concerns

woensdag 6 oktober 2010

Page 52: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Scatteringpublic class HibernateAccountManager implements AccountManager { public Account getAccountForEditing(Long id) { if (!hasPermission(SecurityContext.getPrincipal()) { throw new AccessDeniedException(); } …

public class HibernateMerchantReportingService implements MerchantReportingService { public List<DiningSummary> findDinings(String merchantNumber, DateInterval interval) { if (!hasPermission(SecurityContext.getPrincipal()) { throw new AccessDeniedException(); } …

woensdag 6 oktober 2010

Page 53: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Scatteringpublic class HibernateAccountManager implements AccountManager { public Account getAccountForEditing(Long id) { if (!hasPermission(SecurityContext.getPrincipal()) { throw new AccessDeniedException(); } …

public class HibernateMerchantReportingService implements MerchantReportingService { public List<DiningSummary> findDinings(String merchantNumber, DateInterval interval) { if (!hasPermission(SecurityContext.getPrincipal()) { throw new AccessDeniedException(); } …

Duplication

woensdag 6 oktober 2010

Page 54: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

39

BankService

Without AOP

CustomerService ReportingService

woensdag 6 oktober 2010

Page 55: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

39

BankService

Without AOPSecurity

CustomerService ReportingService

woensdag 6 oktober 2010

Page 56: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

39

BankService

Without AOP

TransactionsSecurity

CustomerService ReportingService

woensdag 6 oktober 2010

Page 57: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

39

BankService

Without AOP

TransactionsSecurity

Logging

CustomerService ReportingService

woensdag 6 oktober 2010

Page 58: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

39

BankService

Without AOP

CodescatteringTransactions

Security

Logging

CustomerService ReportingService

woensdag 6 oktober 2010

Page 59: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

39

BankService

Without AOP

Codescattering

Codetangling

TransactionsSecurity

Logging

CustomerService ReportingService

woensdag 6 oktober 2010

Page 60: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

How AOP Works

1.Implement your mainline application logic

2.Write aspects to implement your cross-cutting concerns

3.Weave the aspects into your application

woensdag 6 oktober 2010

Page 61: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

41

AOP based

BankService CustomerService ReportingService

woensdag 6 oktober 2010

Page 62: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

41

AOP based

SecurityAspect

BankService CustomerService ReportingService

woensdag 6 oktober 2010

Page 63: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

41

AOP based

TransactionAspect

SecurityAspect

BankService CustomerService ReportingService

woensdag 6 oktober 2010

Page 64: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

41

AOP based

TransactionAspect

SecurityAspect

LoggingAspect

BankService CustomerService ReportingService

woensdag 6 oktober 2010

Page 65: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

41

AOP based

TransactionAspect

SecurityAspect

LoggingAspect

BankService CustomerService ReportingService

woensdag 6 oktober 2010

Page 66: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

AOP Quick Start

•Consider this basic requirement

•How can you use AOP to meet it?

Log a message every time a property is about to change

woensdag 6 oktober 2010

Page 67: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Target Objectpublic class SimpleCache implements Cache { private int cacheSize; private DataSource dataSource;

public void setCacheSize(int size) { cacheSize = size; }

public void setDataSource(DataSource ds) { dataSource = ds; } …}

woensdag 6 oktober 2010

Page 68: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

The Aspect

woensdag 6 oktober 2010

Page 69: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

The Aspect@Aspect

woensdag 6 oktober 2010

Page 70: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

The Aspect@Aspectpublic class PropertyChangeTracker {

woensdag 6 oktober 2010

Page 71: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

The Aspect@Aspectpublic class PropertyChangeTracker { private Logger logger = Logger.getLogger(getClass());

woensdag 6 oktober 2010

Page 72: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

The Aspect@Aspectpublic class PropertyChangeTracker { private Logger logger = Logger.getLogger(getClass());

woensdag 6 oktober 2010

Page 73: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

The Aspect@Aspectpublic class PropertyChangeTracker { private Logger logger = Logger.getLogger(getClass());

@Before(“execution(void set*(*))”)

woensdag 6 oktober 2010

Page 74: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

The Aspect@Aspectpublic class PropertyChangeTracker { private Logger logger = Logger.getLogger(getClass());

@Before(“execution(void set*(*))”) public void trackChange() {

woensdag 6 oktober 2010

Page 75: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

The Aspect@Aspectpublic class PropertyChangeTracker { private Logger logger = Logger.getLogger(getClass());

@Before(“execution(void set*(*))”) public void trackChange() { logger.info(“Property about to change…”);

woensdag 6 oktober 2010

Page 76: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

The Aspect@Aspectpublic class PropertyChangeTracker { private Logger logger = Logger.getLogger(getClass());

@Before(“execution(void set*(*))”) public void trackChange() { logger.info(“Property about to change…”); }

woensdag 6 oktober 2010

Page 77: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

The Aspect@Aspectpublic class PropertyChangeTracker { private Logger logger = Logger.getLogger(getClass());

@Before(“execution(void set*(*))”) public void trackChange() { logger.info(“Property about to change…”); }}

woensdag 6 oktober 2010

Page 78: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

<beans> <aop:aspectj-autoproxy/>

<bean id=“propertyChangeTracker” class=“example.PropertyChangeTracker” />

<bean name=“cache-A” class=“example.SimpleCache” ../> <bean name=“cache-B” class=“example.SimpleCache” ../> <bean name=“cache-C” class=“example.SimpleCache” ../>

</beans>

Tell Spring about the Aspect

woensdag 6 oktober 2010

Page 79: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Run...

ApplicationContext context = new ClassPathXmlApplicationContext(“application-config.xml”);Cache cache = (Cache) context.getBean(“cache-A”);cache.setCacheSize(2500);

woensdag 6 oktober 2010

Page 80: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Run...

ApplicationContext context = new ClassPathXmlApplicationContext(“application-config.xml”);Cache cache = (Cache) context.getBean(“cache-A”);cache.setCacheSize(2500);

INFO: Property about to change…

woensdag 6 oktober 2010

Page 81: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Reason #5@Transactional

woensdag 6 oktober 2010

Page 82: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

Why use Transactions?• Atomic

–Each unit of work is an all-or-nothing operation

• Consistent

–Database integrity constraints are never violated

• Isolated

–Uncommitted changes are not visible to other transactions

• Durable

–Committed changes are permanent

woensdag 6 oktober 2010

Page 83: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

Local Transaction Management

•Transactions can be managed at the level of a local resource

–Such as the database

•Requires programmatic management of transactional behavior on the Connection

woensdag 6 oktober 2010

Page 84: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Examplepublic void updateBeneficiaries(Account account) {... try { conn = dataSource.getConnection(); conn.setAutoCommit(false); ps = conn.prepareStatement(sql); for (Beneficiary b : account.getBeneficiaries()) { ps.setBigDecimal(1, b.getSavings().asBigDecimal()); ps.setLong(2, account.getEntityId()); ps.setString(3, b.getName()); ps.executeUpdate(); } conn.commit(); } catch (Exception e) { conn.rollback(); throw new RuntimeException(“Error updating!”, e); }

woensdag 6 oktober 2010

Page 85: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

Problems with Local Transactions

•Connection management code is error-prone

•Transaction demarcation belongs at the service layer

–Multiple data access methods may be called within a transaction

–Connection must be managed at a higher level

woensdag 6 oktober 2010

Page 86: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Passing Connectionspublic RewardConfirmation rewardAccountFor(Dining dining) { Connection conn = DataSourceUtils.getConnection(); conn.setAutoCommit(false); try { ... accountRepository.updateBeneficiaries(account, conn); rc = rewardRepository.confirmReward(contrib, dining, conn); conn.commit(); } catch (Exception e) { conn.rollback(); throw new RuntimeException(“reward failed”, e); }}

woensdag 6 oktober 2010

Page 87: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

Programmatic JTA

• Application Servers enable use of the Java Transaction API (JTA)

• The UserTransaction object is bound to JNDI

• Transactions can be managed from the service layer

• May call multiple data access methods

woensdag 6 oktober 2010

Page 88: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

JTApublic RewardConfirmation rewardAccountFor(Dining dining) { Context ctx = new InitialContext(); UserTransaction transaction = (UserTransaction) ctx.lookup(“java:comp/UserTransaction”); transaction.begin(); try { ... accountRepository.updateBeneficiaries(account); confirmation = rewardRepository.confirmReward(contribution, dining); transaction.commit(); } catch (Exception e) { transaction.rollback(); throw new RuntimeException(“failed to reward”, e); }}

woensdag 6 oktober 2010

Page 89: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

Programmatic JTA Problems

• Depends on an Application Server environment

• Still requires code to manage transactions

• The code is error-prone

woensdag 6 oktober 2010

Page 90: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

@Transactional

public class RewardNetworkImpl implements RewardNetwork { @Transactional public RewardConfirmation rewardAccountFor(Dining d) { // atomic unit-of-work }}

woensdag 6 oktober 2010

Page 91: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

@Transactional• Works though AOP

• Consistent approach

• JDBC

• JPA

• Hibernate

• JMS

• JTA

• ...

woensdag 6 oktober 2010

Page 92: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Reason #6Scripting Languages

woensdag 6 oktober 2010

Page 93: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

Scripting Languages

• More and more popular

• Especially when running on the JVM

• Mix-and-match approach

• Front-end in JRuby

• Back-end in Java

woensdag 6 oktober 2010

Page 94: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

Dynamic Language Support in Spring

• Spring container supports

• Groovy

• JRuby

• BeanShell

woensdag 6 oktober 2010

Page 95: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

JRubypackage org.springframework.scripting;

public interface Messenger {

String getMessage();}

require 'java'

class RubyMessenger include org.springframework.scripting.Messenger

def setMessage(message) @@message = message end

def getMessage @@message endend

<lang:jruby id="messageService" script-interfaces="org.springframework.scripting.Messenger" script-source="classpath:RubyMessenger.rb"> <lang:property name="message" value="Hello World!" />

</lang:jruby>

woensdag 6 oktober 2010

Page 96: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Reason #7OSGi

woensdag 6 oktober 2010

Page 97: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

JAR Hell

YourApplication

Spring 2.5.4

Hibernate3.3.0

log4j1.2.13

log4j1.2.15

woensdag 6 oktober 2010

Page 98: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

JAR Hell

YourApplication

Spring 2.5.4

Hibernate3.3.0

log4j1.2.13

log4j1.2.15

woensdag 6 oktober 2010

Page 99: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

JAR Hell

YourApplication

Spring 2.5.4

Hibernate3.3.0

log4j1.2.13

log4j1.2.15

woensdag 6 oktober 2010

Page 100: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

OSGi

YourApplication

Spring 2.5.4

Hibernate3.3.0

log4j1.2.13

log4j1.2.15

woensdag 6 oktober 2010

Page 101: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

OSGi

YourApplication

Spring 2.5.4

Hibernate3.3.0

log4j1.2.13

log4j1.2.15

MANIFEST.MFImport-package:org.apache.log4j;version=1.2.13

woensdag 6 oktober 2010

Page 102: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

OSGi

YourApplication

Spring 2.5.4

Hibernate3.3.0

log4j1.2.13

log4j1.2.15

MANIFEST.MFImport-package:org.apache.log4j;version=1.2.13

MANIFEST.MFImport-package:org.apache.log4j;version=1.2.15

woensdag 6 oktober 2010

Page 103: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

OSGi

YourApplication

Spring 2.5.4

Hibernate3.3.0

log4j1.2.13

log4j1.2.15

woensdag 6 oktober 2010

Page 104: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

OSGi

YourApplication

Spring 2.5.4

Hibernate3.3.0

log4j1.2.13

log4j1.2.15

woensdag 6 oktober 2010

Page 105: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

65

OSGi Service contribution

Published services

Private implementationobjects

woensdag 6 oktober 2010

Page 106: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission66

OSGitm – Dynamic System for Java

• Partition an app into modules

• Module = Bundle = Jar = a set of classes

• Each module has its own:

• class space

• lifecycle

• Strict visibility rules

• Understands versioning

• Everything is Dynamic!

woensdag 6 oktober 2010

Page 107: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

OSGi

• Import specific versions of dependencies

• Export as services

woensdag 6 oktober 2010

Page 108: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

Spring Dynamic Modules

• Decorate objects as OSGi services

• Dependency injection for OSGi services

• Remove OSGi dependencies

• Easier to test

woensdag 6 oktober 2010

Page 109: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission69

Exporting OSGi Service

<bean id=“myPojo” class=“someObject”/>

<osgi:service

ref=“myPojo”

interface= “com.springsource.MyService"/>

woensdag 6 oktober 2010

Page 110: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission70

Importing OSGi service<osgi:reference

id=“osgiService"

interface=“com.springsource.DynamicService"/>

<bean id=“consumer” class..>

<property name=“service” ref=“osgiService”>

</bean>

woensdag 6 oktober 2010

Page 111: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

SpringSource Confidential. Do not distribute without express permission

But wait, there is more!

• There is more!

• JMS, Web, Security, Web Services, Batch, Integration, ...

• Check out www.springframework.org

woensdag 6 oktober 2010

Page 112: 7 (or so) Reasons to use Spring · • Grew from practical experience Simple things should be simple. Complex things should be possible. Alan Kay woensdag 6 oktober 2010. SpringSource

Q & A

woensdag 6 oktober 2010