Spring Framework
Spring Framework
1
Agenda – Spring Framework
2
9. Testing using Spring
Spring
6
Inversion of Control
}catch(Exception e){
}
}
// Spring sets the value thru runtime injection!
private setOpportunityDAO(OpportunityDAO oppDao){
this.oppDAO = oppDao;
} 11
Spring Usage Scenarios
Business layer
Integrates with EJBs
Provides integration with components using IoC.
Transaction (declarative and programmatic)
Persistence layer
DAO pattern implementation
Template support for Hibernate, iBatis DataMapper and JDBC
Transaction management, Exception translation, connection management.
General
Email, JNDI, WebServices
12
Spring Bean Definition
<bean id=“CampaignServiceTarget"
class="com.corp.CampaignServiceImpl">
<property name=“promotionService">
<ref bean=“promotionService" />
</property>
<property name=“count">2</property>
</bean>
ContextLoaderPlugin
Loads a Spring application context for the Struts ActionServlet.
Struts Actions are managed as Spring beans.
17
Configuring Context – Option 1
<plug-in
className="org.springframework.web.struts.Contex
tLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml"/>
</plug-in>
18
Configuring Context – Option 2
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<!-- listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener -->
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> 19
Locating a Bean with Struts
<bean id=“campaignService"
class=“com.corp.CampaignServiceImpl">
20
Spring with EJB
21
Spring and EJB
23
Spring EJB Declaration – ejb-jar.xml
<session id=“CampaignService">
<ejb-name>CampaignService</ejb-name>
<local-home>com.corp.ejb.CampaignServiceLocalHome</local-home>
<local>com.corp.ejb.CampaignServiceLocal</local>
<ejb-class>com.corp.ejb.CampaignServiceBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
<env-entry>
<env-entry-name>ejb/BeanFactoryPath</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>ejbContext.xml</env-entry-value>
</env-entry>
<ejb-local-ref id="EJBLocalRef_1136316508000">
<ejb-ref-name>ejb/CampaignService</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local-home>com.corp.ejb.services.CampaignServiceLocalHome</local-home>
<local>com.corp.ejb.services.CampaignServiceLocal</local>
<ejb-link>CampaignService</ejb-link>
</ejb-local-ref>
</session>
24
Spring EJB Declaration
<bean id="accountService"
class=“LocalStatelessSessionProxyFactoryBean">
<property name="jndiName">
<value>ejb/CampaignServiceHome</value>
</property>
<property name="businessInterface">
<value>com.corp.services.CampaignService</value>
</property>
</bean>
25
EJB Client with Spring Helper
EJB Local:
/**
* Local interface for Enterprise Bean: CampaignService.
*/
public interface CampaignServiceLocal extends
javax.ejb.EJBLocalObject, CampaignService {
}
26
EJB class with Spring Helper
/**
* Bean implementation class for Enterprise Bean:
CampaignService
*/
public class CampaignServiceBean extends
AbstractStatelessSessionBean
implements CampaignService {
/**
* POJO implementation class for Enterprise Bean:
CampaignServiceBean
*/
public class CampaignServiceImpl implements CampaignService {
<bean id=“campaignServiceTarget"
class="com.corp.services.CampaignServiceImpl">
<property name=“promoDAO">
<ref bean=“promoDAO" />
</property>
</bean>
28
Transaction Management With Spring
29
Transaction Management With Spring
Local Transactions:
<bean id=“localTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionMana
ger">
<property name="dataSource">
<ref local="springContainerDataSource" />
</property>
</bean>
<bean id="wsJtaTm"
class="org.springframework.transaction.jta.WebSphereTransactionManag
erFactoryBean"/>
<bean id=“distTransactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="wsJtaTm" />
<property name="userTransactionName"><null /></property>
30
</bean>
Enabling Transactions on Services
Transactions are typically enabled at the business tier level and
not in the DAO
Spring transactions are an alternative to EJB transactions; for
both declarative and programmatic
Have to be enabled after disabling EJB CMT. No point in having
both
31
Enabling Declarative Transactions on Services
<bean id=“campaignServiceTxn"
class="org.springframework.transaction.interceptor.TransactionProxy
FactoryBean">
<property name="transactionManager">
<ref bean="distTransactionManager" />
</property>
<property name="target">
<ref bean=“campaignServiceTarget" />
</property>
<property name="transactionAttributes">
<props>
<prop key="update*">
PROPAGATION_REQUIRED, -com.corp.common.exception.NWException
</prop>
<prop key="*">
PROPAGATION_REQUIRED,readOnly,-com.corp.common.exception.NWException
</prop>
</props>
</property>
32
</bean>
Dependency Injection
beanId:CampaignServiceTxn
-TransactionProxyFactoryBean
TransactionAttributes
beanId:CampaignServiceTarget beanId:TransactionManager
-CampaignServiceImpl - JTATransactionManager
beanId:promoDAO beanId:hibernateSessionFactory
- HibernatePromotionDAO MappingResources
- setHibernateSessionFactory() HibernateProperties
33
Persistence With Spring
34
Defining Datasource
<bean id="springContainerDataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>jdbc/CustomerDB</value>
</property>
</bean>
35
Defining Persistence and wiring
Datasource
<bean id=“hibernateSessionFactory" class=
“org.springframework.orm.hibernate.LocalSessionFactoryBean“>
<property name="dataSource">
<ref local="springContainerDataSource” />
</property>
<property name="mappingResources">
<list> <value>com/corp/orm/Customer.hbm.xml</value>
<value>com/corp/orm/Account.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
net.sf.hibernate.dialect.DB2Dialect
</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.autocommit">false</prop>
<prop key="cache.provider_class">
org.hibernate.cache.OSCacheProvider
</prop>
</props>
36
</property>
</bean>
Defining Persistence Beans
<bean id=“promoDao"
class="com.corp.dao.HibernatePromotionDao">
<property name="sessionFactory">
<ref local="hibernateSessionFactory" />
</property>
</bean>
37
DAO Support classes
Support for
JdbcDaoSupport
Provides callback methods for row iteration
HibernateDaoSupport
Initializes Hibernate session factory
Provides templates to invoke Hibernate API or the session
SqlMapDaoSupport
Provides template for iBatis SQLMaps
Support similar to Hibernate template
38
JDBC Code without a template
}
Spring JDBC DAO Template
return promo;
}
public void setAppDataSource (DataSource ds) {
this.appDataSource = ds;
} 40
Hibernate Code without a template
session.save(promo);
return promo;
}
});
}
...
}
42
Spring Hibernate DAO Template Example
}
Consistent Exception Handling
44
Spring AOP
beanId:methodAuthzAdvisor beanId:methodAuthzAdvice
- RegexpMethodPointcutAdvisor - AuthorizationAdvice
PointCut pattern(.*)
Before
CampaignServiceImpl
-createCampaign(String);
46
Spring AOP Advice for Component Authorization
if (!isValidAccess) {
//Unauthorized user access, do not process any
further.
throw new Exception();
}
48
}
..
Spring Interceptor Injection
beanId:methodAuthzAdvisor
- RegexpMethodPointcutAdvisor
PointCut pattern(.*)
beanId:CampaignServiceTxn beanId:methodAuthzAdvice
-TransactionProxyFactoryBean - AuthorizationAdvice
TransactionAttributes
beanId:CampaignServiceTarget beanId:TransactionManager
-CampaignServiceImpl - JTATransactionManager
beanId:promoDAO beanId:hibernateSessionFactory
- HibernatePromotionDAO MappingResources
- setHibernateSessionFactory() HibernateProperties
49
Incorporating Advice in Service
<bean id=“promotionService"
class="org.springframework.aop.framework.ProxyFactor
yBean">
<property name="proxyInterfaces">
<value>com.corp.CampaignService</value>
</property>
<property name="target">
<ref local=“CampaignServiceTxn"/>
</property>
<property name="interceptorNames">
<list>
<value>methodAuthzAdvisor</value>
</list>
</property>
</bean> 50
Spring and Testing
JavaMail helpers
JMS implementation
JNDI lookup
Integration with iText for PDF generation
Helper classes for Http Invocation and WebServices
.. and a whole lot of other abstractions
52
Resources/References
Spring – www.springframework.org
Inversion of Control -
http://www.martinfowler.com/articles/injection.html
Java Development With Spring Framework – Johnson,
Hoeller et al.
Spring in Action – Walls, Breidenbach
53