728x90
내가 얼핏 듣기로는 스프링 컨텍스트가 프록시로 만든다는 말을 들은 적이 있는데.. 확실하지가 않다..
모든 빈들을 프록시로 만드는 건지..
그래서 간단하게 테스트 해보았다.
@Component
public class Service {
public void test(){
System.out.println("test");
}
}
@Configuration
public class Config {
@Bean
public Service service(){
return new Service();
}
}
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
Service service = (Service) applicationContext.getBean("service");
service.test();
}
해서 디버깅해보니...
오..?? 그냥 일반객체를 받아오네??
그럼 스프링 빈은 언제 Proxy객체로 만들어 질까..??
그래서 빈 생성과정에서 사용되는 클래스중 AbstractAutoProxyCreator를 살펴보니
/**
* Wrap the given bean if necessary, i.e. if it is eligible for being proxied.
* @param bean the raw bean instance
* @param beanName the name of the bean
* @param cacheKey the cache key for metadata access
* @return a proxy wrapping the bean, or the raw bean instance as-is
*/
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) {
return bean;
}
if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
return bean;
}
if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return bean;
}
// Create proxy if we have advice.
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
if (specificInterceptors != DO_NOT_PROXY) {
this.advisedBeans.put(cacheKey, Boolean.TRUE);
Object proxy = createProxy(
bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
}
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return bean;
}
"Create proxy if we have advice"라고 딱 적혀져있다.
다시 말해 SpringContext가 필요여부에 따라서 스프링 빈을 proxy로 만들지 아닐지 결정하여 빈으로 등록한다.
음.. 그냥 단순하게 궁금했다 스프링컨텍스트가 과연 모든 빈들을 프록시로 등록할지.. 그래서 어찌어찌 찾아보니 그건 아니더라..!
'개발 이론 > Spring' 카테고리의 다른 글
프록시??? 스프링 AOP?? 트랜젝션?? (3) (0) | 2024.03.12 |
---|---|
프록시??? 스프링 AOP?? 트랜젝션?? (2) (0) | 2024.03.02 |
프록시??? 스프링 AOP?? 트랜젝션?? (1) (0) | 2024.02.22 |
트랜젝션에 대해 알아낸거를 기록하자 (0) | 2023.12.28 |
API호출 RestTemplate, WebClient, Feign?? (0) | 2023.12.21 |