Why do we need Repository in Spring Boot Applications - spring-boot

I am new to spring boot, and recently started learning it, I have a question why do we create a repository in spring boot applications. Can anyone please explain to me logically?
Thanks in advance.

We build app to present data. These data may be static and small(In case we don't need Database).
But if want to store it some where and get it from somewhere we need services your service with use some data or consume some services.
So whenever to want to have a controller over the data and want to store it for future use you have to store to. To control the data you use repository.

Related

What is the best way to maintain queries in Spring boot application?

In My Application, Using the below technologies
Spring boot 2.7.x
Cassandra
spring batch 5. x
java 11
As part of this, I need to extract data from the Cassandra database and need to write out the file
so here I need to use queries to fetch data so
just want to know what is the best way to maintain all queries at one place so any query changes come in the future, I shouldn't build the app rather just need to modify the query.
Using a repository class is necessary. If you are using JPA i recommend using a repository for each Entity class. With JDBC it is possible to create a single repository which contains all the queries. To access the query methodes i would use a service class. In this way your code is structured well and maintainable for future changes.

Don't want to store properties in database

I have some properties in my microservice. They are changed a lot so I want to store them somewhere out of microservice so that I don't have to undeploy it again and again. One solution is to store them in database but it will be less efficient that way. Can you advise me a solution where I can store them?
Basically this microservice is used by a vast number of people. I want that file to be read ONCE when microservice is deployed (unless or until there is some change in the file) to minimize the calls.
You should use Spring Cloud Config which enables to use a centralized server which exposes a Git Repo which you can use to store environment specific configuration files (application.properties)
You should go with Centralized configuration, spring cloud supports config server, It is highly recommended for microservice architecture.
For reference: https://spring.io/guides/gs/centralized-configuration/
Basically, you have a centralized place to store all the configurations and don't need the redeploy the application every time you change the configs.
NOTE: in case you change the db properties then probably you need the restart the service

Which datastore (database) should be used for Spring-boot REST API application with AZURE

There are may blog available around this but still not getting exactly what is needed.
I am trying to write a REST API with Spring Boot and store data in database. Here the database structure may change (new tables can get introduced or some existing names may get renamed).
Which DB can be used so that there would be minimal code changes needed both at java side and DB side.
What could be a best design approach in this scenario considering technology stack as Spring Boot and Azure
Please visualize about your persistent storage? Why Azure Only? Refine question.
e.g. H2 database with Spring Boot is the most memory efficient.
see Lightest Database to be packed with an application
About Minimal code changes - I'd go with one of the ORM - JPA(or Hibernate). So will only need to maintain #Entity class on java side.
Don't forget - minimal changes still need to be addressed at database & Java side.

Spring Statemachine persister for spring data

We are currently investigating spring state machine and we have a very similar need to the eventservice sample with a pool and context switching using a repository, the only problem is redis is linux only (for production) and we can not lean on that... is there a clean out-of-the-box way to integrate persistence using spring data or will i have to write my own implementation for StateMachinePersister.
https://github.com/spring-projects/spring-statemachine/tree/master/spring-statemachine-samples/eventservice/src/main/java/demo/eventservice
should i go about using AbstractStateMachinePersister or StateMachinePersist?
thanks!
There is no OOB spring data integration so you need to use low level API's to build your own persist impl. Having said that, spring data support has been in my mind but just haven't had time to push it forward. PR's highly appreciated ;)
StateMachinePersister is an interface to follow which AbstractStateMachinePersister implements.

When to use default Spring Data REST behavior?

I recently worked on a project which uses Spring Data REST with Spring Boot. While it is GREAT to harness the power of Spring Data REST and build a powerful web service in no time, I have come to regret one thing: how tightly coupled the "presentation" layer (JSON returns) is to the underlying data structure.
Sure, I have used Projections and ResourceProcessors to manipulate the JSON, but that still does not completely sever ties with the database structure.
I want to introduce Controllers to the project, to integrate some of the "old" ways of building a web service in Spring. But how should I draw the line? I don't want to eradicate Spring Data REST from my project.
I am sure many of you have faced similar decisions, so any advice would be most appreciated!

Resources