Consider the following classes:
public class Store {
private final ArrayList<String> store;
public ArrayList<String> getStore() {
return store;
}
public Store(ArrayList<String> store){
this.store = store;
}
}
I have a text file called input.txt
I have a normal controller which is annotated with @RestController
as follows:
@RestController
@RequestMapping(value="/test")
public class Controller {
.
.
.
}
I need to do the following operations:
- Read input.txt by using
Files.readAllLines(path,cs)
(from JDK 1.7) - Set the returned value(
List<String>
) toStore.store
- I wanna use Spring annotations all the way (I'm writing a spring-boot application)
- I need Store to be a Singleton bean.
- The store needs to be initialized during the bootstrapping of my application.
This question might be too vague, but I have absolutely no idea about how to make it more specific.
P.S.
I'm a newbie to Spring.
It seems like using Constructor Injection would be ideal.
you will want to add the store.file.path property to your properties file that is read in by your spring context. You will also want to add a bean definition for Store
<bean id="Store" class="com.test.Store" />
Then your Rest Controller can look something like this:
There's a couple different ways to write your injection & controller so do some research and use the method that best suites your needs.