Dependency injection

DI is a system where dependencies of an object are provided by outside containers. Spring DI helps in wiring a class with its dependencies and keeping them decoupled so that we can inject these dependencies at runtime.

The dependencies are characterized in the bean configuration. The two most common approaches to injecting objects utilizing XML are constructor injection and setter injection, which we'll take a look at now:Constructor injection

Constructor injections inject dependencies to the class constructor. Let's take a look at an example of the constructor injection. Reuse the previous project and modify the content of beans.xml:

<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!--Constructor-based Dependency Injection Example Start-->
<bean id="userGreeting" class="ktPackage.UserGreeting">
<constructor-arg ref="userSurname" />
</bean>
<bean id="userSurname" class="ktPackage.UserSurname"/>
<!--Constructor-based Dependency Injection Example End-->
</beans>

constructor-arg is utilized to inject dependencies. The reference of constructor-arg is an object of the constructor.

Create a class of UserSurname.kt to see the use of the constructor injection. We'll get the surname from this class, as follows:

class UserSurname {
init {
println("This is init of UserSurname")
}

fun getSurname(){
println("This is the surname of user")
}
}

Initialize UserSurname and add the getUserSurname() function to CreateUserGreeting.kt:

// added a constractor of UserSurname
class
UserGreeting(surname: UserSurname) {
private var userSurname: UserSurname ?= surname
init {
println("It is a constructor for user's surname")
}

private var globalGreeting: String? = "Sasuke Uchiha"

fun setGreeting(greeting: String) {
globalGreeting = greeting
}

fun getGreeting() {
println("Welcome, " + globalGreeting!! + "!!")
}

fun afterPropertiesSet(){
println("Bean is going to start.")
}

fun destroy(){
println("Bean is going to destroy.")
}

fun getUserSurname(){
userSurname?.getSurname()
}
}

Now, if we call the getUserSurname() function in BeansScopeApplication, we'll get the UserSurname class.

Here's the sample code of BeansScopeApplication.kt:

fun main(args: Array<String>) {
val context = ClassPathXmlApplicationContext("Beans.xml")
val objectA = context.getBean("userGreeting", UserGreeting::class.java)
objectA.getUserSurname()

// objectA.setGreeting("Naruto Uzumaki")
// objectA.getGreeting()
// context.registerShutdownHook()
}

The output will be as follows:

This is init of UserSurname                <------ init from UserSurname.kt
It is a constructor for user's surname <------ init from UserGreeting.kt
This is the surname of user <------ getUserSurname() of UserGreeting.kt

Setter injection

In Spring, a setter injection is a kind of DI in which the framework injects the objects that are dependent on another object into the customer using a setter function. The container first calls the no contention constructor and then calls the setters. The setter-based injection will work regardless of whether a few dependencies have been injected utilizing the constructor.

Let's see an example of the setter injection. Here, reuse the previous project and modify the content of beans.xml:

<?xml version = "1.0" encoding = "UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--Setter Injection Example Start-->
<bean id="userGreeting" class="ktPackage.UserGreeting">
<property name="userSurnameClass" ref="userSurname"/>
</bean>
<bean id="userSurname" class="ktPackage.UserSurname"/>
<!--Setter Injection Example End-->
</beans>

After modifying the bean file, add a setter and getter of UserSurname to the CreateUserGreeting.kt file:

class UserGreeting {
private var userSurname: UserSurname? = null

fun setUserSurnameClass(surname: UserSurname) {

userSurname = surname
}

fun getUserSurnameClass(): UserSurname? {
return userSurname
}


private var
globalGreeting: String? = "Sasuke Uchiha"

fun setGreeting(greeting: String) {
globalGreeting = greeting
}

fun getGreeting() {
println("Welcome, " + globalGreeting!! + "!!")
}

fun afterPropertiesSet() {
println("Bean is going to start.")
}

fun destroy() {
println("Bean is going to destroy.")
}

fun getUserSurname() {
userSurname?.getSurname()
}
}

The result will be as follows:

This is init of UserSurname
Setting User Surname in UserGreeting
This is the surname of user

An example of an empty string or null value is as follows:

<bean id="app" class="App">
<property name="name" value=""/>
</bean>
<!-- If we need to pass an empty string or null as a value -->
<bean id="app" class="App">
<property name="name"><null/></property>
</bean>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.34.146