BeanClassLoaderAware 테스트

이전 글에서 사용한 Bean 클레스에 BeanClassLoaderAware 인터페이스를 추가로 구현합니다.

public class BeanLifeCycleTestBean implements BeanNameAware, BeanClassLoaderAware{

    String beanName;

    ClassLoader classLoader;

    public void setBeanName(String beanName) {
        System.out.println(“setBeanName() 실행합니다.”);
        this.beanName = beanName;

    public String getBeanName(){
        return beanName;

    public void setBeanClassLoader(ClassLoader classLoader) {
        System.out.println(“setBeanClassLoader() 실행합니다.”);
        this.classLoader = classLoader;

    public ClassLoader getClassLoader() {
        return classLoader;

간단한 로깅을 위해서 문자열을 출력해 줍니다. 빈 설정은 바뀔 것이 없으며 테스트 코드만 약간 변경합니다.

public class BeanLifeCycleTest {

    private ApplicationContext context;
    private BeanLifeCycleTestBean bean;

    public void setUp(){
        context = new ClassPathXmlApplicationContext(“net/agilejava/jedi/spring/beanLifeCycle/applicationContext.xml”);
        bean = (BeanLifeCycleTestBean) context.getBean(“test”);

    public void testBeanNameAware() {
        assertEquals(“test”, bean.getBeanName());

    public void testClassLoaderAware() {
        ClassLoader classLoader = bean.getClassLoader();


Spring 소스코드에서 BeanClassLoader 인터페이스를 구현한 클레스들입니다.
– AbstractBeanFactory :: configurableBeanFactory 인터페이스 구현체
– AbstractBeanDefinitionReader :: Set the ClassLoader to use for bean classes. Default is null, which suggests to not load bean classes eagerly but rather to just register bean definitions with class names, with the corresponding Classes to be resolved later (or never).
– ConfigurableBeanFactory 인터페이스 :: Set the class loader to use for loading bean classes. Default is the thread context class loader. Note that this class loader will only apply to bean definitions that do not carry a resolved bean class yet. This is the case as of Spring 2.0 by default: Bean definitions only carry bean class names, to be resolved once the factory processes the bean definition.
– AbstractHttpInvokerRequestExecutor
– HttpInvokerTests

This is mainly intended to be implemented by framework classes which
have to pick up application classes by name despite themselves potentially
being loaded from a shared class loader.

ClassLoader 에 대한 학습이 필요하겠군요. 얼추 보니까 loadClass(“풀 패키지 이름 붙은 클레스명”) 을 사용하여 Class 객체를 받아 올 수 있습니다. 그 이후에는 리플렉션으로 이어지겠군요.

ClassLoader를 테스트 하기 위해 테스트 코드를 약간 수정했습니다.

    public void testClassLoaderAware() throws ClassNotFoundException {
        ClassLoader classLoader = bean.getClassLoader();
        Class clazz = classLoader.loadClass(“net.agilejava.jedi.spring.beanLifeCycle.BeanLifeCycleTestBean”);
        assertEquals(BeanLifeCycleTestBean.class, clazz);