Introduction To Spring: Stuart Halloway and Justin Gehtland
Introduction To Spring: Stuart Halloway and Justin Gehtland
Sample code associated with this presentation is Copyright (c) 2005-6 Relevance, LLC (www.relevancellc.com), unless otherwise marked. Code
citations from other projects are subject to the license(s) appropriate to those projects. You are responsible for complying with licenses for code you
use.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.aop.*;
import java.lang.reflect.Method;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import util.TestBase;
import org.springframework.aop.framework.ProxyFactory;
import util.TestBase;
import static org.easymock.classextension.EasyMock.*;
Pro
minimize dependencies
minimize repetition
code easy to test
Con
powerful abstractions are a mess when misused
can be difficult to follow, even when used well
lots of XML (for now)
JDBC Support
Hibernate Support
iBatis Support
Connection Setup
Error Handling
Calling close
import java.sql.*;
Simplified Setup
Closes Resources Automatically
Convenience Methods For
translating exceptions
mapping results to objects
returning Java collections
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
import java.util.*;
<beans>
<!-- not for production use (use driver with pooling!) -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/hibernate_xt"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
</beans>
package intro.jdbc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jdbc.core.*;
import javax.sql.DataSource;
import java.sql.*;
import java.util.*;
Configuration
Calling close
Simplified Setup
Closes Resources Automatically
Convenience Methods
Does This Sound Familiar?
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.context.ApplicationContext;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import java.util.*;
<beans>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>intro/hibernate/event.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/hibernate_xt"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
</beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.hibernate.SessionFactory;
import java.util.*;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import java.util.List;
<beans>
<bean id="dao" class="intro.hibernate.HibernateDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>intro/hibernate/event.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/hibernate_xt"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
</beans>
Security Framework
Pluggable Every Possible Way
Good, Simple Defaults For Web Apps
Highly Recommended
Resources
Codecite (Presentations and Code Samples), http://www.codecite.com
Relevance Consulting and Development, http://www.relevancellc.com/main/services
Relevance Training, http://www.relevancellc.com/main/training
Relevance Weblog, http://blogs.relevancellc.com
Projects Cited
Spring Exploration Application, http://www.codecite.com/projects/download/spring_xt