[JAVA] A record of studying the Spring Framework from scratch

Overview

Study record about Spring Framework. Basically, while watching Introduction to Spring Framework for beginners, we will build the environment-create a simple project. In this article, I will focus on what I do not understand or stumbled upon while reading the above site.

environment

I think it's the latest version as of May 15, 2019.

--macOS Mojave version 10.14.4 --Spring Framework 4.1.7.RELEASE (*** Not the latest **) --Spring Tool Suite 4 version 4.2.1

1. Create a Spring project

It says "Create a project with Maven" at once, but I haven't even installed Maven, so first install Apache Maven.

Install Apache Maven

Download Apache Maven

Download the latest version of Apache Maven from Apache Maven. Since it is a Mac, download the binary in tar.gz format. スクリーンショット 2019-05-16 9.58.00.png Extract the downloaded compressed file to an appropriate directory.

System path setting

Added ʻexport PATH = $ PATH: (DirectoryName) / maven / bin to .bash_profile`.

Version confirmation

Execute the following command. If the version is displayed, the installation is completed normally.

$ mvn -version
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-05T04:00:29+09:00)
Maven home: (DirectoryName)/maven
Java version: 1.8.0_211, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre
Default locale: ja_JP, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.4", arch: "x86_64", family: "mac"

Spring Framework installation

After installing Apache Maven, install Spring Framework with the mvn command.

Where to add Spring core library to pom.xml

In addition, since the contents are narrowed down to the minimum necessary this time, the JUnit library has been removed. For that reason, delete the folders inside the "src" folder. (Since there is no JUnit, the unit test source code file cannot be built, so leaving it will result in a build error.)

It says, but if you delete it, you will not be able to check the operation later, so do not delete it. Also leave the description of pom.xml.

Where Spring core library can be installed and operation is confirmed

Now download the required libraries etc., build the project and create the Jar file. A "target" folder will be created, so let's take a look inside it. Then, the file "** MySpringApp-0.0.1-SNAPSHOT.jar **" should be created. This is the Jar file that was built.

Now let's do this. Execute "cd target" from the command line to move to the "target" folder, and execute as follows.

java -classpath .;MySpringApp-0.0.1-SNAPSHOT.jar com.tuyano.libro.App

Run com.tuyano.libro.App with ** MySpringApp-0.0.1-SNAPSHOT.jar ** in the classpath. This will execute the main method of the App class and display the text "Hello World!".

Is written, but the jar file name is incorrect. The correct jar file name is MySpringApp-1.0-SNAPSHOT.jar. This jar file name can be specified when installing the Spring Framework. The .; Before the command jar file name was also unnecessary in my environment (adding it would result in a command error). Also, if you delete the JUnit description and src folder when you add the Spring core library to pom.xml, you cannot check this operation.

Import project into STS4

Import the project created by the mvn command into STS4. When you start STS4, an error message indicating that there is no JDK is displayed, so install the JDK.

JDK installation

Download the latest version of the JDK from the Java SE Development Kit (Oracle account required). スクリーンショット 2019-05-16 10.34.53.png Mount the downloaded dmg and run the package. Follow the wizard to complete the installation.

Start STS4 and select [File]-[Import] from the menu. スクリーンショット 2019-05-16 10.42.51.png Select Maven --Existing Maven Projects and click Next. スクリーンショット 2019-05-16 10.46.36.png When you open the created MySpringApp, pom.xml will be displayed in Projects, so make sure it is selected and complete.

2. Dependency Injection (DI) and Bean

Create and execute in order. There was nothing I didn't understand or stumbled upon.

3. Implementation of DI by annotation

When creating a component

This completes the component, but there's actually one more thing to do. Please add the following annotation before the declaration of Bean configuration class (SampleBeanConfig).

@ComponentScan

It's already annotated with @Configuration, so you can add it above or below it.

It says, but just adding @ComponentScan will result in an error. Add ʻimport org.springframework.context.annotation.ComponentScan;in addition to@ComponentScan`.

4. Use of AOP

When creating an AOP configuration class with annotations, add ʻimport org.springframework.context.annotation.AnnotationConfigApplicationContext;` because there is not enough import in App.java.

5. Use AspectJ

When using Bean configuration class, add ʻimport org.springframework.context.annotation.AnnotationConfigApplicationContext;` because there is not enough import in App.java.

6. Using Spring Data JPA

When using the Bean configuration class, add ʻimport org.springframework.context.annotation.AnnotationConfigApplicationContext;` because there is not enough import in App.java.

7. Master the basics of CRUD

Create and execute in order. There was nothing I didn't understand or stumbled upon.

8. Use Jpa Repository

Create and execute in order. There was nothing I didn't understand or stumbled upon.

Recommended Posts

A record of studying the Spring Framework from scratch
A review note of the Spring Framework Resource interface
About the initial display of Spring Framework
Find the difference from a multiple of 10
About the official start guide of Spring Framework
1. Start Spring framework from 1
Extract a specific element from the list of objects
A survey of the Kubernetes native Java framework Quarkus
The story of raising Spring Boot from 1.5 series to 2.1 series part2
A story packed with the basics of Spring Boot (solved)
Get a proxy instance of the component itself in Spring Boot
A memorandum of the FizzBuzz problem
Filter the result of BindingResult [Spring]
We will build a Spring Framework development environment in the on-premises environment.
Create a Tokyo subway map from the CSV file of station data.jp
Personal memo Features of Spring Boot (mainly from a DI point of view)
How to get the setting value (property value) from the database in Spring Framework
Change the half-width space of STS (Spring Tool Suite) from "u" to "・"
The story of encountering Spring custom annotation
Understand the basics of Android Audio Record
Why spring consider as a lightweight framework
The story of RxJava suffering from NoSuchElementException
Ruby from the perspective of other languages
Features of spring framework for java developers
[Java] [Spring] Test the behavior of the logger
Output the sum of each name and its contents from a multiple array
Introducing the Spring Boot Actuator, a function that makes the operation of Spring Boot applications easier.
Get the path defined in Controller class of Spring boot as a list
Has the content of useBodyEncodingForURI changed from Tomcat8?
Make a margin to the left of the TextField
Try calling the CORBA service from Spring (Java)
Set the time of LocalDateTime to a specific time
A fledgling engineer learned JUnit from the basics
The story of raising Spring Boot 1.5 series to 2.1 series
Let's check the feel of Spring Boot + Swagger 2.0
Various correspondence table of Spring Framework and Spring Boot
Transaction management of the integrated framework "Apache Camel"
The official name of Spring MVC is Spring Web MVC
[Comparison verification] How different is the development productivity of Spring Boot apps from the past?