Spring dependency injection

14
Dependency Injection with Spring Presenter: Danil Kornishev 1

Transcript of Spring dependency injection

Dependency Injectionwith Spring

Presenter: Danil Kornishev1

• DI is a defined methodology

• DI is needed

• DI is not magical

2

Introduction

3

Conceptual Model

Interface

Dep

loym

ent

Impl1 Impl2 Impl3

Programming to an Interface

creates need for a wiring

component in the deployment layer

<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

Inline

Constructor

Property

Factory bean

Static Factory

Init

DestroyScope

4

Bean Definition

• @PostConstruct• Invoked when DI is finished

• @PreDestroy• Invoked before context is destroyed

@PostConstruct

public void warmup()

{

// initialize using injected properties

}

@PreDestroy

public void teardown()

{

// close connections, send notifications...etc

}

5

An

no

tati

on

s

Init

/Cle

anu

p

Additional Initialization

Collections

<list>

<value>[email protected]</value>

<value>[email protected]</value>

<value>[email protected]</value>

<value>[email protected]</value>

</list>

Sets

6

<util:list id="emails">

<value>[email protected]</value>

<value>[email protected]</value>

<value>[email protected]</value>

<value>[email protected]</value>

</util:list>

<set>

<value>[email protected]</value>

<value>[email protected]</value>

<value>[email protected]</value>

<value>[email protected]</value>

</set>

<util:set id="emails">

<value>[email protected]</value>

<value>[email protected]</value>

<value>[email protected]</value>

<value>[email protected]</value>

</util:set>

<map>

<entry key="pec" value=“[email protected]"/>

<entry key="rask" value="[email protected]"/>

<entry key="starogin" value=“[email protected]"/>

<entry key="porfiry" value=“[email protected]"/>

</map>

<util:map id="emails">

<entry key="pech" value=“[email protected]"/>

<entry key="rask" value="[email protected]"/>

<entry key="stavrogin" value=“[email protected]"/>

<entry key="porfiry" value=“[email protected]"/>

</util:map>

List

sM

aps

Inline Stand-Alone

ApplicationContext ctx = new ClassPathXmlApplicationContext("/path/to/config1.xml","/path/to/config2.xml");

7

Create the Context

Obtain the Bean

Spring Context Usage

ServiceLocator locator = (ServiceLocator)ctx.getBean("serviceLocator");

Use

URL sererHost = locator.locate("myservice");

Close when done

ctx.close();

class SpringContext implements ApplicationContextAware

{

@Override

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException

{

// Do something like caching the context

}

}

8

Create a class that implements Context Awareness

Raising Context Awareness

<bean id="contextAware" class="com.db.fw.entl.context.SpringContext" />

Reference it in a configuration file

Now, when Spring is done loading the context, it will invoke SpringContext.setApplicationContextpassing in the context created.

9

Life after XML

Spring 3 allows Java-only configurationDefine beans using familiar languageNo XML files -- live XML-free

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

@Bean

@Lazy

@Scope

@Primary

@ImportResource

11

Java-only Bean Factory

@Configuration

public 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);

}

}

12

Annotation Driven Context

ApplicationContext ac = new AnnotationConfigApplicationContext(AnnotationConfiguredBeans.class);

Create the Context

Use it in the same way as you would an XML generated context.Programming to an interface works!Freedom from the XML tyranny!

13

Alternative DI Frameworks

EJB 3.0 Google Guice

14

Q & A