How to load a JSON config file on start up automatically using Jackson in Java Springboot

Viewed 3026

I have these files:

@Component
@PropertySource(value = "classpath:prop.json")
public class Prop {
    String hello;
    String bye;
   ...

   // Getters and Setters and toString
}

My Application

@SpringBootApplication
@ComponentScan(basePackageClasses = { Prop.class})
public class TestApp {
public static void main(String[] args) {
    SpringApplication.run(TestApp.class, args);
}

}

My prop.json configuration file

{
  "hello": "test1",
  "bye": "test2"
}

My test class:

@ExtendWith(SpringExtension.class)
@ContextConfiguration(
        classes = Prop.class)
class PropTest {

    @Autowired
    private Prop jsonProperties;

    @Test
    public void testone() {
        System.out.println(jsonProperties.toString());
    }
}

My test is printing out : Prop{hello='null', bye='null'}

It works fine when I use an ObjectMapper and retrieve it the old fashion way, but shouldn't Springboot be able to load configs like this? What am I missing?

0 Answers
Related