{"id":23431951,"url":"https://github.com/keremtan/java-spring","last_synced_at":"2025-04-13T00:30:26.672Z","repository":{"id":61775358,"uuid":"552810934","full_name":"KeremTAN/Java-Spring","owner":"KeremTAN","description":"This repository has basic information about Spring Framework and a sample application made with Spring Boot.","archived":false,"fork":false,"pushed_at":"2023-01-30T12:27:32.000Z","size":581,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-26T18:22:30.033Z","etag":null,"topics":["back-end","dependency-injection","docker-compose","h2-database","java","postgresql-database","rest-api","spring","spring-boot","spring-framework"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/KeremTAN.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-10-17T09:06:19.000Z","updated_at":"2024-12-14T19:58:06.000Z","dependencies_parsed_at":"2023-02-16T07:15:34.039Z","dependency_job_id":null,"html_url":"https://github.com/KeremTAN/Java-Spring","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KeremTAN%2FJava-Spring","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KeremTAN%2FJava-Spring/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KeremTAN%2FJava-Spring/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KeremTAN%2FJava-Spring/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KeremTAN","download_url":"https://codeload.github.com/KeremTAN/Java-Spring/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248650591,"owners_count":21139670,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["back-end","dependency-injection","docker-compose","h2-database","java","postgresql-database","rest-api","spring","spring-boot","spring-framework"],"created_at":"2024-12-23T10:51:47.964Z","updated_at":"2025-04-13T00:30:26.628Z","avatar_url":"https://github.com/KeremTAN.png","language":"Java","readme":"# JAVA Spring\n\u003c!-- Lists --\u003e\n* [Spring Framework](#springframework)\n    * [The brief reasons why the Spring Framework is so popular and used are;](#why)\n        * [Plain Old Java Object(POJO) in Briefly](#pojo)\n        * [Inversion of Control(IoC) in Briefly](#ioc)\n        * [Spring's IoC Container in Briefly](#sioc)\n        * [Dependency Injection(DI) in Briefly](#di)\n            1. [Constructor Dependency Injection](#dic)\n            2. [Property(or Setter) Dependency Injection](#dip)\n            3. [Interface(or Method) Dependency Injection](#dii)\n            4. [[Bonus] Field Dependency Injection](#dif)\n* [Spring Boot](#springboot)\n    * [Spring Boot Layered Architecture](#sbla)\n        * [Presentation Layer](#pl)\n        * [Business Layer](#bl)\n        * [Persistence Layer](#pla)\n    * [Entity Objects](#eo)\n* [Application Programming Interface(API)](#api)\n* [Representational State Transfer(REST)](#rest)\n    * [REST API](#restapi)\n* [Challenge Project](#chpr)\n   * [Information about on Database](#db)\n   * [Request and Postman](#postman)\n   * [An output example from the Challenge Project](#output)\n\n\u003ca name=\"springframework\"\u003e\u003c/a\u003e\n## Spring Framework\n\n\u0026nbsp;  **Spring Framework** is an open source application framework which is leightweight has been designed by **Rod Johnson** and first version released in 2003.\n\n\u0026nbsp;  **The purpose of Spring Framework** is to enable easy, fast and reliable development of any robust and large-scale Java application. Additionally, it provides a bunch of different extensions that can be used to build all kinds of large-scale applications on Java EE (Java Enterprise Edition). Therefore, it is one of the most preferred frameworks in most enterprise Java applications.\n\n\u0026nbsp;  **The reason for the emergence of the Spring Framework** is that Java EE (new name Jakarta EE), which was widely used before the Spring Framework, has a very complex application development environment and a deep Java EE learning requirement due to this complexity. The fact that these requirements also increase the management cost of the applications has ensured that the Spring Framework has been kept in a short time against to Java EE.\n\n\u003ca name=\"why\"\u003e\u003c/a\u003e\n### The brief reasons why the Spring Framework is so popular and used are;\n * It uses POJO(Plain Old Java Object), don’t need an enterprise container like an application server.\n * It's is Inversion of Control (IoC) and Dependency Injection (DI) features provide the foundation for a wide-ranging set of features and functionality.\n * It provides a great level of modularity.\n * Well-Designed Web Framework.\n * Spring application code tends to be very easy to make test cases for various testings.\n * Middle-tier objects can be easily organized.\n\n\u003ca name=\"pojo\"\u003e\u003c/a\u003e\n\n### Plain Old Java Object(POJO) in Briefly\n\u0026nbsp; POJO is an ordinary object that is not subject to any special restrictions.\nThe POJO class does not have connections with another class, such as extends, implements.\nPOJO file does not require any special classpath. It increases the readability and reusability of a Java program.\n\nSome Properties of POJO\n * The POJO class must be public.\n * It can have a public default constructor or arguments constructor.\n * POJO Classes can have any access modifies such as private, public, protected.\n * A POJO class should not extend predefined classes.\n * It should not implement prespecified interfaces.\n * It should not have any prespecified annotation.\n * All objects must have some public Getters and Setters to access the object values by other Java Programs.\n \n A Code Example of POJO \u003c/br\u003e\n\n```java\npublic class Human{\n    private String name; \n    private String lastName;  \n    private int age;\n\n    public String getName() {  return name;  } \n    public void setName(String name) {  this.name = name;  } \n    public String getlastName() {   return lastName;  }  \n    public void setLastName(String lastName) {   this.lastName = lastName;  }\n    public int getage() {  return age;  }\n    public void setAge(int age) {  this.age = age;  }\n}\n```\n---------------------------------\n\n\u003ca name=\"ioc\"\u003e\u003c/a\u003e\n\n### Inversion of Control(IoC) in Briefly \u003c/br\u003e\nWhen a class uses another class, the class controls the another class.\u003c/br\u003e\nFor example, the save behavior of an object calls the save behavior of another object which is a component of the object which is created within itself in some situations. \u003c/br\u003e\nIn some cases, we want to take that control back into our own hands. \u003c/br\u003e\nThis is the inversion of control paradigm.\u003c/br\u003e\nThere are different solutions to do Inversion of Control.\u003c/br\u003e\nHowever, the most common way to do IoC is usually to inject another object component inside the object by injecting it from outside instead of rendering it inside the object. \u003c/br\u003e\nIn this way, we can set the save behavior of any desired object wanted to use the save behavior of the object.\u003c/br\u003e\nThat is called Dependency Injection principle.\u003c/br\u003e\nOther ideas for implementing the IoC paradigm apart from Dependency Injection are Strategy Design Pattern, Service Lacator Design Pattern and Factory Design Pattern. \u003c/br\u003e\n\n\u003ca name=\"sioc\"\u003e\u003c/a\u003e\n\n### Spring's IoC Container in Briefly\nSome of classes are used to carry data, while some of classes are used for their functionality. \u003c/br\u003e\nIt is usually sufficient for us to generate only unique an object from the classes we use for their functionality (in this way, the object generation design is called Singleton Design Pattern). \u003c/br\u003e\nIoC container is a kind of memory space in Spring. \u003c/br\u003e\nSingleton objects whose functionality we want to use over and over again are placed in the IoC Container. \u003c/br\u003e\nWhen the objects placed in the IoC Container are desired to be used, a reference is assigned to the relevant field with the Dependency Injection principle.\u003c/br\u003e\nIn this way, the objects that we want to use for their functionality are not constantly reproduced. \u003c/br\u003e\nThe @Autowired annotation is usually used for the Dependency Injection method in Spring. With the @Autowired annotation, the address of the required object is taken from the memory and a reference is assigned to the required field. \u003c/br\u003e\n\n\u003ca name=\"di\"\u003e\u003c/a\u003e\n\n### Dependency Injection(DI) in Briefly \u003c/br\u003e\nDependency Injection is that needed object(s) inject to the dependent object when an object is dependent on another object(s). \u003c/br\u003e\nThe main purpose of using DI principle is that needed object(s) can changed without changing dependent object when the needed object(s) to be changed. \u003c/br\u003e\nLikewise, when a change is made in the needed object(s), it is to prevent it from directly affecting dependent object. \u003c/br\u003e\nDependency Injection must take the control of the creating and etc. of needed object(s) for being able to do these. \u003c/br\u003e\nThis means that DI uses the IoC paradigsm.\u003c/br\u003e\n\n### Injection can be done in 3 different ways in Dependency Injection\n\u003ca name=\"dic\"\u003e\u003c/a\u003e\n\n * **Constructor Dependency Injection:** Needed class is provided through the dependency class' constructor. \u003c/br\u003e\n\n ```java\npublic class Controller{\n    private IServices service;\n    public Controller(IServices service) {\n        this.service=service;\n    }\n}\n```\n --------------------------------- \n\u003ca name=\"dip\"\u003e\u003c/a\u003e\n\n * **Property(or Setter) Dependency Injection:** The injector supplies the needed object(s) through a public property of the dependency class. \u003c/br\u003e\n\n ```java\npublic class Controller{\n    private IServices service;\n\n    public Controller() {}\n\n    public IServices getService() {\n        return this.service;\n    }\n    public void setService(IServices service) { \n        this.service=service\n    }\n}\n```\n --------------------------------- \n\u003ca name=\"dii\"\u003e\u003c/a\u003e\n\n * **Interface(or Method) Dependency Injection:** Provides a method that will pass the transmission of the needed object(s) to any dependent class.\nDependent classes must implement the interface which have the setter method of the object(s) they need.\u003c/br\u003e\n\n ```java\npublic interface IServicesDependency{\n    void setDependecy(IServices service);\n}\npublic class Controller implements IServicesDependency{\n    private IServices service;\n\n    public Controller() {}\n\n    @Override\n    public void setDependecy(IServices service) {\n        this.service=service;\n    }\n}\n```\n ---------------------------------\n\u003ca name=\"dif\"\u003e\u003c/a\u003e\n\n * **[Bonus] Field Dependency Injection:** Field Injection is a DI technique made thanks to the @Autowired annotation in Spring.\nIn fact, Field Injection was mentioned indirectly while explaining the IoC Container.\nDependent object takes the memory address of the needed object that already exists in the IoC Container and assigns the reference address to  related field belong to dependent object thanks to @Autowired annotation.\u003c/br\u003e\n\n```java\npublic class Controller{\n    @Autowired\n    private IServices service;\n    public Controller() {}\n}\n```\n --------------------------------- \n\u003ca name=\"springboot\"\u003e\u003c/a\u003e\n\n## Spring Boot\n\u0026nbsp; The main difference between Spring Framework and Spring Boot is that Spring Framework is a library and Spring Boot is a tool.\nSpring Boot makes it faster and easier to develop applications with the Spring Framework. \u003c/br\u003e\u003c/br\u003e\nA few features of Spring Boot that make it faster and easier \n * Creates stand-alone applications.\n * SB comes with Tomcat, Jetty or Undertow embedded.\n * SB does not need XML configuration.\n * SB aims to reduce LOC (Lines of Code).\n * SB is easy to start.\n * Customization and management is simple.\n \n \u003ca name=\"sbla\"\u003e\u003c/a\u003e\n\n ### Spring Boot Layered Architecture\n\u0026nbsp; This is used architectural structure in Spring Boot.\nIn layered artichecture, classes are divided into certain groups/layers and each divided group/layer has its own task.\nThe reason why the file structures are arranged in this way is that the reusability, maintainability and debugability of the projects(especially the complex ones) can be controlled and updated in a right way. \u003c/br\u003e\n\n\u003ca name=\"pl\"\u003e\u003c/a\u003e\n\nThe first of these layers is the **Presentation Layer** \u003c/br\u003e\n\u0026nbsp; The Presentation Layer communicates with the client. The **@RestController** or **@Controller** annotations are used to indicate that the classes in the Presentation Layer belong to that layer. In this layer, requests(data) from the client are transferred to the next layer(Business Layer) via Database Transfer Objects(DTO) or data which is came from the Business Layer is returned to the client. \u003c/br\u003e\n\n\u003ca name=\"bl\"\u003e\u003c/a\u003e\n\nThe second of these layers is the **Business Layer** \u003c/br\u003e\n\u0026nbsp; The Business Layer is the layer where the desired work is done. For example, calculating the discount of the price of the products in the cart, etc. in an e-commerce application.\nThe **@Service** anonotation is used to indicate that the classes in the Business Layer belong to this layer.\nThe desired solution is produced with the data transferred to it from the Presentation Layer via DTO in the Business layer. If the produced solution is needed to be returned directly to the client, it is returned to the Presentation layer with DTO in this layer. If the produced solution is needed to be saved in the database, it is transferred to the next layer (Persistence Layer) with Entity objects from this layer. \u003c/br\u003e\n\n\u003ca name=\"pla\"\u003e\u003c/a\u003e\n\nThe third of these layers is the **Persistence Layer** \u003c/br\u003e\n\u0026nbsp; Persistence Layer communicates with the Database Layer. Hollow interfaces instead of classes are defined in this layer. Interfaces are defined with **@Repository** annotation in this layer. Spring makes the necessary implementations for the interfaces when it sees **@Repository** annotation and makes its interfaces with Repository annotation ready for use. Entity objects from the Business Layer are sent to the Database Layer or the data requested from the Database Layer are sent to the Business Layer as Entity objects in this layer. \u003c/br\u003e\nPersistence Layer and Presentation Layer cannot communicate directly with each other! \u003c/br\u003e\n\n\u003ca name=\"eo\"\u003e\u003c/a\u003e\n\n**Entity Objects** \u003c/br\u003e\n\u0026nbsp; Entity objects are defined with **@Entity** annotation. An Entity class corresponds to a table in database.  For example, an AdminEntity class corresponds to the Admin table in the database. Fields defined with special annotations in Entity classes are created as columns in the database and the values of these fields are added/deleted or etc. to the relevant columns. \u003c/br\u003e\n\n\u003ca name=\"api\"\u003e\u003c/a\u003e\n \n## Application Programming Interface(API)\n\u0026nbsp;  APIs allow the functions of one application to be used in another application.\nIn this way, applications can be run that are independent of each other in an integrated way.\nIn this respect, the APIs are like a puzzle.\nDifferent platforms can connected and meet their needs using their APIs and services.\nThis means that APIs allows services and products to communicate with each other and leverage each other's data and functionality through a documented interface. \u003c/br\u003e\n\nA few advantages of APIs\n * Centralizing different platforms in one panel.\n * Scattered and large structures can be combined and managed.\n * APIs makes sharing and distributing content easier.\n * APIs enables the most used content and services to be customizable.\n * Contents can be automatically broadcast for each channel.\n * APIs makes service delivery more flexible.\n * Different versions of the same application can be developed on many different devices thanks to APIs.\n\n\u003ca name=\"rest\"\u003e\u003c/a\u003e\n\n## Representational State Transfer(REST) \nREST is an architecture that enables lightweight and easy Clint-Server communication with HTTP. \u003c/br\u003e\nRoy Fielding developed this architecture as a doctoral thesis in the 2000s.\u003c/br\u003e\nREST allows communication with a variable URL rather than a constant URL as in Simple Object Access Protocol (SOAP).\u003c/br\u003e\nIf you want to get information from the http://theserver/example web address in SOAP, the relevant method should be run on this address.\u003c/br\u003e\nFor example; GetUsers method\u003c/br\u003e\nIf you want to get information in Rest, you can directly access the related method with the variable web address logic.\u003c/br\u003e\nFor example; http://theserver/example/GetUser or http://theserver/example/GetUser/8.\u003c/br\u003e\n\n\u003ca name=\"restapi\"\u003e\u003c/a\u003e\n\n### REST API\n\u0026nbsp; A REST API is an API that conforms to the design principles or representative state transfer architecture style of REST.\nREST APIs are sometimes called RESTful APIs.\nREST APIs is used to get information from a web service or to provide information to a website.\nAll communications over the REST API use HTTP request only. \u003c/br\u003e\n\n\u0026nbsp; Working paradigm of Rest APIs A request is sent to the server by the client as HTTP POST, GET, PUT, PATCH, DELETE and etc. method were in the form of a web URL. These correspond to create, read, update and delete (or CRUD) operations respectively. After the server receives the request, it sends a like HTML, XML, Image, JSON or etc. recource to the client.\u003c/br\u003e\n\n**POST** is mostly used to create new record. Returns HTTP status 201 when successfully created. \u003c/br\u003e\n\n**GET** is used to read/get the representation of a record. It returns HTTP status 200 (OK) when secure. It usually returns a status of HTTP 404 (NOT FOUND) or  HTTP 400 (BAD REQUEST) when the event of an error. \u003c/br\u003e\n\n**PUT** is used to update but If the record ID is chosen by the client rather than the server, it can also be used to create a record.\nOn a successful update, it returns an HTTP status of 200 (or 204 if not returning any content in the body). PUT returns HTTP status 201 on successful a record creation If it is used to create a record. \u003c/br\u003e\n\n**PATCH** is used to modify. PATCH request should only contain changes to the record. It should not the entire record.\nThis is similar to PUT but the body contains a set of instructions that explain how a record currently on the server must be modified to produce a new version.\nTherefore, PATCH body must not be just a modified part of the record but it must be in some kind of patch language like JSON Patch or XML Patch. \u003c/br\u003e\n\n**DELETE** is used by the URI to delete an identified record. It returns a 200 (OK) HTTP status on successful a record deletion.\n\n\n\u003ca name=\"chpr\"\u003e\u003c/a\u003e\n\n## Challenge Project\n\n \u0026nbsp; I have developed a Rest API where you can see the stock market share prices of companies which are done in the Challenge Project module. I got stock markets information from https://www.alphavantage.co/documentation/. \u003c/br\u003e\nHere is an example json object related to the data provided by this api;\n```json\n{\n    \"Meta Data\": {\n        \"1. Information\": \"Daily Time Series with Splits and Dividend Events\",\n        \"2. Symbol\": \"IBM\",\n        \"3. Last Refreshed\": \"2023-01-27\",\n        \"4. Output Size\": \"Compact\",\n        \"5. Time Zone\": \"US/Eastern\"\n    },\n    \"Time Series (Daily)\": {\n        \"2023-01-27\": {\n            \"1. open\": \"134.44\",\n            \"2. high\": \"135.488\",\n            \"3. low\": \"133.7701\",\n            \"4. close\": \"134.39\",\n            \"5. adjusted close\": \"134.39\",\n            \"6. volume\": \"8143146\",\n            \"7. dividend amount\": \"0.0000\",\n            \"8. split coefficient\": \"1.0\"\n        },\n        ...\n    }\n}\n```\n\u003ca name=\"db\"\u003e\u003c/a\u003e\n\u0026nbsp; First of all, I populated my own database using Alphavantage's API. If there is no data in the database, all the information is filling the database into the database while the loader object is being created. If our database is not empty then the loader object adds that data to our database if a new data was added to the Alphavantage API the previous day. The h2 database is currently used as the database.\nIf you want to use the Postgresql as the database then you can remove the comment lines in the **application.properties** section and add the h2 database settings to the comment line.\nIf the postgresql database is not installed on your computer, you can go to the **'src/main/recources'** directory and run the\n```bash\ndocker-compose -f docker-compose.yml up -d\n```\ncommand to pull the postgresql image to your own computer.\n\n\u003ca name=\"postman\"\u003e\u003c/a\u003e\n\u0026nbsp; You can see stock prices by making date and company code based requests to this Rest API with Postman. You can go to the **'src/main/recources/PostmanCollection'** directory and import the json file there to your Postman and use the collections I have already prepared. \u003c/br\u003e\nAn image from the Postman's Collections; \u003c/br\u003e\n![UML](https://github.com/KeremTAN/Spring_JAVA/blob/main/ChallengeProject/src/main/resources/PostmanCollections/collections.png)\n\n\u003ca name=\"output\"\u003e\u003c/a\u003e\nAn output example from the Challenge Project; \u003c/br\u003e\n```json\n{\n    \"date\": \"2023-01-27\",\n    \"symbol\": \"IBM\",\n    \"1. open\": \"134.44\",\n    \"2. high\": \"135.488\",\n    \"3. low\": \"133.7701\",\n    \"4. close\": \"134.39\",\n    \"5. adjusted close\": \"134.39\",\n    \"6. volume\": \"8143146\"\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeremtan%2Fjava-spring","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkeremtan%2Fjava-spring","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeremtan%2Fjava-spring/lists"}