1 / 23

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 ;). Review. Bean lifecycle

lamar
Download Presentation

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. Review • Bean lifecycle • XML Schema-based Configuration (namespace handlers) • Lifecycle hooks • Bean Initialization (JSR 250, @PostConstruct, …) • Bean post processors • Component scanning • Spring Component Annotations • DI Annotations (JSR 330, @Inject, @Named)

  4. Overview • Advanced Injection • Spring EL • Additional Injection Annotations • Providers • Application Context web integration • Testing framework

  5. Spring EL (SpEL) • Allows access to Spring beans and properties • Supports querying and manipulating object graph at runtime • Similar syntax to Unified EL (but more extensive) • Method invocation, string templating • Namespace handlers use it to inject references into attributes • For more specifics, please see the Spring docs: • http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/expressions.html

  6. Spring EL Examples • We recommend only using this when necessary • For example • When extracting a property from a map • Or injecting a reference into a namespace handler <property name="someProperty" value="#{systemProperties['someValue']}" /> <jee:jndi-lookup id="databasePassword" jndi-name="dbPassword" /> <jee:jndi-lookup id="databaseUsername" jndi-name="dbUsername"/> <data-source driver-class="org.h2.Driver" url="jdbc:h2:mem:stack-starter;MODE=Oracle" user="#{databaseUsername}" password="#{databasePassword}" override="true" db-env="EMBEDDED"/>

  7. Additional Injection Annotations • Many additional injection annotations • Please refer to the Spring documentation here: • http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-annotation-config

  8. Providers • Providers allow us to defer instantiation or accessing a resource until it is accessed • Providers facilitate (from the JavaDoc): • Retrieving multiple instances • Lazy or optimal retrieval of an instance • Breaking circular dependencies • Abstracting scope so you can look up an instance in a smaller scope from an instance in a containing scope

  9. Previous Training Lab • We used the Named annotation to select the prototypeRabbit to inject into the farm as the prize rabbit • The result was something like the following • Does anyone see any problem with this? @Component public class Farm { @Inject @Named("prototypeRabbit") private Provider<Rabbit> prizeRabbit; … }

  10. Provider Demo DEMO

  11. Lab 1: Providers https://tech.lds.org/wiki/Intermediate_Spring#Lab_1_Advanced_Injection

  12. Web Context Listener • Loading application context in a web environment

  13. Traditionally • Previously we have loaded application contexts with something like: • In a web environment however • You will want the context to automatically be loaded on startup • And be shared across the entire application ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); SomeBeansomeBean = context.getBean(SomeBean.class);

  14. Servlet Listeners • The Java Servlet spec provides a listener (startup hook) • Triggers these listeners to run on startup • Spring utilized this functionality and created a listener • Will load the application context on start up

  15. Context Loader Listener • Specifically the following configures this listener in web.xml: • And utilizes the following context parameter <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:META-INF/spring/applicationContext.xml,classpath:*beans.xml</param-value> <param-value>classpath:anotherContext.xml</param-value> </context-param>

  16. Application Contexts and Servlets • Servlets not instantiated by Spring • Instantiated by the servlet container • Spring unable to inject dependencies • However Spring provides a way to access the application context

  17. Application Context and Servlet • For the given servlet configuration (web.xml) • Application Context accessed as follows: <servlet> <servlet-name>servlet</servlet-name> <servlet-class>org.lds.training.TrainingServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>servlet</servlet-name> <url-pattern>/servlet</url-pattern> </servlet-mapping> public class TrainingServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Context loader listener stores context in the servlet context - which is why it is required ApplicationContextapplicationContext= WebApplicationContextUtils.getWebApplicationContext(getServletContext()); SomeBeansomeBean = (SomeBean) applicationContext.getBean(SomeBean.class); someBean.printSomething(); } }

  18. A Better Way • Spring provides a servlet that delegates to a bean that is Spring managed • Allowing annotations and injection • Called an HttpRequestHandler • Create a Spring bean that matches the name of the servlet name • This provides the mapping between the two

  19. Utilizing a Spring Request Handler • The configuration: <!– web.xml --> <servlet> <servlet-name>trainingHandler</servlet-name> <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>trainingHandler</servlet-name> <url-pattern>/trainingHandler</url-pattern> </servlet-mapping> <!– applicationContext.xml --> <bean id="trainingHandler" class="org.lds.training.TrainingRequestHandler" /> //Java Implementations public class TrainingRequestHandler implements HttpRequestHandler { @Inject private SomeBeansomeBean; public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ someBean.printSomething(); } }

  20. Spring MVC and Application Contexts • Spring MVC provides and even better way to integrate with the web container • Look forward to further discussion of this in a future training

  21. Lab 2: Web Context Listener https://tech.lds.org/wiki/Intermediate_Spring#Lab_2_Web_Context_Listener

  22. Template for code

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

More Related