Beans initialization map using Spring JavaConfig

I have a class AbstractParentwith class autwired variable fieldand Childthat extends AbstractParent. In my application, I need a subclass map AbstractParentto implement a strategy template.

I am running my application using Spring JavaConfig. Here is part of the config class:

    @Bean
    public String field() {
        return "Field of abstract parent class";
    }

    @Bean
    public Child child() {
        return new Child();
    }

    @Bean
    public HashMap<String, Object> initMap() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("child", child());
        return map;
    }

But after initialization, the child of the HashMap has an uninitialized field. I find an interesting thing. Below are parts of my test:

@Before
public void setUp() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(Config.class);
    context.refresh();
    child = context.getBean(Child.class);
    map = context.getBean(HashMap.class);
}
@Test
public void test() {
    assertNotNull(child.getField());
    System.out.println(child);
    AbstractParent childFromMap = map.get("child");
    System.out.println(childFromMap);
    assertNotNull(childFromMap);
    System.out.println(childFromMap.getField());
    assertNotNull(childFromMap.getField()); //FAIL =(
}

The variable Childhas a nonzero field, and the var field childFromMapis null. In println, I see that these are two different objects. Therefore, I have a suggestion that the object for the map was not created in the context of Spring.

So my question is: Is it possible to populate a map with objects created in JavaConfig?

\ .

AbstractParent

import org.springframework.beans.factory.annotation.Autowired;

public abstract class AbstractParent {

    @Autowired
    protected String field;

    protected abstract void method();

    protected String getField() {
        return field;
    }
}

Child

public class Child extends AbstractParent{

   @Override
   protected void method() {
       System.out.println(field);
   }
}

TestConfig

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertNotNull;

public class TestConfig {
private Child child;
private Map<String,AbstractParent> map;
static class Config {

    @Bean
    public String field() {
        return "Field of abstract parent class";
    }

    @Bean
    public Child child() {
        return new Child();
    }

    @Bean
    public HashMap<String, Object> initMap() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("child",child());
        return map;
    }

}

@Test
public void test() {
    assertNotNull(child.getField());
    System.out.println(child);
    AbstractParent childFromMap = map.get("child");
    System.out.println(childFromMap);
    assertNotNull(childFromMap);
    System.out.println(childFromMap.getField());
    assertNotNull(childFromMap.getField());
}

@Before
public void setUp() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(Config.class);
    context.refresh();
    child = context.getBean(Child.class);
    map = context.getBean(HashMap.class);
}

}

PS: .

+4
1

, @Configuration Config. Spring bean . @Configuration , @Bean Lite, - Java-

Spring @Bean doc

+1

Source: https://habr.com/ru/post/1624668/


All Articles