16
1. Advantages : lighweight(POJO), loose coupling(DI and interface orientation), declarative programming(aspects and common conventions), boilerplate reduction(aspects) 2. Does not lock u in to the framework 3. Difficult to write tests against 4. Does not litter ur code with its API 5. Traditionally each object is responsible to obtain own refs to objects ti corroborates with(itddepndencies) 6. Tightly coupled code 7. Coupling is necsaary but shud be managed 8. Wiring : creating associations between componenets 9. Application context loads bean definitions and wires them together 10. Several implementations of application context, each differing in how they load their configuration 11. Aspects : AOP promotes seperation of concerns 12. Cross cutting concerns : logging , trnasaction and security 13. These concerns are duplicated across modules, and mix up with core functionality 14. AOp makes possible to modularize these concermns and apply them declaratively 15. Resulting in cohesive systems

Chapter 1-3

Embed Size (px)

DESCRIPTION

Spring In Action - 3rd Edition

Citation preview

1. Advantages : lighweight(POJO), loose coupling(DI and interface orientation), declarative programming(aspects and common conventions), boilerplate reduction(aspects)2. Does not lock u in to the framework3. Difficult to write tests against4. Does not litter ur code with its API5. Traditionally each object is responsible to obtain own refs to objects ti corroborates with(itddepndencies)6. Tightly coupled code7. Coupling is necsaary but shud be managed8. Wiring : creating associations between componenets9. Application context loads bean definitions and wires them together10. Several implementations of application context, each differing in how they load their configuration11. Aspects : AOP promotes seperation of concerns12. Cross cutting concerns : logging , trnasaction and security13. These concerns are duplicated across modules, and mix up with core functionality14. AOp makes possible to modularize these concermns and apply them declaratively15. Resulting in cohesive systems

16. Before advice after advice AspectJ's pointcut expression language17. U can do anything with aspects dat u can with other psring beans18. Spring container : creates configures wires and manages the complete lifecycle of beans19. Uses DI 20. Spring comes with several container implementations 21. 2 distinct categories : Bean factories(basic support for DI, BeanFactory interface) and Application contexts(ApplicationContext interface, provides application framework services like resolving messages from prop file and publishing events to listeners)22. Several flavors of application context , 3 most popular are : ClassPathXmlApplicationContext, FileSystemXmlApplicationContext, XmlWebApplicationContext

23. Instanitae bean --> inject values and refs --->setBeanName if it implements BeanNameAware -->setBeanFactory if Bean FactoryAware -->ApplicationContextAwaresetBeanContext---->BeanPostProcessorpostProcessBeforeIntitialization --->afterpropertiesSet---->postProcessorAfterIntilizationdestroy on close of applicationcontext if DisposableBean

24. What if the bean does not have a public constructor25. Instantiate thru factory method using factory-method attribute

26. Bean scoping : by default all spring beans are singletons, if u want a new one each time one is needed set scope attribute to prototype

27. Springs notion of singleton is limited to spring context28. If u specify several bean declarations that instaiate the same class29. Intitlializing and destroying beans

Method called after instantiation, method called before bean is removed from container30. Alternative to above is to have bean implement the InitilaizingBean and DisposableBean interfaces with afterPropertiesSet and destroy methods31. Shud use interfaces only when bean is to be specifically within springs container else use declarations32. If several beans have the same init and destroy methods

33. I njecting into bean properties : setter injection

34. Like inner classes we can have inner bean if the bean is not to be exposed to other beans

35. Even constructor arg can be given an inner bean

36. It is not necessary to give an id to an inner bean37. Declare p namespace schema in xml and use it in place of elements

38. Wiring collections

39. , when prop is implementation of Collection or array40. Actual implementation of Collection has no relation to set or list tag41. , Map, Properties, props when key and value are strings and map when they are any type

42. Other value setting spring elements of can be , , 43. may contain another for multidimensional lists

44. Wiring map collections

45. Wiring properties collection

46. element is for injecting a value o ref into a property of class47. And here it is 48. Wiring nothing, null49. But why null coz all properties in bean wud start out as null . 2 reasons : bean may set it but u want to force it to null always. 2nd is if u want to override autowired property value

50. SpELChapter 3Autowiring : reduce or elimnate the need for or by letting spring figure out how to wire bean dependenciesAutodiscovery : lets spring figure out which classes shud be configured as beans reducing the need for element4 kinds of autowiring1. byName : attempts to match the properties of the autowired bean with beans that have the same name2. byType : assignable to properties3. constructor : assignable to contructorargs4. autodetect : attempts by constructor then by typebyName : matches prop name with id name

byTypeone problem with this is if u have multiple beans whose type is assinable to the autowired property, spring will throw exception2 solutions : 1. identify primary candidate for autowiring 2. Eliminate beans from autowiring candidacyWeird side of primary it defaults to true

Autowiring by constructor

Its got the same problems as byType, cant automatically figure out which to to assign. also multiple constructors can be satisfied

Best-fit autowiring

Autowire by constructor first then attempt by type

Default autowiring

1. default autowire will be applied to all beans in that config file2. u cud have mutipleconfig filesMixing auto with explicit wiring

Wiring with annotationsAnnotation wiring isnt configured in spring container by default

Spring supports a few different annotations for autowiring@Autowired, @Inject, @Resource1. u can have @Autowired on setter or another method , even constructor and the field itself2. when spring sees the @Autowired it does a byTypesearc

2 problemsNo matching bean throws exception NoSuchBeanDefinitionExceptionSolutionOptional Autowiring : using required attribute

1. when used on constructor only one constructor can have required value true2. spring will use the constructor which has the most arguments that can be satisfiedQualifying ambiguous dependcies : qualifier attribute

1. matches the id with qualifier2. other than id u can give a qualfier to bean itself

3. or can even qualify a class

Creating custom qualifier

If ther's more than on @StringedInstrument u can further qualifiy

If you dont want spring specific dependencyUse @Inject1. rather than inject a ref directly u can ask @Inject to inject a Provider : enables lazy injection and injection of multiple instances of bean

2. Knife bean shud be defined with scope prototype3. At construction only a provider is injected and leter get () Is called on provider 5 times4. Qualifying @Inject : answer to ambiguous bean definitions : @Named

5. @Named searches by ID only6. Custome Qualifier : this is diff from spring only name same

Can annotation sbe used to wire values into String and other primitive valuesUsing expressions with annotation injection

@Value is just an expression and can evaluate down to any type and thus can be applied to any propertyBut its not used for hardcoding values, its combined with SpEL

Automatically discovering beans1. So far whatever u have done, u must still define 2. does everything plus is configues spring to automatically discover beans and declare them for you

3. It scans the base package and all the sub packages for beans looks for following annotations1. @Component : class is a Spring component2. @Controller : class is Spring MVC controller3. @Repository : class defines a data repository4. @Service : class defines a service

1. Spring will register it as bean using camel case as id if none providedFiltering component scans1. To avoid having to Annotate a lot of instrument implementations with Component or we may not have access to all implementations2. Use and or 3. To automatically register all classes that are assignable to Intrument

4. In above we tell that all classes that are assignable to Intrument to be registed as components5. Filtering types

6. To not register a class with a custom @SkipIt annotation use below

1. If you dont want XMl used Java based configuration2. To bootstrap the java based configuration u still need the basic spring xml with component scan3. Next define the Configuration class4. It is basically a class annotated with @Configuration and containing @Bean annotations on methods.

1. The id of bean is the name of method, so u get some type checking, rather than giving string namesInjecting with Spring's Java based configuration1. Create a juggler bean that juggles 15 bean bags

2. Setter injection

3. Wiring refs to other beans

4. The call to method in constructor is interpretted as wiring the bean registed with that method, and not a call to method