I want to make a bean which has values from properties file. I made something like this:
@Component
public class MainNavs implements Iterable<Nav>{
@Value("${newshome.navs.names}")
String[] names;
@Value("${newshome.navs.ids}")
String[] ids;
final private List<Nav> navs = new ArrayList<Nav>();
public MainNavs() throws Exception {
for (int i = 0; i < names.length; i++) {
navs.add(new Nav(names[i], ids[i]));
}
}
public Iterator<Nav> iterator() {
Iterator<Nav> n = navs.iterator();
return n;
}
public class Nav {
private String name;
private String id;
private String imageNumber;
public Nav(String name, String id, String imageNumber) {
this.name = name;
this.id = id;
}
//....
}
}
But when I autowire like this @Autowired MainNavs navs;, it makes NullPointerException because the names and ids were not initiated when it try to access those in constructor.
If I made some method like init() and tried to initiate with it, there wasn't any problem.
public init() throws Exception {
for (int i = 0; i < names.length; i++) {
navs.add(new Nav(names[i], ids[i]));
}
}
However, I don't want initiate manually. Can I initiate it in constructor? Or any other alternatives?
Use @PostConstruct on your init() method - it will be called by spring as soon as the object is inside the spring context, which means all if its dependencies will be injected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With