Using an ArrayList as a datasource in an ephemeral Spring Boot project Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Create ArrayList from arrayWhen to use LinkedList over ArrayList in Java?Initialization of an ArrayList in one lineSort ArrayList of custom Objects by propertyConvert ArrayList<String> to String[] arrayWhat's the difference between @Component, @Repository & @Service annotations in Spring?Unit testing a Spring Boot service class with(out) repository in JUnitHow to configure port for a Spring Boot applicationUnable to autowire dozer Mapper in component class in spring bootSpring Boot: Failed .. write HTTP message: springframework.http.converter.HttpMessageNotWritableException

How much damage would a cupful of neutron star matter do to the Earth?

What do you call the main part of a joke?

What initially awakened the Balrog?

Project Euler #1 in C++

Why are my pictures showing a dark band on one edge?

Maximum summed subsequences with non-adjacent items

What's the meaning of "fortified infraction restraint"?

Why does it sometimes sound good to play a grace note as a lead in to a note in a melody?

Why is it faster to reheat something than it is to cook it?

How can I prevent/balance waiting and turtling as a response to cooldown mechanics

Putting class ranking in CV, but against dept guidelines

Amount of permutations on an NxNxN Rubik's Cube

How did Fremen produce and carry enough thumpers to use Sandworms as de facto Ubers?

What does it mean that physics no longer uses mechanical models to describe phenomena?

Random body shuffle every night—can we still function?

Converted a Scalar function to a TVF function for parallel execution-Still running in Serial mode

How can I set the aperture on my DSLR when it's attached to a telescope instead of a lens?

Is it possible for SQL statements to execute concurrently within a single session in SQL Server?

Is CEO the "profession" with the most psychopaths?

Getting prompted for verification code but where do I put it in?

Co-worker has annoying ringtone

How could we fake a moon landing now?

What does Turing mean by this statement?

How to save space when writing equations with cases?



Using an ArrayList as a datasource in an ephemeral Spring Boot project



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Create ArrayList from arrayWhen to use LinkedList over ArrayList in Java?Initialization of an ArrayList in one lineSort ArrayList of custom Objects by propertyConvert ArrayList<String> to String[] arrayWhat's the difference between @Component, @Repository & @Service annotations in Spring?Unit testing a Spring Boot service class with(out) repository in JUnitHow to configure port for a Spring Boot applicationUnable to autowire dozer Mapper in component class in spring bootSpring Boot: Failed .. write HTTP message: springframework.http.converter.HttpMessageNotWritableException



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am building a REST provider with Spring Boot. It is ephemeral by design i.e. the data can be lost when the application is killed. So, I decided to use an ArrayList to make CRUD operations on: it should be like a singleton -created with the app and used along the way.



I have this rest controller, with the service layer autowired:



@RestController
public class MyController

@Autowired
private MyServiceInterface myService;

@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public List<MyEntity> retrieveAll()
return myService.getAll();


@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public MyEntity create(@RequestBody MyEntity entity)
return myService.insert(entity);




and the MyService implementing MyServiceInterface is as follows:



@Service
public class MyService implements MyServiceInterface

// This is my ArrayList to live while the Spring Boot app runs
private List<MyEntity> myList = new ArrayList<MyEntity>();

@Override
public MyEntity insert(MyEntity entity)
this.myList.add(entity);
return entity;


@Override
public List<MyEntity> getAll()
return this.myList;




So, is it fine to use a humble private myList object in the serive class as shown above, or should I take a different approach (inject that myList after assigning it as a @Bean, add @Configuration or whatsoever Spring stuff)?



EDIT: Not to sail away from my concern, my primary point is not discussing databases instead of lists, but how to declare a variable used by multiple methods of a Spring Bean.










share|improve this question
























  • Forgot to state that, just added to question: any kind of database (in-memo, rdbms, so on) is not allowed

    – vahdet
    Mar 8 at 22:17











  • isn't your list an in memory list? you also do not want to use an array list because it's not thread safe

    – Taugenichts
    Mar 8 at 22:17











  • Apart from the thread-safety (which is a good point of yours), I mean third party in-memory data grid solutions.

    – vahdet
    Mar 8 at 22:19











  • Unless you need to share data across multiple instances of your program, I think any kind of data grid is overkill. Data structures that just need to exist ephemerally inside a single process are a great use of Python's build in data structures. You can deal with the thread safety without bringing in complexity of some sort of data grid module. - if you ARE talking about sharing across processes, then Redis is a great solution. Very simple to use. But it requires external setup. Not at all necessary for a single process that doesn't need to preserve it's data across restarts.

    – Steve
    Mar 8 at 22:23







  • 2





    ah, ok. well, it doesn't matter much how your list comes into existence. No need to fancy it up with spring injection unless there's some external reason to do that, like mocking for testing or something like that. whatever gets it done is probably fine. In terms of thread safety, it depends on how efficient you need to be. A basic solution is that you can turn many non-thread-safe java classes into thread safe ones with something like this: List newList = Collections.synchronizedList(new ArrayList());

    – Steve
    Mar 8 at 22:31

















0















I am building a REST provider with Spring Boot. It is ephemeral by design i.e. the data can be lost when the application is killed. So, I decided to use an ArrayList to make CRUD operations on: it should be like a singleton -created with the app and used along the way.



I have this rest controller, with the service layer autowired:



@RestController
public class MyController

@Autowired
private MyServiceInterface myService;

@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public List<MyEntity> retrieveAll()
return myService.getAll();


@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public MyEntity create(@RequestBody MyEntity entity)
return myService.insert(entity);




and the MyService implementing MyServiceInterface is as follows:



@Service
public class MyService implements MyServiceInterface

// This is my ArrayList to live while the Spring Boot app runs
private List<MyEntity> myList = new ArrayList<MyEntity>();

@Override
public MyEntity insert(MyEntity entity)
this.myList.add(entity);
return entity;


@Override
public List<MyEntity> getAll()
return this.myList;




So, is it fine to use a humble private myList object in the serive class as shown above, or should I take a different approach (inject that myList after assigning it as a @Bean, add @Configuration or whatsoever Spring stuff)?



EDIT: Not to sail away from my concern, my primary point is not discussing databases instead of lists, but how to declare a variable used by multiple methods of a Spring Bean.










share|improve this question
























  • Forgot to state that, just added to question: any kind of database (in-memo, rdbms, so on) is not allowed

    – vahdet
    Mar 8 at 22:17











  • isn't your list an in memory list? you also do not want to use an array list because it's not thread safe

    – Taugenichts
    Mar 8 at 22:17











  • Apart from the thread-safety (which is a good point of yours), I mean third party in-memory data grid solutions.

    – vahdet
    Mar 8 at 22:19











  • Unless you need to share data across multiple instances of your program, I think any kind of data grid is overkill. Data structures that just need to exist ephemerally inside a single process are a great use of Python's build in data structures. You can deal with the thread safety without bringing in complexity of some sort of data grid module. - if you ARE talking about sharing across processes, then Redis is a great solution. Very simple to use. But it requires external setup. Not at all necessary for a single process that doesn't need to preserve it's data across restarts.

    – Steve
    Mar 8 at 22:23







  • 2





    ah, ok. well, it doesn't matter much how your list comes into existence. No need to fancy it up with spring injection unless there's some external reason to do that, like mocking for testing or something like that. whatever gets it done is probably fine. In terms of thread safety, it depends on how efficient you need to be. A basic solution is that you can turn many non-thread-safe java classes into thread safe ones with something like this: List newList = Collections.synchronizedList(new ArrayList());

    – Steve
    Mar 8 at 22:31













0












0








0








I am building a REST provider with Spring Boot. It is ephemeral by design i.e. the data can be lost when the application is killed. So, I decided to use an ArrayList to make CRUD operations on: it should be like a singleton -created with the app and used along the way.



I have this rest controller, with the service layer autowired:



@RestController
public class MyController

@Autowired
private MyServiceInterface myService;

@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public List<MyEntity> retrieveAll()
return myService.getAll();


@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public MyEntity create(@RequestBody MyEntity entity)
return myService.insert(entity);




and the MyService implementing MyServiceInterface is as follows:



@Service
public class MyService implements MyServiceInterface

// This is my ArrayList to live while the Spring Boot app runs
private List<MyEntity> myList = new ArrayList<MyEntity>();

@Override
public MyEntity insert(MyEntity entity)
this.myList.add(entity);
return entity;


@Override
public List<MyEntity> getAll()
return this.myList;




So, is it fine to use a humble private myList object in the serive class as shown above, or should I take a different approach (inject that myList after assigning it as a @Bean, add @Configuration or whatsoever Spring stuff)?



EDIT: Not to sail away from my concern, my primary point is not discussing databases instead of lists, but how to declare a variable used by multiple methods of a Spring Bean.










share|improve this question
















I am building a REST provider with Spring Boot. It is ephemeral by design i.e. the data can be lost when the application is killed. So, I decided to use an ArrayList to make CRUD operations on: it should be like a singleton -created with the app and used along the way.



I have this rest controller, with the service layer autowired:



@RestController
public class MyController

@Autowired
private MyServiceInterface myService;

@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public List<MyEntity> retrieveAll()
return myService.getAll();


@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public MyEntity create(@RequestBody MyEntity entity)
return myService.insert(entity);




and the MyService implementing MyServiceInterface is as follows:



@Service
public class MyService implements MyServiceInterface

// This is my ArrayList to live while the Spring Boot app runs
private List<MyEntity> myList = new ArrayList<MyEntity>();

@Override
public MyEntity insert(MyEntity entity)
this.myList.add(entity);
return entity;


@Override
public List<MyEntity> getAll()
return this.myList;




So, is it fine to use a humble private myList object in the serive class as shown above, or should I take a different approach (inject that myList after assigning it as a @Bean, add @Configuration or whatsoever Spring stuff)?



EDIT: Not to sail away from my concern, my primary point is not discussing databases instead of lists, but how to declare a variable used by multiple methods of a Spring Bean.







java spring spring-boot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 23:01







vahdet

















asked Mar 8 at 22:07









vahdetvahdet

2,29951636




2,29951636












  • Forgot to state that, just added to question: any kind of database (in-memo, rdbms, so on) is not allowed

    – vahdet
    Mar 8 at 22:17











  • isn't your list an in memory list? you also do not want to use an array list because it's not thread safe

    – Taugenichts
    Mar 8 at 22:17











  • Apart from the thread-safety (which is a good point of yours), I mean third party in-memory data grid solutions.

    – vahdet
    Mar 8 at 22:19











  • Unless you need to share data across multiple instances of your program, I think any kind of data grid is overkill. Data structures that just need to exist ephemerally inside a single process are a great use of Python's build in data structures. You can deal with the thread safety without bringing in complexity of some sort of data grid module. - if you ARE talking about sharing across processes, then Redis is a great solution. Very simple to use. But it requires external setup. Not at all necessary for a single process that doesn't need to preserve it's data across restarts.

    – Steve
    Mar 8 at 22:23







  • 2





    ah, ok. well, it doesn't matter much how your list comes into existence. No need to fancy it up with spring injection unless there's some external reason to do that, like mocking for testing or something like that. whatever gets it done is probably fine. In terms of thread safety, it depends on how efficient you need to be. A basic solution is that you can turn many non-thread-safe java classes into thread safe ones with something like this: List newList = Collections.synchronizedList(new ArrayList());

    – Steve
    Mar 8 at 22:31

















  • Forgot to state that, just added to question: any kind of database (in-memo, rdbms, so on) is not allowed

    – vahdet
    Mar 8 at 22:17











  • isn't your list an in memory list? you also do not want to use an array list because it's not thread safe

    – Taugenichts
    Mar 8 at 22:17











  • Apart from the thread-safety (which is a good point of yours), I mean third party in-memory data grid solutions.

    – vahdet
    Mar 8 at 22:19











  • Unless you need to share data across multiple instances of your program, I think any kind of data grid is overkill. Data structures that just need to exist ephemerally inside a single process are a great use of Python's build in data structures. You can deal with the thread safety without bringing in complexity of some sort of data grid module. - if you ARE talking about sharing across processes, then Redis is a great solution. Very simple to use. But it requires external setup. Not at all necessary for a single process that doesn't need to preserve it's data across restarts.

    – Steve
    Mar 8 at 22:23







  • 2





    ah, ok. well, it doesn't matter much how your list comes into existence. No need to fancy it up with spring injection unless there's some external reason to do that, like mocking for testing or something like that. whatever gets it done is probably fine. In terms of thread safety, it depends on how efficient you need to be. A basic solution is that you can turn many non-thread-safe java classes into thread safe ones with something like this: List newList = Collections.synchronizedList(new ArrayList());

    – Steve
    Mar 8 at 22:31
















Forgot to state that, just added to question: any kind of database (in-memo, rdbms, so on) is not allowed

– vahdet
Mar 8 at 22:17





Forgot to state that, just added to question: any kind of database (in-memo, rdbms, so on) is not allowed

– vahdet
Mar 8 at 22:17













isn't your list an in memory list? you also do not want to use an array list because it's not thread safe

– Taugenichts
Mar 8 at 22:17





isn't your list an in memory list? you also do not want to use an array list because it's not thread safe

– Taugenichts
Mar 8 at 22:17













Apart from the thread-safety (which is a good point of yours), I mean third party in-memory data grid solutions.

– vahdet
Mar 8 at 22:19





Apart from the thread-safety (which is a good point of yours), I mean third party in-memory data grid solutions.

– vahdet
Mar 8 at 22:19













Unless you need to share data across multiple instances of your program, I think any kind of data grid is overkill. Data structures that just need to exist ephemerally inside a single process are a great use of Python's build in data structures. You can deal with the thread safety without bringing in complexity of some sort of data grid module. - if you ARE talking about sharing across processes, then Redis is a great solution. Very simple to use. But it requires external setup. Not at all necessary for a single process that doesn't need to preserve it's data across restarts.

– Steve
Mar 8 at 22:23






Unless you need to share data across multiple instances of your program, I think any kind of data grid is overkill. Data structures that just need to exist ephemerally inside a single process are a great use of Python's build in data structures. You can deal with the thread safety without bringing in complexity of some sort of data grid module. - if you ARE talking about sharing across processes, then Redis is a great solution. Very simple to use. But it requires external setup. Not at all necessary for a single process that doesn't need to preserve it's data across restarts.

– Steve
Mar 8 at 22:23





2




2





ah, ok. well, it doesn't matter much how your list comes into existence. No need to fancy it up with spring injection unless there's some external reason to do that, like mocking for testing or something like that. whatever gets it done is probably fine. In terms of thread safety, it depends on how efficient you need to be. A basic solution is that you can turn many non-thread-safe java classes into thread safe ones with something like this: List newList = Collections.synchronizedList(new ArrayList());

– Steve
Mar 8 at 22:31





ah, ok. well, it doesn't matter much how your list comes into existence. No need to fancy it up with spring injection unless there's some external reason to do that, like mocking for testing or something like that. whatever gets it done is probably fine. In terms of thread safety, it depends on how efficient you need to be. A basic solution is that you can turn many non-thread-safe java classes into thread safe ones with something like this: List newList = Collections.synchronizedList(new ArrayList());

– Steve
Mar 8 at 22:31












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071659%2fusing-an-arraylist-as-a-datasource-in-an-ephemeral-spring-boot-project%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55071659%2fusing-an-arraylist-as-a-datasource-in-an-ephemeral-spring-boot-project%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved