#1 ์ด์ ๊ธ
[Android] Dagger2 - ๊ธฐ์ด
#1 ์ด์ ๊ธ ์์กด์ฑ ์ฃผ์ (Dependency Injection)#1 ์์กด์ฑ ์ฃผ์ (Dependency Injection)์ด๋?#1-1 Dependent์ Dependencyfun main() { val car = Car() car.startCar() } class Car { private val engine = Engine() private val airbag = Airbag() private
kenel.tistory.com
์ ๊ฒ์๊ธ์ ์์ฑ๋ ์ฑ์ ์ผ๋ถ ์์ ํด์, Application์ ํ์ฉํ๊ฒ ๋ง๋ ๋ค. ์ด๋ฅผ ํตํด, @Component ํด๋์ค ์ธ์คํด์ค์ ์ค๋ณต ํธ์ถ์ ์ ๊ฑฐํ๋ค.
#2 Application์ ํ์ฉ์ฑ
Application | Android Developers
developer.android.com
์ ํ๋ฆฌ์ผ์ด์
ํด๋์ค๋ ์๋๋ก์ด๋ ์ฑ์ ๊ธฐ๋ณธ ํด๋์ค๋ก ๋ชจ๋ Activity์ ์์ฑ ์ ์ ์์ฑ๋๋ ์ต์์ ๊ฐ์ฒด์ด๊ธฐ๋ ํ๋ค. Application์ ์ฑ์ ์ ์ญ์์ ์ ์ผํ๊ฒ(= ์ฑ๊ธํค) ์คํ๋๋ ํด๋์ค๋ผ๋ ํน์ง์ด ์๋ค. ๊ทธ๋ฆฌ๊ณ ์ด ํน์ง์ dagger๋ฅผ ์ฌ์ฉํ ๋ ์ ์ ํ ์ด์ฉํ ์ ์๋ค.
์ด์ ๊ธ์์๋ MainActivity๋ก๋ถํฐ @Component ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ์์ฑํด์ Car ๊ฐ์ฒด๋ฅผ ์ป์๋ค. ์ด ๋, ์ค์ ์๋๋ก์ด๋ ์ฑ์์ ์ฌ๋ฌ ๊ฐ์ Activity๊ฐ ์กด์ฌํ๋ ์ํฉ์ด๋ผ๋ฉด, ๊ทธ ๊ฐฏ์๋งํผ @Component ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ์ป์ด์ผํ ๊ฒ์ด๋ค. ์ฆ ์ฝ๋์ ์ค๋ณต์ด ๋ฐ์ํ๋ค. ์ด ์ค๋ณต๋๋ ๋์์ ํด๊ฒฐํ๋ ์ข์ ๋ฐฉ๋ฒ ์ค ํ๋๊ฐ ๋ฐ๋ก ํด๋น @Component ์ธ์คํด์ค๋ฅผ Application ๊ฐ์ฒด์ ์ ์ํ๋ ๊ฒ์ด๋ค.
#3 ์ฝ๋ ์์
#3-1 ๊ฐ์
์ด์ , Application ํด๋์ค๋ฅผ ์์๋ฐ์ ์ฌ์ฉ์ ์ ์ Application ํด๋์ค๋ฅผ ๋ง๋ค๊ณ , ๊ธฐ๋ณธ Application ๋์ ์ฌ์ฉ์ ์ ์ Application ํด๋์ค๊ฐ ์คํ๋๊ฒ ๋ง๋ค์ด๋ณด๊ฒ ๋ค.
#3-2 MyApplication.kt ์์ฑ
// package com.example.useapplication
import android.app.Application
class MyApplication : Application() {
lateinit var carComponent: CarComponent
override fun onCreate() {
super.onCreate()
carComponent = initDagger()
}
private fun initDagger(): CarComponent {
return DaggerCarComponent.create()
}
}
#3-3 AndroidManifest.xml ์์
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:name=".MyApplication"
...
</application>
</manifest>
<application> ํด๋์ค์ android:name ์์ฑ์ ์ถ๊ฐํ๋ค. ์ด ์์ฑ์ ์ฑ์ ๊ธฐ๋ณธ Application ํด๋์ค๋ฅผ ์ง์ ํ๋ค. ์ฌ๊ธฐ์ ๋ฐฉ๊ธ ๋ง๋ค์๋ MyApplication ํด๋์ค๋ฅผ ํ ๋นํ๋ค. ์ด์ , Dagger์ Component ํด๋์ค๋ฅผ Application์ ์ด๊ธฐํํ์ฌ ์ ์ญ์ ์ผ๋ก ์ฌ์ฉํ ์ค๋น๊ฐ ๋๋ฌ๋ค.
์ฌ๋ด์ผ๋ก, ์๋๋ก์ด๋ ํ๋ก์ ํธ๋ฅผ ๋ง๋ค๋ฉด <application>์ android:name ์์ฑ์ ๊ธฐ๋ณธ์ ์ผ๋ก ์๋ต๋์ด์๋ค. ์ด ๊ฒฝ์ฐ ์๋๋ก์ด๋ ์์คํ
์ ๊ธฐ๋ณธ Application ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ค. ๋งค์ฐ ๊ฐ๋จํด์ ์ ์ญ์ ์ธ ์ด๊ธฐํ ์์
์ด๋ ์ํ ๊ด๋ฆฌ๊ฐ ํ์ํ์ง ์์ ์ฑ์ android:name ์์ฑ์ ์๋ตํด๋ ๋๋ค.
#3-4 MainActivity.kt ์์
// package com.example.useapplication
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedState: Bundle?) {
super.onCreate(savedState)
setContentView(R.layout.activity_main)
/*
...
*/
/* application์ getํ๊ณ MyApplication์ผ๋ก ํ์
์บ์คํ
.
* ํ์
์บ์คํ
์ ์ด์ :
* application ๊ฐ์ฒด๋ ๊ธฐ๋ณธ์ ์ผ๋ก Application ํ์
์ผ๋ก ์ ๊ณต๋์ง๋ง,
* ์ค์ ๋ก๋ MyApplication ํด๋์ค์ ์ธ์คํด์ค์ด๊ธฐ ๋๋ฌธ์ด๋ค.
*/
val car = (application as MyApplication).carComponent.getCar()
car.startCar()
}
}
#4 ์๋ ํ์ธ (๋ก๊ทธ ๋ฉ์์ง)
Crankshaft is ready
Cylinder is ready
Piston is ready
Engine is ready
Airbag is ready
Battery is ready
Car is ready
#5 ์์ฝ
Application์ ํ์ฉ์ ์ผ์ข
์ ์ฑ๊ธํค ๊ธฐ๋ฒ์ด๋ผ๊ณ ๋ ๋ณผ ์ ์๋ค.
#6 ์์ฑ๋ ์ฑ
android-practice/dagger2/UseApplication at master · Kanmanemone/android-practice
Contribute to Kanmanemone/android-practice development by creating an account on GitHub.
github.com
'๊นจ์ ๊ฐ๋ ๐ > Android' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android] Unit Testing - ๊ธฐ์ด (0) | 2024.07.08 |
---|---|
[Android] Unit Testing - ๊ฐ์์ ํ๊ฒฝ ์ค์ (2) | 2024.07.03 |
[Android] Dagger2 - Activity์ Dependency ์ฃผ์ (0) | 2024.06.25 |
[Android] Dagger2 - ์ธํฐํ์ด์ค ๊ตฌํ์ฒด ์ฃผ์ (@Binds) (0) | 2024.06.25 |
[Android] Dagger2 - ๋งค๊ฐ๋ณ์ ๋์ ํ ๋น (0) | 2024.06.24 |