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.
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
It says "Create a project with Maven" at once, but I haven't even installed Maven, so first install Apache Maven.
Download the latest version of Apache Maven from Apache Maven. Since it is a Mac, download the binary in tar.gz format. Extract the downloaded compressed file to an appropriate directory.
Added ʻexport PATH = $ PATH: (DirectoryName) / maven / bin to
.bash_profile`.
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"
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 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.
Download the latest version of the JDK from the Java SE Development Kit (Oracle account required). Mount the downloaded dmg and run the package. Follow the wizard to complete the installation.
Start STS4 and select [File]-[Import]
from the menu.
Select Maven --Existing Maven Projects
and click Next.
When you open the created MySpringApp, pom.xml will be displayed in Projects
, so make sure it is selected and complete.
Create and execute in order. There was nothing I didn't understand or stumbled upon.
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`.
When creating an AOP configuration class with annotations, add ʻimport org.springframework.context.annotation.AnnotationConfigApplicationContext;` because there is not enough import in App.java.
When using Bean configuration class, add ʻimport org.springframework.context.annotation.AnnotationConfigApplicationContext;` because there is not enough import in App.java.
When using the Bean configuration class, add ʻimport org.springframework.context.annotation.AnnotationConfigApplicationContext;` because there is not enough import in App.java.
Create and execute in order. There was nothing I didn't understand or stumbled upon.
Create and execute in order. There was nothing I didn't understand or stumbled upon.
Recommended Posts