вторник, 16 августа 2016 г.

Strange: autowiring and spring boot resource reading

Strange things:
1. Spring bean autowiring might not work as expected, the beans your bean depends on are not instantinated and turn out to be null. As in this case:
@Autowired
private SomeBean someBean;

@Bean
public OtherBean otherBean() {
    OtherBean b = new OtherBean();
 b.setSome(someBean); // someBean here can be null!
    return b;
} 

So you can try to pass the someBean through parameter, this worked for me once:

@Bean
public OtherBean otherBean(SomeBean someBean) {
    OtherBean b = new OtherBean();
 b.setSome(someBean); // someBean will be instantinated
    return b;
}

2. If you want to read some resource from spring boot jar, you may run into FileNotFoundException, even if actually file will be on the specified path. Reading the resource as InputStream should help:

try (BufferedReader buffer = new BufferedReader(
new InputStreamReader(cpr.getInputStream()))) 
{
    List<String> lines = buffer.lines().collect(Collectors.toList());
    lines.forEach(System.out::println);
}

Комментариев нет:

Отправить комментарий