728x90
๋ฑ๋ก๋ ๋ชจ๋ ๋น ์กฐํํ๊ธฐ
๋ฑ๋ก๋ ์คํ๋ง ๋น์ ํ์ธํ๊ธฐ ์ํด ApplicationContext๋ฅผ ์ฃผ์ ๋ฐ์ ๋ค, beanDefinitionNames()๋ฅผ ํตํด ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
๊ฐ๋จํ ์์๋ฅผ ์ํด CommandLineRunner๋ฅผ ํตํด ๋น์ด ๋ชจ๋ ๋ฑ๋ก๋ ํ ๋น์ ์ถ๋ ฅํ๋๋ก ๋ง๋ค์์ต๋๋ค.
@Component
class BeanPrinter(
ac: ApplicationContext
) : CommandLineRunner {
private final val ac: ApplicationContext = ac
override fun run(vararg args: String?) {
val beanDefinitionNames = ac.beanDefinitionNames
for (beanDefinitionName in beanDefinitionNames) {
val bean = ac.getBean(beanDefinitionName)
println("name = ${beanDefinitionName} + object = ${bean}")
}
}
}
๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
๋น ์ด๋ฆ์ผ๋ก ์กฐํํ๊ธฐ
@Component
class BeanPrinter(
ac: ApplicationContext
) : CommandLineRunner {
private final var ac: ApplicationContext = ac
override fun run(vararg args: String?) {
val memberService = ac.getBean("memberService", MemberService::class.java)
println(memberService)
}
}
ํ์ ์ผ๋ก๋ง ์กฐํํ๊ธฐ
@Component
class BeanPrinter(
ac: ApplicationContext
) : CommandLineRunner {
private final var ac: ApplicationContext = ac
override fun run(vararg args: String?) {
val memberService = ac.getBean(MemberService::class.java)
println(memberService)
}
}
๊ฐ์ ํ์ ์ด ์ฌ๋ฌ๊ฐ์ธ ๊ฒฝ์ฐ์๋ ๋ค์๊ณผ ๊ฐ์ ์์ธ๊ฐ ๋ฐ์ํฉ๋๋ค.
ํน์ ํ์ ์ ๋น ๋ชจ๋ ์ถ๋ ฅํ๊ธฐ
getBeansOfType์ ์ฌ์ฉํฉ๋๋ค.
@Component
class BeanPrinter(
ac: ApplicationContext
) : CommandLineRunner {
private final var ac: ApplicationContext = ac
override fun run(vararg args: String?) {
val beansOfType = ac.getBeansOfType(MemberService::class.java)
beansOfType.forEach { (k, v) -> println("key = ${k} value = ${v}") }
}
}
728x90