2

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:

  1. Read input.txt by using Files.readAllLines(path,cs) (from JDK 1.7)
  2. Set the returned value(List<String>) to Store.store
  3. I wanna use Spring annotations all the way (I'm writing a spring-boot application)
  4. I need Store to be a Singleton bean.
  5. 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.

3
  • I have a couple questions that should make your question more specific... 1) Are you trying to make a RESTful Web Service? If so, does this file need to be passed to a server, is it on the server, or is it on a user's local machine? 3) when you say arrayList<String> store, is this a list of store names? What format can you expect the input.txt to be in? Commented May 26, 2017 at 13:39
  • stackoverflow.com/questions/1363310/… and mkyong.com/spring/… Commented May 26, 2017 at 13:44
  • Yes, I am trying to make a RESTful WS. and the input.txt is on the server, UTF-8 format. Commented May 26, 2017 at 13:48

1 Answer 1

2

It seems like using Constructor Injection would be ideal.

public class Store {
    private final List<String> storeList;

    @Autowired
    public Store(@Value("${store.file.path}") String storeFilePath) throws IOException {
            File file = new File(storeFilePath);
            storeList = Files.readAllLines(file.toPath());
    }
}

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:

@RestController
@RequestMapping(value="/store")
public class StoreRestController {

    @Autowired
    Store store;

    @RequestMapping(value="/get", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Store> getStore(HttpServletRequest request) {
        ResponseEntity<Store> response = new ResponseEntity<Store>(store, HttpStatus.OK);
        return response;
    }
}

There's a couple different ways to write your injection & controller so do some research and use the method that best suites your needs.

Sign up to request clarification or add additional context in comments.

3 Comments

I would consider adding cache, since reading file is heavy operation, and should be made once
The file is readed just once per Store instance, so if default singleton scope is used for the bean, the file would be readed just once
This is just what I was looking for. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.