spring - object computingjava.ociweb.com/javasig/knowledgebase/2004-11/springtalk111204.pdfspring...
TRANSCRIPT
![Page 1: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/1.jpg)
Spring
Paul JensenPrincipal, Object Computing Inc.
![Page 2: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/2.jpg)
Spring Overview� “Lightweight Container”
� Very loosely coupled� Components widely reusable and separately
packaged
� Created by Rod Johnson� Based on “Expert one-on-one J2EE Design and
Development”� Currently on version 1.1.1
![Page 3: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/3.jpg)
Why Use Spring?� Wiring of components (Dependency Injection)
� Promotes/simplifies decoupling, design to interfaces, TDD
� Declarative programming without J2EE� Easily configured aspects, esp. transaction support
� Simplify use of popular technologies� Abstractions insulate application from specifics, eliminate
redundant code, and handle common error conditions� Underlying technology specifics still accessible (closures)
![Page 4: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/4.jpg)
Why Use Spring?� Conversion of checked exceptions to unchecked
� (Or is this a reason not to use it?)
� Not an all-or-nothing solution� Extremely modular and flexible
� Well designed� Easy to extend� Many reusable classes
![Page 5: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/5.jpg)
Spring Framework
![Page 6: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/6.jpg)
Spring Application
![Page 7: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/7.jpg)
Spring Dependency Injection� Inversion of Control (IoC)� “Hollywood Principle”
� Don't call me, I'll call you
� “Container” resolves (injects) dependencies of components by setting implementation object (push)
� As opposed to component instantiating or Service Locator pattern where component locates implementation (pull)
� Martin Fowler calls Dependency Injection
![Page 8: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/8.jpg)
Dependency Injection Variants� Variations on dependency injection
� Interface based (Avalon)� Constructor-based (PicoContainer, Spring)� Setter-based (Spring)
� BeanFactory provides configuration framework to initialize and “wire” JavaBeans� org.springframework.beans and
org.springframework.context
� Typically use the XmlBeanFactory, employing XML configuration files
![Page 9: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/9.jpg)
Dependency Injection (cont'd)� BeanFactory configured components need have
no Spring dependencies� Simple JavaBeans
� Beans are singletons by default� Properties may be simple values or references to
other beans� Built-in support for defining Lists, Maps, Sets,
and Properties collection types. � Custom PropertyEditors may be defined to
convert string values to other, arbitrary types.
![Page 10: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/10.jpg)
XmlBeanFactory Example� Property and constructor based IoC
<bean id="exampleBean" class="examples.ExampleBean"><property name="beanOne"><ref bean="anotherExampleBean"/></property><property name="beanTwo"><ref bean="yetAnotherBean"/></property><property name="integerProperty">1</property>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/><bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
<bean id="exampleBean" class="examples.ExampleBean"><constructor-arg><ref bean="anotherExampleBean"/></constructor-arg><constructor-arg><ref bean="yetAnotherBean"/></constructor-arg><constructor-arg><value>1</value></constructor-arg>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/><bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
![Page 11: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/11.jpg)
Bean Creation� Direct instantiation
� <bean id=“beanId” class=“className”>
� BeanFactory instantiation� Same syntax but class is subclass of BeanFactory� getObject() called to obtain Bean
� Static Factory� <bean id=“beanId” class=“className" factory-method="
staticCreationMethod“>
� Instance Factory Method� <bean id=“beanId” factory-bean=“existingBeanId" factory-
method=“nonStaticCreationMethod">
![Page 12: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/12.jpg)
Bean Creation� Beans may be singletons or “prototypes”
� Attribute singleton=“false” causes instantiation with each getBean() lookup
� Singleton is default� XmlBeanFactory pre-instantiates singletons
� May be overridden on per-instance basis by lazy-init=“true”
� Beans may also be marked abstract, allowing reuse of attribute values through inheritance
![Page 13: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/13.jpg)
Autowiring Properties� Beans may be auto-wired (rather than using <ref>)
� Per-bean attribute autowire� Explicit settings override
� autowire=“name”� Bean identifier matches property name
� autowire=“type”� Type matches other defined bean
� autowire=”constructor”� Match constructor argument types
� autowire=”autodetect”� Attempt by constructor, otherwise “type”
![Page 14: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/14.jpg)
Dependency Checking� Ensures properties are defined
� Per-bean attribute dependency-check� None required by default� Verifies autowiring succeeded
� “simple”� all but collaborators
� “object”� collaborators only
� “all”� Collaborators, primitive types, and collections
![Page 15: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/15.jpg)
Lifecycle Customization� Can define init method called after properties set
� init-method=”<method-name>”
� Can define destroy method as shutdown hook� destroy-method=”<method-name>”
� May alternatively implement InitializingBeanand/or DisposableBean� At cost of Spring dependency
![Page 16: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/16.jpg)
BeanFactory Miscellany� BeanFactoryAware interface provides BeanFactory for
bean� setBeanFactory(BeanFactory)
� BeanNameAware interface provides bean name� setBeanName(String)
� FactoryBean for beans which are themselves factories� Object getObject()� Boolean isSingleton()� Class getObjectType()
![Page 17: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/17.jpg)
BeanFactory Usage
InputStream is = new FileInputStream("beans.xml");XmlBeanFactory factory = new XmlBeanFactory(is);MyBeanClass bean = (MyBeanClass)factory.getBean(“myBean”);
ApplicationContext ctx = newClassPathXmlApplicationContext("beans.xml");MyBeanClass bean = (MyBeanClass)ctx.getBean(“myBean”);
OR
![Page 18: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/18.jpg)
ApplicationContext� Extends functionality of BeanFactory� Pre-instantiates singleton beans� Detects and registers BeanPostProcessors and
BeanFactoryPostProcessors� Supports nesting of contexts� ApplicationListener and ApplicationEvents
� Initialized and closed predefined� Custom may be created
� MessageSource provides i18n messaging� <bean id=”messageSource”
class=”...ResourceBundleMessageSource”/>� Contains list of bundle base names
![Page 19: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/19.jpg)
Web Initialization� Web applications may use
ContextLoaderListener to initialize Spring
<context-param> <param-name>contextConfigLocation</param-name><param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
web.xml
Automatically done by Spring DispatcherServlet
![Page 20: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/20.jpg)
Specialized Beans� MethodInvokingFactoryBean
� Invokes method on registered beans or any static methods
� Stores return value
� SingletonBeanFactoryLocator andContextSingletonBeanFactoryLocator� Useful for sharing BeanFactories� Eliminate duplication of beans in multiple similar
factories or contexts
![Page 21: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/21.jpg)
ApplicationContext customization� Defined beans inheriting from
BeanFactoryPostProcessor are detected and invoked� CustomEditorConfigurer
� Registers custom PropertyEditors for converting configuration string values to specific types
� AutoProxyCreators� Wrap beans in proxies based on various criteria (name,
metadata, etc)
� PropertyResourceConfigurer� Sets from property file and/or system properties
![Page 22: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/22.jpg)
ApplicationContext Example<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location"><value>database.properties</value></property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName">
<value>${database.connection.driver_class}</value></property><property name="url">
<value>${database.connection.url}</value></property>
</bean>
![Page 23: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/23.jpg)
Spring AOP
![Page 24: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/24.jpg)
AOP Fundamentals� Aspect-oriented programming (AOP) provides for
simplified application of cross-cutting concerns� Transaction management� Security� Logging� Auditing� Locking
� AOP sometimes (partially) achieved via Decorators or Proxies� CORBA Portable Interceptors� Servlet Filters
![Page 25: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/25.jpg)
AOP Fundamentals� Aspect - Implementation of a cross-cutting concern.
� Spring Advisors or Interceptors� Joinpoint - Execution point to target
� Typically, methods� Advice - Action taken at a particular joinpoint.� Pointcut - A set of joinpoints specifying where advice
should be applied (e.g. Regular expression)� Introduction/Mixin - Adding methods or fields to an
advised class.� Weaving - Assembling aspects into advised objects.
![Page 26: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/26.jpg)
Spring AOP� Generally, applies aspects to beans using BeanFactory
� Uses Dynamic Proxies if interface available otherwise CGLIB
� CGLIB creates derived class which proxies requests� Bean class may not be final
� Less capable than AspectJ� does not have field interception� only runtime weaving solution is available� Closer integration with AspectJ anticipated
![Page 27: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/27.jpg)
Spring Pointcuts� Pointcut applicability to a class may be evaluated
statically or dynamically� Spring only creates proxies where necessary
public interface Pointcut {ClassFilter getClassFilter();MethodMatcher getMethodMatcher();
}
public interface ClassFilter {boolean matches(Class clazz);
}
![Page 28: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/28.jpg)
Pointcuts (cont'd)
public interface MethodMatcher {boolean matches(Method m, Class targetClass);boolean isRuntime();boolean matches(Method m, Class targetClass, Object[] args);
}
� Pointcut may be statically or dynamically evaluated based on isRuntime()
� Abstract class StaticMethodMatcherPointcutrequires override of 1st method only
Only called if isRuntime() == true
![Page 29: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/29.jpg)
Pointcuts (cont'd)� Spring predefined pointcuts
� In org.springframework.aop.support package� RegexpMethodPointcut
� Union of multiple regular expressions� Uses Jakarta ORO package
� ControlFlowPointcut� Similar to AspectJ cflow� Applied if call stack includes specific class and, optionally,
method� UnionPointcut
� Merges pointcuts
![Page 30: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/30.jpg)
Spring Advice� Can have per-class or per-instance Advice� Spring provides several Advice types� Around Advice
� AOP Alliance compliant� Must call invocation.proceed() to call target
public class MyAdvice implements AroundAdvice {Object invoke(MethodInvocation invocation) {
// change arguments, start transaction, lock, etc.invocation.proceed();// change return value, stop transaction, unlock,etc.
}}
![Page 31: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/31.jpg)
Spring Advice� MethodBeforeAdvice
� void before(Method m, Object[] args, Object target)� Cannot alter return type
� ThrowsAdvice� Marker interface� Implementors define methods of form:
� afterThrowing([Method], [args], [target], subclassOfThrowable)
� AfterReturningAdvice� void afterReturning(Object returnValue, Method, m,
Object[] args, Object target)� Cannot modify return value
![Page 32: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/32.jpg)
Spring Advice� IntroductionInterceptor provides ability to define
mixinspublic class RollbackAdvice extends DelegatingIntroductionInterceptor
implements RollbackSupport {Map map = new HashMap();
void rollback(Date date) {// rollback to state at given time
}
public Object invoke(MethodInvocation invocation) {// record change and time of change
}}
![Page 33: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/33.jpg)
Injecting Advice<bean id=“meetingTarget" class=“ex.DefaultMeeting“
singleton=“false”><property name=“topic">Spring</property>
</bean>
<bean id="myAdvisor" class=“ex.RollbackAdvice"singleton=”false”>
</bean>
<bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugInterceptor">
</bean>
![Page 34: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/34.jpg)
Injecting Advice (cont'd)<bean id=“meeting"
class="org.springframework.aop.framework.ProxyFactoryBean"><property name="proxyInterfaces">
<value>ex.Meeting</value></property>
<property name="target"><ref local=“meetingTarget"/></property>
<property name="interceptorNames"><list>
<value>myAdvisor</value><value>debugInterceptor</value>
</list></property>
</bean>
Advisors applied in order
All methodsusing CGLib if none defined
![Page 35: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/35.jpg)
Autoproxying� Autoproxy bean definitions automatically proxy
selected beans.� BeanNameAutoProxyCreator
� Adds listed advisors/interceptors to beans with names matching regular expression
� DefaultAdvisorAutoProxyCreator� Generic autoproxy infrastructure support� Applies all advisors defined in the context to
all beans, proxying appropriately
![Page 36: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/36.jpg)
Metadata support� Spring supports obtaining meta data Object
attributes at class, method, and field level� Not yet argument level (as JSR-175)
� Currently supports Jakarta Commons Attributes� Support for JSR-175 in work� Metadata support provided via Attributes
interface� Amenable to mocking unlike JDK reflection and
Commons static methods
![Page 37: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/37.jpg)
Metadata autoproxying� Configuration of autoproxying based on metadata
attributes simplifies configuration� Define custom attribute class� Define Advisor with pointcut based on custom attribute� Add Advisor in ApplicationContext with autoproxy
� Examples� Transaction Attributes� Security Attributes� Pooling� Mapping of controllers to URLs
![Page 38: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/38.jpg)
Transactions
![Page 39: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/39.jpg)
AOP Transactions� Spring provides AOP support for declarative
transactions� Delegates to a PlatformTransactionManager
instance� DataSourceTransactionManager� HibernateTransactionManager� JdoTransactionManager� JtaTransactionManager
![Page 40: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/40.jpg)
Transaction Configuration<bean id="sessionFactory"
class="org.springframework.orm.hibernate.LocalSessionFactoryBean"><property name="dataSource"><ref bean="dataSource"/></property><property name="mappingResources">
<list><value>com/../model/*.hbm.xml</value>
</list></property>
</bean>
<bean id="transactionManager”class="org.springframework.orm.hibernate.HibernateTransactionManager"><property name="sessionFactory"><ref bean="sessionFactory"/>
</property></bean>
![Page 41: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/41.jpg)
Declarative Transactions� Declarative transactional support can be added to
any bean by using TransactionProxyFactoryBean� Similar to EJB, transaction attributes may be
defined on a per-method basis� Also allows definition of pre- and post-
interceptors (e.g. for security)
![Page 42: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/42.jpg)
Injecting Transaction Support
<bean id=“reservationService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager">
<ref bean="transactionManager"/></property> <property name="target"><ref local=“reservationServiceTarget"/></property><property name="transactionAttributes"> <props><prop key=“reserveRoom*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props> </property>
</bean>
Declarative transaction support for single bean
![Page 43: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/43.jpg)
Transaction Autoproxy<bean id="autoproxy"
class="org...DefaultAdvisorAutoProxyCreator"></bean>
<bean id="transactionAdvisor"class="org...TransactionAttributeSourceAdvisor"autowire="constructor" >
</bean>
<bean id="transactionInterceptor"class="org...TransactionInterceptor"autowire="byType">
</bean>
<bean id="transactionAttributeSource"class="org...AttributesTransactionAttributeSource"autowire="constructor">
</bean>
<bean id="attributes"class="org...CommonsAttributes"
/>
Caches metadatafrom classes
Generic autoproxysupport
Applies transactionusing transactionManager
Invokes interceptorbased on attributes
![Page 44: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/44.jpg)
Data Access
![Page 45: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/45.jpg)
Data Access� DAO support provides pluggable framework for
persistence� Currently supports JDBC, Hibernate, JDO, and
iBatis� Defines consistent exception hierarchy (based on
RuntimeException)� Provides abstract “Support” classes for each
technology� Template methods define specific queries
![Page 46: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/46.jpg)
Hibernate DAO Examplepublic class ReservationDaoImpl extends HibernateDaoSupport
implements ReservationDao {public Reservation getReservation (Long orderId) {return (Reservation)getHibernateTemplate().load(Reservation .class,
orderId);}
public void saveReservation (Reservation r) {getHibernateTemplate().saveOrUpdate(r);
}
public void remove(Reservation Reservation) {getHibernateTemplate().delete(r);
}
![Page 47: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/47.jpg)
Hibernate DAO (cont’d)
public Reservation[] findReservations(Room room) {List list = getHibernateTemplate().find(
"from Reservation reservation “ +“ where reservation.resource =? “ +“ order by reservation.start",instrument);
return (Reservation[]) list.toArray(new Reservation[list.size()]);
![Page 48: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/48.jpg)
Hibernate DAO (cont’d)public Reservation[] findReservations(final DateRange range) {
final HibernateTemplate template = getHibernateTemplate();List list = (List) template.execute(new HibernateCallback() {
public Object doInHibernate(Session session) {Query query = session.createQuery(
"from Reservation r “ +“ where r.start > :rangeStart and r.start < :rangeEnd “);
query.setDate("rangeStart", range.getStartDate()query.setDate("rangeEnd", range.getEndDate())return query.list();
}});return (Reservation[]) list.toArray(new Reservation[list.size()]);
}}
![Page 49: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/49.jpg)
Hibernate Example<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource"><ref bean="dataSource"/></property><property name="mappingResources">
<list><value>com/jensenp/Reservation/Room.hbm.xml</value><value>com/jensenp/Reservation/Reservation.hbm.xml</value><value>com/jensenp/Reservation/Resource.hbm.xml</value>
</list></property> <property name="hibernateProperties">
<props><prop key="hibernate.dialect">${hibernate.dialect}</prop><prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop><prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props></property>
</bean>
<bean id=“reservationDao"class="com.jensenp.Reservation.ReservationDaoImpl">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
</bean>
![Page 50: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/50.jpg)
JDBC Support� JDBCTemplate provides
� Translation of SQLExceptions to more meaningful Spring Runtime exceptions
� Integrates thread-specific transactions
� MappingSQLQuery simplifies mapping of ResultSets to Java objects
![Page 51: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/51.jpg)
Web Framework
![Page 52: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/52.jpg)
DispatcherServlet� The DispatcherServlet is the Spring Front
Controller� Initializes WebApplicationContext� Uses /WEB-INF/[servlet-name]-servlet.xml by
default� WebApplicationContext is bound into
ServletContext
![Page 53: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/53.jpg)
DispatcherServlet Configuration� HandlerMapping
� Routing of requests to handlers
� HandlerAdapter� Adapts to handler interface. Default utilizes Controllers
� HandlerExceptionResolver� Maps exceptions to error pages� Similar to standard Servlet, but more flexible
� ViewResolver� Maps symbolic name to view
![Page 54: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/54.jpg)
Dispatcher Servlet Configuration� MultipartResolver
� Handling of file upload
� LocaleResolver� Default uses HTTP accept header, cookie, or
session
![Page 55: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/55.jpg)
Controllers� Controller interface defines one method
� ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception
� ModelAndView consists of a view identifier and a Map of model data
![Page 56: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/56.jpg)
Controller Implementations� CommandControllers bind parameters to data
objects� AbstractCommandController� AbstractFormController� SimpleFormController� WizardFormController
![Page 57: Spring - Object Computingjava.ociweb.com/javasig/knowledgebase/2004-11/SpringTalk111204.pdfSpring Advisors or Interceptors Joinpoint - Execution point to target Typically, methods](https://reader036.vdocuments.us/reader036/viewer/2022062920/5f02cc227e708231d4060f30/html5/thumbnails/57.jpg)
References� Spring’s homepage: http://www.springframework.org� “Introducing the Spring Framework” by Rod Johnson:
http://theserverside.com/news/thread.jsp?thread_id=21893� “Inversion of control containers and dependency injection” by
Martin Fowler: http://www.martinfowler.com/articles/injection.html
� AOP Alliance: http://aopalliance.sourceforge.net