https://github.com/rominalodolo/springinaction6
Spring Frame Class Work
https://github.com/rominalodolo/springinaction6
class configuration database java jdbctemplate spring spring-mvc springtoolsuite sts4 thymeleaf
Last synced: 4 months ago
JSON representation
Spring Frame Class Work
- Host: GitHub
- URL: https://github.com/rominalodolo/springinaction6
- Owner: rominalodolo
- Created: 2022-05-16T07:38:16.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-05-25T21:14:17.000Z (over 3 years ago)
- Last Synced: 2025-01-12T05:10:45.194Z (9 months ago)
- Topics: class, configuration, database, java, jdbctemplate, spring, spring-mvc, springtoolsuite, sts4, thymeleaf
- Language: Java
- Homepage:
- Size: 20.3 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Spring In Action 6th Edition
Spring Frame Class Work by Craig Walls- [Download](https://spring.io)
- [Spring Initializr](https://start.spring.io)> "Spring offers a container, reffered to as the Spring Application context. It creates and manages application components."
>
> - Data persistance options
> - Microserve support
> - Reactive programming model
> - Runtime monitoring
> - Security framework[MVC](https://developer.mozilla.org/en-US/docs/Glossary/MVC)
(Model-View-Controller) is a pattern in software design commonly used to implement user interfaces, data, and controlling logic. It emphasizes a separation between the software's business logic and display. This "separation of concerns" provides for a better division of labor and improved maintenance. Some other design patterns are based on MVC, such as MVVM (Model-View-Viewmodel), MVP (Model-View-Presenter), and MVW (Model-View-Whatever).1. Model: Manages data and business logic.
2. View: Handles layout and display.
3. Controller: Routes commands to the model and view parts.
[Annotation note reference](https://springframework.guru/spring-framework-annotations/)
@Configuration
This annotation is used on classes which define beans. `@Configuration` is an analog for XML configuration file – it is configuration using Java class. Java class annotated with `@Configuration` is a configuration by itself and will have methods to instantiate and configure the dependencies.
@Bean
This annotation is used at the method level. `@Bean` annotation works with `@Configuration` to create Spring beans. As mentioned earlier, `@Configuration` will have methods to instantiate and configure dependencies. Such methods will be annotated with `@Bean`. The method annotated with this annotation works as bean ID and it creates and returns the actual bean.
Spring Initializer:
How to make a New Project:

Create New Project:

Click Next and wizard takes you to setting up dependencies:
Then search for the missing dependencies eg. Spring Web and add it:

How the project looks once you click Finish and it has finished loading:

More about the files:

Open the Taco Test File to view this code:

Running the Maven Test File:


Create a new class in the tacos package:

Adding the code to the home controller class:

[Annotation note reference](https://springframework.guru/spring-framework-annotations/)
@ComponentThis annotation is used on classes to indicate a Spring component. The `@Component` annotation marks the Java class as a bean or say component so that the component-scanning mechanism of Spring can add into the application context.
@Service
This annotation is used on a class. The `@Service` marks a Java class that performs some service, such as execute business logic, perform calculations and call external APIs. This annotation is a specialized form of the `@Component` annotation intended to be used in the service layer.
@Repository
This annotation is used on Java classes which directly access the database. The `@Repository` annotation works as marker for any class that fulfills the role of repository or Data Access Object.
This annotation has a automatic translation feature. For example, when an exception occurs in the `@Repository` there is a handler for that exception and there is no need to add a try catch block.
@GetMapping
This annotation is used for mapping HTTP GET requests onto specific handler methods. `@GetMapping` is a composed annotation that acts as a shortcut for `@RequestMapping`(method = RequestMethod.GET)
Create a new HTML file in the template folder under src:
Add your taco image and images folder to the static folder:

Create a Test File:

Run the test in IDE:
`$ mvnw test`Run Spring Boot App:

Open your borswer and search localhost:8080

[How do I install and use the browser extensions?](http://livereload.com/extensions/)
Download & open to install:- [Safari extension 2.1.0](http://download.livereload.com/2.1.0/LiveReload-2.1.0.safariextz) — note: due to Safari API limitations, browser extension does not work with file: URLs; if you’re working with local files via file: URL, please use Chrome or insert the SCRIPT snippet.
- [Chrome extension on the Chrome Web Store](https://chrome.google.com/webstore/detail/livereload/jnihajbhpnppcggbcgedagnkighmdlei) — if you want to use it with local files, be sure to enable “Allow access to file URLs” checkbox in Tools > Extensions > LiveReload after installation.
- [Firefox extension 2.1.0](https://addons.mozilla.org/en-US/firefox/addon/livereload/) from addons.mozilla.org.
> Insight into the data your application is working with:
> http://localhost:8080/h2-console### Spring Dependencies:
## The Core Spring Framework
## Spring Boot
## Spring Data
## [Spring Security](https://www.baeldung.com/spring-security-with-maven)
### spring-security-core
`
5.3.4.RELEASE
5.2.8.RELEASEorg.springframework.security
spring-security-core
${spring-security.version}
`
### spring-security-web
`
org.springframework.security
spring-security-web
${spring-security.version}
`### spring-security-config
`
org.springframework.security
spring-security-config
${spring-security.version}
`## Spring Integration and Spring Batch
## Spring CLoud
## Spring Native
Instead of starting from scratch, import chapter 1 code as a starter for chapter 2:

Please note that doing it this way will sow down your machince and will take a while to load. Rather start a new file or build on the pervious version.
## Lombok
[Lombok tutorial](https://projectlombok.org/#)
Add -> Spring -> Add Starters -> SELECT POM file

Watch this [tuotorial](https://www.youtube.com/watch?v=Xx3urDhrN_M) if you aren't sure how to save your jar file that you have downloaded from the [Project Lombok](https://projectlombok.org/download) web page.



## Simple Logging Facade for Java [SLF4J](https://www.slf4j.org)
Serves as a simple facade or abstraction for various logging frameworks (e.g. java.util.logging, logback, log4j) allowing the end user to plug in the desired logging framework at deployment time.
[READ THE MANUAL](https://www.slf4j.org/manual.html)

## Designing the view
- JavaServer Pages (JSP)
- Thymeleaf`
placeholder message
`:Thymeleaf templates are just HTML with some additional element attributes that guide a template in rendering request data.`${}`: operator tells it to use the value of a request attribute ("message", in this case).
`th:each`:iterates over a collection of elements, rendering the HTML once for each item in the collection.
- FreeMarker
- Mustache
- Groovy-based templates## `localhost:8080/design` in your browser to view
The Taco OrderForm.html File

Spring supports the JavaBean Validation API [JSR 303](https://jcp.org/en/jsr/detail?id=303)
## Adding a Valedation dependency to your pom.xml file:
Right click on your pom file and say Spring -> Add Starters
Then you will see this pop up

Select pom only:

This is the new code you will have in your file:

## The Luhn Algorithm for Credit Card Validation
`@CreditCardNumber`
`ccNumber` property
[Luhn algorithm check](https://www.creditcardvalidator.org/articles/luhn-algorithm)#### How to calculate a Luhn checksum:
> 1. From the rightmost digit (the check digit), move left and double the value of every second digit; if doubled number is greater than 9 (e.g., 7 × > 2 = 14), then subtract 9 from the product (e.g., 14: 14 - 9 = 5).
> 2. Sum of all the digits in the newly calculated number.
> 3. Multiply the sum by 9, the Luhn check digit is the rightmost digit of the result (e.g, the result modulo 10).In the following example, we use a sample credit card number "7992739871", with an unknown Luhn check digit at the end, displayed as 7992739871x:
The sum of all the digits in the third row above is 67+x.
We still need to calculate the check digit, X. The check digit can be obtained by computing the sum of the non-check digits then computing 9 times that value modulo 10. For our example, the equation is 67 × 9 mod 10. Broken down in more detail:
1. Compute the sum of the non-check digits (67).
2. Multiply by 9 (603).
3. The units digit (3) is the check digit. Thus, x=3.## Domain Driven Design (DDD)
Core concepts of DDD: Aggregates and aggregate roots - a design approach that promotes the idea that the structure and language of software code should match the business domain.
Read for more info on the topic [Domain-Driven Design: Tackling Complexity in the Heart of Software](https://www.dddcommunity.org/book/evans_2003/)
## Selenium jar
Download your selenium jar file then follow these steps..in your menu select build path

Go into your library and choose add extrnal jar and select your jar file.


Watch this video if you are struggling with your selenium dependency: [Tutorial](https://www.youtube.com/watch?v=beWEdwfjF28)
What the new login form looks like after Chapter 5
## Configuration
> *Fine-tuning configuration*
> - Bean wiring: Configuration that declares application components to be created
as beans in the Spring application context and how they should be injected into
each other
> - Property injection: Configuration that sets values on beans in the Spring application
context
## YAML
_Known as ain’t markup language OR yet another markup language_
[More Info Here](https://www.redhat.com/en/topics/automation/what-is-yaml)> - Features that come from Perl, C, XML, HTML, and other programming languages. YAML is also a superset of JSON, so JSON files are valid in YAML.
> - Python-style indentation to indicate nesting.
> - Tab characters are not allowed, so whitespaces are used instead. There are no usual format symbols, such as braces, square brackets, closing tags, or quotation marks.
> - YAML files use a .yml or .yaml extension.
> - The structure of a YAML file is a map or a list.
> - Contains scalars, which are arbitrary data (encoded in Unicode) that can be used as values such as strings, integers, dates, numbers, or booleans.More YAML examples and usage:
[1](https://stackabuse.com/reading-and-writing-yaml-files-in-java-with-jackson/)
[2](https://www.cloudbees.com/blog/yaml-tutorial-everything-you-need-get-started)
[3](https://phoenixnap.com/blog/what-is-yaml-with-examples)## REST services
HTTPie is making APIs simple and intuitive for those building the tools of our time.
[httpie](https://httpie.io/)Testing the api
CommandLine test in Terminal

RESTful Controllers:

[Read more](https://livebook.manning.com/book/play-for-java/chapter-5/)

## Testing the Taco Application:
`localhost:8080/tacos/apis/4`
The 404 page not found code
`localhost:8080/tacos/apis/3`
`localhost:8080/ingredients`

`$ curl http://localhost:8080/ingredients/FLTO`

Adding
```
spring:
data:
rest:
base-path: /data-api
```
to the application.yml file`$ curl http://localhost:8080/data-api/tacos`
`$ curl http://localhost:8080/data-api/tacoes`
`localhost:8080/data-api`

Paging and Sorting:
requesting the first page of tacos where the page size is 5, you can issue the following GET request (using curl):`curl "localhost:8080/data-api/tacos?size=5"`

Links working:
Firefox Browser

Chrome Browser - Add the JSON viewer extension

[JSONview Download](https://chrome.google.com/webstore/detail/jsonview/gmegofmjomhknnokphhckolhcffdaihd/related?gclid=Cj0KCQjw-JyUBhCuARIsANUqQ_KF--q5ygsDSzxbA2q6afK3NhXaWIbQISzJnVAduQkG_5h1TwaaYSoaAhMJEALw_wcB)
## [HATEOAS](https://restfulapi.net/hateoas/)
_Hypermedia as the Engine of Application State_## Consuming REST services
> _A Spring application can consume a REST API with the following:_
> - RestTemplate—A straightforward, synchronous REST client provided by the core Spring Framework.
> - Traverson—A wrapper around Spring’s RestTemplate, provided by Spring HATEOAS, to enable a hyperlink-aware, synchronous REST client. Inspired from a JavaScript library of the same name.
> - WebClient—A reactive, asynchronous REST client.
LAST UPDATE: May 2022