Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Spring Continued Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack.

Similar presentations


Presentation on theme: "Introduction to Spring Continued Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack."— Presentation transcript:

1 Introduction to Spring Continued 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

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

5 Review ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); SomeBean someBean = context.getBean(SomeBean.class); someBean.callMethod(); Bean definition (beans.xml) Application Context

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 SchemaDescription / Documentation utilCreate non-anonymous collection types that can be referenced by id reference/html/xsd-config.html#xsd-config-body-schemas-util jeeElements such as jndi support / ejb shortcuts reference/html/xsd-config.html#xsd-config-body-schemas-jee langExpose beans written in another language like JRuby or Groovy reference/html/xsd-config.html#xsd-config-body-schemas-lang jmsDeal with configuring JMS-related beans reference/html/xsd-config.html# xsd-config-body-schemas-jms txTransaction support reference/html/xsd-config.html# xsd-config-body-schemas-tx

9 Xml Configuration Extensions (cont.) SchemaDescirption / Documentation aopHelpers for Spring’s aspect oriented programming mechanisms reference/html/xsd-config.html# xsd-config-body-schemas-aop contextConfiguration relation to the application context plumbing reference/html/xsd-config.html# xsd-config-body-schemas-context toolsConfiguration for adding tooling specific meta-data reference/html/xsd-config.html# xsd-config-body-schemas-tool securityProvides elements for web security configuration config.html mvcProvides interceptors, view-conroller, ….. reference/html/mvc.html#mvc-config

10 Example You have options Or its relatively equivalent brother

11 Wait that’s not all And this

12 Another Example Let us utilize a namespace handler

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 Bean Definition Parsers Bottom line – Namespace handlers are backed by code The code supplements bean configuration and is a lot more than meets the eye Let’s look at one

15 DEMO

16 Lab 1: Xml Configuration Extensions https://tech.lds.org/wiki/Introduction_to_Spring_C ontinued#Lab_1_Xml_Configuration_Extensions

17 JSR 250 (Common) Annotations JSR 250 provides many annotations for common use cases in Java

18 Hooking into the Lifecycle Define init-method in bean definition The associated bean The init method is called after the bean had been initialized an all properties set public class SomeBean { public void init() { //some initialization code }

19 Spring Bean Lifecycle Review 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

20 Jar Dependency JSR 250 Annotation Dependency (only required in Java 1.5) javax.annotation jsr250-api 1.0

21 Annotate the Class JSR 250 annotations annotation for bean initialization There is likewise counterpart – Called just before the bean is destroyed – Allows for cleanup of resources public class SomeBean public void init() { // do some initialization work }

22 Configure Annotation Handling Specify annotation handlers (bean post processors)

24 Spring Annotations We have seen how to use annotations to call an init method during initialization Wouldn’t it be nice if didn’t need to define even the beans themselves in xml at all? – We will need something to scan the classes for annotations and register bean definitions

25 Welcome component-scan component-scan element in context schema – Scans classpath searching for matching beans Registers bean definitions matching classes – Can specify an include filter and/or exclude filter You can also assign a filter type for a targeted search – framework-reference/html/beans.html#beans-scanning-filters – annotation – assignable – aspectj – regex – cutsom

26 Bean Lifecycle and Component Scan 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

27 For Example This configuration will (for the given packages): – Register bean definitions for all classes with “abc” in their names – Not register beans for any classes that extend / implement Animal

28 Naming What id will be given for beans that are registered By default it is the class name with the first letter lower cased – For example, the following will result in a bean definition with public class Rabbit { }

29 Naming What id will be given for beans that are registered? By default it is the class name with the first letter lower cased – For example, a class named Rabbit would result in a bean definition with id=“rabbit”

30 Annotation Scanning What do we do if: – The default naming is not acceptable – Difficult to come up with a pattern that matches only the beans that we want registered with Spring Annotation scanning

31 Spring Annotations Spring provides stereotype annotations to help identify a bean’s role the application architecture – denotes application services – denotes a view layer components – the most general stereotype annotation – denotes any class to be managed by Spring – most often used to demarcate DAOs – You can also create your own custom stereotype annotations

32 For Example In a given application context you may want to have the scanner – Register beans annotated with your custom annotation – But not register definitions for beans annotated

33 Naming So how does annotation scanning help naming? – The following will still register a bean with id=“rabbit” – But, this will register a bean with public class Rabbit { public class Rabbit { }

34 The Main Point All this to tell you that now you can create a bean automatically without defining it in xml That is to say, the following are basically equivalent in public class SomeBean { }

35 Scope But what about scope – i.e. what is the equivalent annotation for specifying a scope of prototype

36 @Scope Be sure to use org.springframework.context.annotation.Scope – Not javax.inject.Scope Possible values:

37 Putting it all together Xml definition Equivalent public class Turkey { }

38 Lab 3: Spring Annotations https://tech.lds.org/wiki/Introduction_to_Spring_C ontinued#Lab_3_Spring_Annotations

39 JSR 330 Annotations (DI) Now that we can create bean definitions from annotations we probably would like to be able to inject one bean into another

40 Dependency JSR 330 annotations require you to include the following dependency: – Don’t be alarmed by the unorthodox version value It is correct as of this writing javax.inject 1

41 Dependency Injection Annotations To inject beans we have the following new annotations – inject bean references by type – modify injection by providing a name is similar to specifying the following in a bean definition file

42 can be used almost anywhere //on a member private Rabbit rabbit; //on a public Farm(Rabbit prizeRabbit) {…} //on a setter public void setPrizeRabbit(Rabbit rabbit) { this.rabbit = rabbit; } //if you can’t inject all of them by type you could to narrow to a single public void anyMethod(Chicken Rabbit rabbit, Duck duck) { … } //on collections (will inject all beans in the application context of the specified private Rabbit[] private List rabbits; //will contain all beans with the given type and the bean name as the private Map rabbits;

43 @Inject (cont) By default injection injects by type – Finds any declared instances of the type for the annotated entity What if you have two targets of the same type? – You can specify by name – Downside is that this is no longer type safe Only referenced by a String Could employ a Qualifier to remain type safe private Rabbit prizeRabbit;

44 Putting it all together Xml definition Equivalent annotation public class Turkey { … billysFarm ") public class Farm { private Turkey public Farm(Turkey turkey) { this.turkey = turkey; }

45 DEMO ??? Assume the following configuration Now say I wanted to get rid of the chicken declaration Remove the chicken xml declaration Remove the farm xml declaration Assume the following configuration Of course our farm would look something like this: public class Farm { private Chicken chicken; public void setChicken(Chicken chicken) { this.chicken = chicken; } … }

46 Lab 4: JSR 330 (DI) Annotations https://tech.lds.org/wiki/Introduction_to_Spring_C ontinued#Lab_4_JSR_330_Annotations

47 Credit where credit is due Spring Recipies 2 nd Edition (Gary Mak, Josh Long and Daniel Rubio)


Download ppt "Introduction to Spring Continued Matt Wheeler. Notes This is a training NOT a presentation Please ask questions Prerequisites – Introduction to Java Stack."

Similar presentations


Ads by Google