Spring dependency injection

938 views

Published on

Published in: Technology, News & Politics
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
938
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
14
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Spring dependency injection

  1. 1. Dependency Injection with SpringPresenter: Danil Kornishev 1
  2. 2. Introduction• DI is a defined methodology• DI is needed• DI is not magical 2
  3. 3. Conceptual Model Interface Programming to an InterfaceDeployment creates need for a wiring Impl1 Impl2 Impl3 component in the deployment layer 3
  4. 4. Bean Definition<bean id="sslContextFactory" class="com.db.fw.entl.remoteClient.rest.impl.ssl.EtlSSLConfig“ scope="prototype" > <property name="keyStoreFile" value="certs/keystore_rds" /> <property name="keyStorePass" ref="password" /> <property name="trustStoreFile" value="certs/truststore_rds" /> <property name="trustStorePass" ref="password" /></bean><bean id="serviceLocator" class="com.db.fw.entl.locator.LoadAwareHTTPServiceLocator" init-method="startPolling"><constructor-arg> <map> <entry key="READ_SERVICE"> <bean class="java.lang.String"> <constructor-arg value="https://localhost:1998" /> </bean> </entry> </map></constructor-arg><constructor-arg value="300000" /><constructor-arg name="sslCtx"> <bean factory-bean="sslContextFactory" factory-method="createSSLContext" /></constructor-arg></bean><bean id="staticFactoryCreated" class="com.static.factory" factory-method="newSSLContext" destroy-method="close" /> Beans Constructor Factory bean Init Scope Inline Property Static Factory Destroy 4
  5. 5. Additional Initialization • @PostConstructAnnotations • Invoked when DI is finished • @PreDestroy • Invoked before context is destroyed @PostConstruct public void warmup() Init/Cleanup { // initialize using injected properties } @PreDestroy public void teardown() { // close connections, send notifications...etc } 5
  6. 6. Collections <list> <util:list id="emails"> <value>pechorin@hero.org</value> <value>pechorin@hero.org</value>Lists <value>raskolnikov@slums.org</value> <value>raskolnikov@slums.org</value> <value>stavrogin@gov.org</value> <value>stavrogin@gov.org</value> <value>porfiry@gov.org</value> <value>porfiry@gov.org</value> </list> </util:list> <set> <util:set id="emails"> <value>pechorin@hero.org</value> <value>pechorin@hero.org</value>Sets <value>raskolnikov@slums.org</value> <value>raskolnikov@slums.org</value> <value>stavrogin@gov.org</value> <value>stavrogin@gov.org</value> <value>porfiry@gov.org</value> <value>porfiry@gov.org</value> </set> </util:set> <map> <util:map id="emails"> <entry key="pec" value=“chorin@hero.org"/> <entry key="pech" value=“chorin@hero.org"/>Maps <entry key="rask" value="rask@slums.org"/> <entry key="rask" value="rask@slums.org"/> <entry key="starogin" value=“sn@gov.org"/> <entry key="stavrogin" value=“sn@gov.org"/> <entry key="porfiry" value=“iry@gov.org"/> <entry key="porfiry" value=“iry@gov.org"/> </map> </util:map> Inline Stand-Alone 6
  7. 7. Spring Context UsageCreate the ContextApplicationContext ctx = new ClassPathXmlApplicationContext("/path/to/config1.xml","/path/to/config2.xml"); Obtain the BeanServiceLocator locator = (ServiceLocator)ctx.getBean("serviceLocator"); UseURL sererHost = locator.locate("myservice"); Close when donectx.close(); 7
  8. 8. Raising Context Awareness Create a class that implements Context Awarenessclass SpringContext implements ApplicationContextAware{ @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { // Do something like caching the context }} Reference it in a configuration file<bean id="contextAware" class="com.db.fw.entl.context.SpringContext" />Now, when Spring is done loading the context, itwill invoke SpringContext.setApplicationContextpassing in the context created. 8
  9. 9. Life after XMLSpring 3 allows Java-only configurationDefine beans using familiar languageNo XML files -- live XML-free 9
  10. 10. Spring 3 Annotations • @Configuration • Marks a class as a bean provider • @Bean • Marks a method as a bean provider • @Lazy, @Scope, @Primary • Same goodies you know and love • @ImportResource • Interoperability with Spring XML@Configuration @Lazy @Primary@Bean @Scope @ImportResource 10
  11. 11. Java-only Bean Factory@Configurationpublic class AnnotationConfiguredBeans{ @Bean public String greeting() { return "Hello World "; } @Primary @Bean @Lazy public ComplexValue complex() { return new ComplexValue(simpleValue1(), simpleValue2()); } @Bean @Scope(BeanDefinition.SCOPE_PROTOTYPE) public SimpleValue simpleValue1() { return new SimpleValue("Good Bye“, Math.random()*1000); }} 11
  12. 12. Annotation Driven ContextCreate the ContextApplicationContext ac = new AnnotationConfigApplicationContext(AnnotationConfiguredBeans.class); Use it in the same way as you would an XML generated context. Programming to an interface works! Freedom from the XML tyranny! 12
  13. 13. Alternative DI FrameworksEJB 3.0 Google Guice 13
  14. 14. Q&A 14

×