{"id":21332207,"url":"https://github.com/thisoecode/springboot-studynote_following-devtiro","last_synced_at":"2025-03-16T01:09:32.014Z","repository":{"id":222150916,"uuid":"755868416","full_name":"ThisoeCode/springboot-studynote_following-devtiro","owner":"ThisoeCode","description":"Spring Boot first-try project \u0026 learning notes. Following Devtiro.","archived":false,"fork":false,"pushed_at":"2024-02-17T15:22:25.000Z","size":88,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-22T13:51:39.326Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/ThisoeCode.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-02-11T10:29:20.000Z","updated_at":"2024-02-12T11:31:39.000Z","dependencies_parsed_at":"2025-01-22T13:51:20.920Z","dependency_job_id":"a52dd51a-cbc7-42ab-8ab8-59a4fafbf564","html_url":"https://github.com/ThisoeCode/springboot-studynote_following-devtiro","commit_stats":null,"previous_names":["thisoecode/springboot-studynote_following-devtiro"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ThisoeCode%2Fspringboot-studynote_following-devtiro","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ThisoeCode%2Fspringboot-studynote_following-devtiro/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ThisoeCode%2Fspringboot-studynote_following-devtiro/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ThisoeCode%2Fspringboot-studynote_following-devtiro/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ThisoeCode","download_url":"https://codeload.github.com/ThisoeCode/springboot-studynote_following-devtiro/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243809886,"owners_count":20351407,"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":[],"created_at":"2024-11-21T22:46:17.698Z","updated_at":"2025-03-16T01:09:31.996Z","avatar_url":"https://github.com/ThisoeCode.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# About this Repo\n\nMost of the content is me following [Devtiro](https://www.devtiro.com/)'s [Spring Boot Tutorial](https://youtu.be/Nv2DERaMx-4) on YouTube.\n\n[Devtiro's Repo of this course](https://github.com/devtiro/course-spring-boot)\n\nCodes are written on Windows IntelliJ.\n\n\u003e Start learning on 2024/02/11.\n\n\n# Learning Note\n\n## 1. Hello world quickstart proj\n\n- Dependency *\"Spring Web\"* allow us to build a REST-API web application.\n- Notation `@SpringBootApplication` identifies a Spring Boot application. It is an entry of our app that we can then run.\n- `Test` directory: Needs the notation `@SpringBootTest`. On run, it starts up a testing version of the application. \u003cbr\u003e The (test class) context loads is a [dependency injection](#Dependency_injection) concept. \u003cbr\u003e It's making sure the app starts up like is in production or any other environments.\n- `pom` file is used by Maven.\n\n\n## 2. Maven\n\u003e ***Apache Mavan** helps programmers manage their projects and all the things they need ot build their programs.*\n\n- What Maven do:\n  - Manage dependencies;\n  - Build and test the application;\n  - Build and package the app up;\n  - Share the app as a dependency (only if you want).\n\u003cbr\u003e\n- Maven is a command line tool.\n```bat\n./mvnw clean compile\n```\nLike this, the second part is called *phases*.\n\n### About phases\n```yaml\n./mvnw:\n  clean: Removed temporary directories and files.\n  default: Where the most useful goals live.\n  site: Where documentation is generated.\n```\nAbout `./mvnw clean`:\n```yaml\n./mvnw clean:\n  pre-clean: Hook for before cleaning.\n  clean: Does the actual cleaning.\n  post-clean: Hook for after cleaning.\n```\n- Maven puts all its stuff in `target/` directory.\n- `./mvnw clean` deletes files in `target/`.\n\nAbout `./mvnw [default]`:\n```yaml\n./mvnw [default]:\n  compile: Compiles code into bytecode.\n  test: Runs unit tests.\n  package: Creates a \".jar\" or \".war\" file.\n  verify: Runs checks \u0026 intergration tests.\n```\nIt runs things in order. (If you run `./mvnw package`, it will compile, **then** test, **then** package.)\n\n### Maven project structure\n[Maven Official Docs for Standard Directory Layout](https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html)\n```\nmy-project/\n├── src/\n│   ├── main/\n│   │   ├── java/\n│   │   │   └── com.name.proj/\n│   │   │       └── app/\n│   │   │           └── MyApp.java\n│   │   └── resources/\n│   │       └── application.properties\n│   └── test/\n│       ├── java/\n│       │   └── com.name.proj/\n│       │       └── app/\n│       │           └── MyAppTest.java\n│       └── resources/\n└── pom.xml\n```\n- `MyApp.java` is the main Java source file.\n- `MyAppTest.java` is the test source file.\n- `application.properties` is the proj config resource file used by the main application.\n- `pom.xml` is the Maven project configuration file.\n\nThis structure is ***required*** in Maven.\n\n### Workflow with Maven\n\u003e This is Devtiro's personal preferred workflow.\n\u003e - Using terminal command rather than with the IDE;\n\u003e - Not following the [TDD](https://github.com/ThisoeCode/springacademy_CashCard-REST-API?tab=readme-ov-file#3-testing-first) (Test Driven Development) as a general rule.\n\n#### 1. Code\n\nIn terms of the project: \nCheck the todo-list,\nlook at requirements,\nand start coding.\n\n#### 2. Get feedback\nWhile writing, when want to check if anything may go wrong or see any compilation error:\n```bat\n./mvnw compile\n```\nThe terminal will give a fast feedback.\n\n#### 3. Running test\nWhen done with a feature: \n```bat\n./mvnw clean test\n```\nSuccess message: \u003cbr\u003e `[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0`\n\n#### 4. Before pushing\nDone with the feature and the tests, before committing \u0026 pushing, run: \u003cbr\u003e\n```bat\n./mvnw clean package\n```\nor\n```bat\n./mvnw clean verify\n```\nto get the `.jar` file. \u003cbr\u003e Then run it as if it's the released ver:\n```bat\ncd target\nls\njava -jar [***.jar]\n```\nGo to `localhost:8080` to check if is up in running.\n\n#### 5. DOUBLE CHECK\nRun for the last time:\n```bat\n./mvnw clean test\n```\nIf everything in the terminal works fine, commit + push!\n\n### Maven Spring Boot Plugin\nThis is something like `npm run dev` in Next.js:\n```bat\n./mvnw spring-boot:run\n```\nAnd the app will be running on `:8080`. \u003cbr\u003e\nTo stop, `Ctrl`+`C` (+`y`).\n\n\n## 3. Spring Boot\n\n### Layers\n#### 1. **Persistence** layer\n  Presentation layer is there to handle interactions with databases.\n- Entities \u003cbr\u003e\n  (E.g. Map through tables in database)\n- Repositories pattern / DAOs (Data Access Objects)\n- The basic functionalities: CRUD\n  - Create\n  - Read\n  - Update\n  - Delete\n\n#### 2. **Service** layer\n- Logics \u003cbr\u003e\n\u003e Even though the logic might just be a simple pass-through, Presentation layer must **ALWAYS** go through Service layer to reach the Persistence layer.\n\n#### 3. **Presentation** layer\n- REST APIs (Controllers as an implementation)\n- and any other types of APIs (GraphQL, Web sockets API...)\n\u003e Benefit: We can easy change APIs using this layer, without touching the Service layer.\n\n### Modularity (Dependencies)\nUse Spring's ecosystem of dependencies, or, pre-can solutions, to build the [three layers](#layers).\n\n\n## 4. Dependency Injection\n\n### Beans\n`Beans` are the concrete classes in the Spring Framework / Spring Boot.\n\n- Inside `@Configuration` class, use `@Bean` annotation to declare a Bean.\n\n\u003e \u003e #### _Steps did in [`colorprint`](https://github.com/ThisoeCode/springboot-studynote_following-devtiro/tree/colorprint) branch_\n\u003e 1. Add a `config/` package under root folder (`com.thisoe.xxx/`).\n\u003e 2. Create a class, label the class using `@Configuration`.\n\u003e 3. Define Beans in the class with `@Bean` annotation.\n\n\n- Use `@Component` or `@Service` annotation to declare a Bean.\n\n\u003e \u003e #### _Steps did in [`component-bean`](https://github.com/ThisoeCode/springboot-studynote_following-devtiro/tree/component-bean) branch:_\n\u003e ```java\n\u003e @Component // or @Server\n\u003e public class ColorPrinterImpl implements ColorPrinter {\n\u003e }\n\u003e ```\n\u003e\n\u003e 1. Add `@Component` annotation to the class.\u003cbr\u003eThis is telling Spring that `ColorPrinter` is a Bean and is an implementation of `ColorPrinterImpl`.\n\u003e\n\u003e 2. If we run the app at this moment, we get an error `Consider defining a bean of type 'RedPrinter' in your configuration.`\u003cbr\u003eSo we should add `@Component` in front of `EnglishRedPrinter` class, Etc.\n\u003e\n\u003e 3. Switch lang of each color by annotating `@Component` to the wanted classes.\n\n### Component scanning\nComponent scanning happens when the app starts up.\n\nIn this process, Spring Boot will:\n1. Look for Beans, and where the Beans are needed.\n2. Do these Beans need any dependencies?\n3. Create an instance of the class, and place it (as a Bean) into the application context.\n\nIn Spring-specific term, this process is also called \"**Auto-wiring**\".\n\n\u003e The `@ComponentScan` (in `@SpringBootApplication`) is the one who starts the component scanning process.\n\u003e \u003cbr\u003eIt will look for all Beans like `@Service` in the annotated package and below.\n\n\n### The `@SpringBootApplication` annotation\n1. `@Configuration`\n2. `@ComponentScan`\n3. `@EnableAutoConfiguration`\n\n\u003e Watch [from 54:18 to 01:05:55](https://youtu.be/Nv2DERaMx-4?t=3258) for how Spring Web dependency work on the Beans scanning.\n\n\n## 5. Config file `application.properties`\n### Common Application Properties\nOfficial docs: [Common Application Properties](https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html)\n\nLooking into the docs, search `server.port`. This is where we can set the port where the app server runs on.\n\nIn `application.properties`:\n```properties\nserver.port=8181\n```\nIn `application.yml`:\n```yaml\nserver:\n  port: 8282\n```\n\n### Config file in `test/`\nSpring will pick the `appplication.x` config file under `test/` instead of the one in `main/` when running test. \u003cbr\u003e\nThis is convenient to connect to the real database or simulate production environment.\n\n\n### `.env` (Environment Variables)\nTo use Application Properties in `.env`, change all the dots or hyphens into underscores, and capitalize all letters.\n```properties\nSERVER_PORT=8383\n```\n\n(In IntelliJ, add env keys at the top bar \u003e `Run / Debug Configurations` \u003e `Edit Configurations...` \u003e `Environment variables`)\n\n\u003e \u003e #### _Tried on branch `helloworld`_\n\u003e When using command line (`./mvnw`) to start the app, put env vars in front:\n\u003e \n\u003e - On Mac / Linux:\n\u003e ```bash\n\u003e SERVER_PORT=23333 ./mvnw spring-boot:run\n\u003e ```\n\u003e - On Windows:\n\u003e ```bash\n\u003e $env:SERVER_PORT=23333 ; ./mvnw spring-boot:runspring-boot:run\n\u003e ```\n\nBefore packaging jar, put env vars by using `export` command. **_(This only works on Mac / Linux!)_**\n\n\n### Configuration Properties\n\n# Todo\n- See tut [1:16:02](https://youtu.be/Nv2DERaMx-4?t=4562).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthisoecode%2Fspringboot-studynote_following-devtiro","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthisoecode%2Fspringboot-studynote_following-devtiro","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthisoecode%2Fspringboot-studynote_following-devtiro/lists"}