Application Context
์ดํ๋ฆฌ์ผ์ด์ ๊ตฌ์ฑ์ ๋ํ ์ ๊ทผ์ ๊ฐ๋ฅ์ผ ํ๋ ์ธํฐํ์ด์ค (์ค๋ช )
Spring Boot ์์๋ ApplicationContextAware ๊ฐ ์ฌ์ฉ๋๋ฉฐ, ํด๋น ์ธํฐํ์ด์ค๋ ApplicationContext์ ๋ํ ์ ๋ณด๋ฅผ ๋ฐ๊ณ ์ ํ๋ ๊ฐ์ฒด์ ์ํด ๊ตฌํ๋๋ค. ์ง๊ธ ์ดํด๋ณด๊ณ ์ ํ๋ ๊ฒ์, Beans์ ์ก์ธ์ค ํ์ฌ ํน์ Bean์ get ๋๋ set ํ๊ธฐ ์ํ ๋ฐฉ๋ฒ์ด๋ค.
package helloworld;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.stereotype.Component;
@Component
public class Beans implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static GenericApplicationContext getGenericContext() {
return (GenericXmlApplicationContext) applicationContext;
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
์ ์ฝ๋๋ฅผ ํตํด ApplicationContext๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ผ๋ฉฐ, ๋น์ ์ก์ธ์ค ํ ์ ์๊ฒ๋๋ค.
getApplicationContext().getBean() ์ ํตํด ์ํ๋ ๊ณณ์์ ๋น์ ๊ฐ์ ธ์ ์ฌ์ฉํ๋ค.
** Spring ๊ตฌ๋ ์, ApplicationContextAware ๊ตฌํ์ด ๋ฐ๊ฒฌ๋๋ฉด setApplicationContext๋ฅผ ์คํํ๋ค.
์ฐธ๊ณ
How to get bean using application context in spring boot
I am developing a SpringBoot project and I want to get the bean by its name using applicationContext. I have tried many solution from web but could not succeed. My Requirement is that I have a cont...
stackoverflow.com
'๐ฎSpring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring Boot] Tiles ์ฌ์ฉํ๊ธฐ. (0) | 2022.02.02 |
---|---|
[Spring Boot] Tag Library ์ฌ์ฉํ๊ธฐ (0) | 2022.02.02 |
[Spring Boot] Spring Boot 2.6.2 Interceptor ์ถ๊ฐ (0) | 2022.01.26 |
[ Spring Multi DataSource ] Read / Write Dynamic DataSource (0) | 2021.10.07 |
[Spring Boot Gradle] Hikari CP MAX Connection Pool ์ค์ ํ๊ธฐ (0) | 2021.07.24 |
๋๊ธ