Zaczytany w książce Spring in action fifth edition spróbowałem postawić swoje własne zmienne konfiguracyjne ze spring bootem .
Proszę vardzo:
@Component
@ConfigurationProperties(prefix = "myprops")
public class MyClass {
private int myvar1;
// Getters and setters...
}
application.properties:
myprops.myvar1=3333
MyClass.getMyvar1()
powinien zwrócić 3333 a wciąż zwraca defaultowe 0.
@EnableConfigurationProperties(MyClass.class)
@SpringBootApplication
public class Demo1Application {
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
@Bean
public CommandLineRunner foo(ApplicationContext ctx) {
return args -> {
MyClass mc = new MyClass();
int x = mc.getMyvar1();
System.out.println(x);
};
}
}