Matt Wheeler
description
Transcript of Matt Wheeler
Intermediate SpringMatt Wheeler
Notes
• This is a training NOT a presentation• Please ask questions• Prerequisites
– Introduction to Java Stack– Basic Java and XML skills– Installed LdsTech IDE (or other equivalent – good luck
there ;)
Overview
• Bean lifecycle• Xml Configuration Extensions (namespace
handlers)• Lifecycle hooks
• JSR 250 • Bean post processors
• Spring Annotations• JSR 330 Annotations (@Inject, @Named)
Review
• Last time we went over– Bean definitions– Dependency Injection (DI) and Inversion of Control
(IoC)– Application context– Bean scopes
Review
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean class="org.lds.training.SomeBean" /></beans>
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");SomeBean someBean = context.getBean(SomeBean.class);someBean.callMethod();
• Bean definition (beans.xml)
• Application Context
Spring Bean Lifecycle
1. Create bean definitions (from xml or annotations, or, …)
2. Instantiate beans using the definitions3. Set bean dependencies (values and bean
references) on the newly instantiated beans4. Initialization5. Deliver bean to requester for use6. On container shutdown call destruction callback
method
Xml Configuration Extension
• Also called namespace handlers• Shorten bean definition configuration• Provide easily reusable definitions• Self documenting• More readable
Spring Xml Configuration Extensions
Schema Description / Documentation
util Create non-anonymous collection types that can be referenced by idhttp://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/xsd-config.html#xsd-config-body-schemas-util
jee Elements such as jndi support / ejb shortcutshttp://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/xsd-config.html#xsd-config-body-schemas-jee
lang Expose beans written in another language like JRuby or Groovyhttp://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/xsd-config.html#xsd-config-body-schemas-lang
jms Deal with configuring JMS-related beanshttp://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/xsd-config.html# xsd-config-body-schemas-jms
tx Transaction supporthttp://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/xsd-config.html# xsd-config-body-schemas-tx
Xml Configuration Extensions (cont.)
Schema Descirption / Documentation
aop Helpers for Spring’s aspect oriented programming mechanismshttp://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/xsd-config.html# xsd-config-body-schemas-aop
context Configuration relation to the application context plumbing http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/xsd-config.html# xsd-config-body-schemas-context
tools Configuration for adding tooling specific meta-datahttp://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/xsd-config.html# xsd-config-body-schemas-tool
security Provides elements for web security configurationhttp://static.springsource.org/spring-security/site/docs/3.0.x/reference/ns-config.html
mvc Provides interceptors, view-conroller, …..http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-config
Example
• You have options
• Or
<mvc:annotation-driven validator="validator" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="order" value="0"/> <property name="useDefaultSuffixPattern" value="false"></property></bean>
<bean class="org.springframework.web.servlet.handler.MappedInterceptor"> <constructor-arg value="null"></constructor-arg> <constructor-arg> <bean class="org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor"> <constructor-arg> <bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean"></bean> </constructor-arg> </bean> </constructor-arg></bean>
Wait that’s not all
And this<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="webBindingInitializer"> <bean id="webBindingInitializer" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> <property name="validator" ref="validator" /> <property name="conversionService"> <bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean" /> </property> </bean> </property> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="writeAcceptCharset" value="false" /> </bean> <bean class="org.springframework.http.converter.ResourceHttpMessageConverter"></bean> <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"></bean> <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter"></bean> <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"></bean> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean> </list> </property></bean>
Another Example• Let us utilize a namespace handler
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"><!-- definitions here --></beans>
<!-- creates a java.util.List instance with the supplied values --><util:list id="alphaGroups"> <value>abc</value> <value>def</value> <value>ghi</value> <value>jkl</value></util:list>
<bean id="alphaGroups" class="org.springframework.beans.factory.config.ListFactoryBean"> <property name="sourceList"> <list> <value>abc</value> <value>def</value> <value>ghi</value> <value>jkl</value> </list> </property></bean>
Xml Configuration Extension architecture
• Pieces of namespace handlers • Create an xml schema that describes allowable
elements• Write a namespace handler• Code a BeanDefinitionParser
– Parses the defined xml and adds any necessary beans into the configuration
For Example
• Bottom line– Namespace handlers are backed by code
• The code supplements bean configuration and is a lot more than meets the eye
Add a parser example here
DEMO
Lab 3: Bean Scopes
https://tech.lds.org/wiki/Introduction_to_Spring#Lab_3_Bean_Scopes
Credit where credit is due
• http://springsource.org• Spring Recipies 2nd Edition (Gary Mak, Josh Long
and Daniel Rubio)
Dedication
• Mike Youngstrom and Bruce Campbell for their undying love and support through this process– And for reviewing and offering suggestions
• And for Spencer Uresk and Mike Heath for the inspiration
• And a special thanks to the rabbit farm who made this all possible