Spring 2 for Enterprise applications Chapter 1 Introduction Chapter 2 Core container Use of abstract factory methods and abstract factory objects: Example of a ReaderFactory: BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File( someFile.txt"))); public class ReaderFactory { public static Reader createReader(ReaderType readerType, File file) throw IOException { if (readerType.equals(ReaderType.FILE) { return new FileReader(file); } else if (readerType.equals(ReaderType.BUFFERED) { return new BufferedReader(new InputStreamReader(new FileInputStream(new File(file)))); } else {throw new IOException("No ReaderType."); } } private enum ReaderType{FILE, BUFFERED}; } BeanFactory interface: package org.springframework.beans.factory; import org.springframework.beans.BeansException; public interface BeanFactory { String FACTORY_BEAN_SUFFIX = "B"; Object getBean(String beanName) throws BeanException; Object getBean(String beanName, Class requiredType) throws BeanException; boolean containsBean(String beanName); boolean isSingleton(String beanName) throws NoSuchBeanDefinitionException; Class getType(String beanName) throws NoSuchBeanDefinitionException; String[] getAliases(String beanName) throws NoSuchBeanDefinitionException; } Chapter 3 AOP Chapter 4 Spring AOP Chapter 5 Data access using Spring Working with database transactions Appropriate connection life Abstract transaction management API Transaction demarcation The org.springframework.transaction.PlatformTransactionManager is an abstract class used for handling transactions. A concrete implementation is org.springframework.jdbc.datasource.DataSourceTransactionManager. This class obtains DataSource Connection objects. Spring offers various techniques for handing the transactions: programmatic Java 5 annotation @Transactional transaction advice transaction aspect using AspectJ a factory class to generate proxy instances DataSource interface and Connection pools Simple implementations java.sql.DriverManager org.springframework.jdbc.datasource.DriverManagerDataSource These return a Connection object from the internal pool when the getConnection() method is called. Datasources that support distributed transactions must be acquired through JNDI. Setting up a connection pool Use the Apache Commons data connection pooling jar: Or jdbc.properties file jdbc.driverClassName=com.mysql.jdbc.MySqLDriver jdbc.URL=jdbc://mysq1:3360/someDatabase jdbc.username=wendell jdbc.password=modica1970 JdbcTemplate class methods: execute() query() queryForInt() queryForLong() queryForMap() queryForList() queryForObject() queryForRowSet() public int getTotal() { String query = "select count(0) from people"; return new JdbcTemplate(dataSource).queryForInt(query); } Advantages: 1. no resource management. The connection to the data source is managed automatically and closed 2. automatic participation in transactions 3. no need for exception handling JdbcDaoSupport class: public class PeopleDaoImplementation extends JdbcDaoSupport implements PeopleDao { public int getTotal() { String query = "select count(0) from people"; return getJdbcTemplate().queryForInt(query); } For updates to the database: getJdbcTemplate().update (update, \ new Object[] { person.getFirstName(); person.getLastName(); ... } ); } Aggregate functions AVG(column) COUNT(0) COUNT(column) MAX(column) MIN(column) SUM(column) Callbacks CallableStatementCreator CallableStatementCallback ConnectCallback PreparedStatementCallback PreparedStatementCreator PreparedStatementSetter RowMapper RowCallbackHandler StatementCallback public Person load(int id) { return (Person)getJdbcTemplate.queryForObject( query, new Object[] {id}, new RowMapper() { public Object mapRow(ResultSet set, int row) throws SQLException { Person person = new Person; person.setId(set.getInt("id")); ... return person; } } ); }