180 likes | 272 Views
Intermediate Spring. Matt 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
E N D
Intermediate Spring Matt 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 • Bean definition (beans.xml) <?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> • Application Context ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); SomeBeansomeBean = context.getBean(SomeBean.class); someBean.callMethod();
Spring Bean Lifecycle 1. Create bean definitions (from xml or annotations, or, …) 2. Instantiate beans using the definitions 3. Set bean dependencies (values and bean references) on the newly instantiated beans 4. Initialization 5. Deliver bean to requester for use 6. 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
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 Add a parser example here • Bottom line • Namespace handlers are backed by code • The code supplements bean configuration and is a lot more than meets the eye
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