1 / 18

Matt Wheeler

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

deiter
Télécharger la présentation

Matt Wheeler

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Intermediate Spring Matt Wheeler

  2. 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 ;)

  3. Overview • Bean lifecycle • Xml Configuration Extensions (namespace handlers) • Lifecycle hooks • JSR 250 • Bean post processors • Spring Annotations • JSR 330 Annotations (@Inject, @Named)

  4. Review • Last time we went over • Bean definitions • Dependency Injection (DI) and Inversion of Control (IoC) • Application context • Bean scopes

  5. 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();

  6. 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

  7. Xml Configuration Extension • Also called namespace handlers • Shorten bean definition configuration • Provide easily reusable definitions • Self documenting • More readable

  8. Spring Xml Configuration Extensions

  9. Xml Configuration Extensions (cont.)

  10. 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>

  11. 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>

  12. 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>

  13. 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

  14. 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

  15. DEMO

  16. Lab 3: Bean Scopes https://tech.lds.org/wiki/Introduction_to_Spring#Lab_3_Bean_Scopes

  17. Credit where credit is due • http://springsource.org • Spring Recipies 2nd Edition (Gary Mak, Josh Long and Daniel Rubio)

  18. 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

More Related