Spring基础知识(2)- 创建 Spring 程序
本文将在 Windows 下使用 IntelliJ IDEA 和 Apache Maven 创建一个简单的 Spring 程序。在开始之前,确保已经正确搭建了 Spring 开发环境,如何搭建 Spring 开发环境见:Spring基础知识(1)- Spring简介、Spring体系结构和开发环境配置
Windows版本 : Windows 10 Home (20H2)
IntelliJ IDEA:Community Edition for Windows 2020.1.4
Apache Maven:3.8.1
1. 在 IDEA 上创建 Maven Quickstart 项目
1) 运行 IDEA
点击菜单 New 创建 Project:
New Project -> Project Type: Maven -> Project SDK: 1.8 -> Check "Create from archtype" -> select "org.apache.maven.archtypes:maven-archtype-quickstart" -> Next
Name: SpringBasic
GroupId: com.example
ArtifactId: SpringBasic
-> Finish
2) 生成的项目目录结构和文件
|-- src
| |-- main
| | |-- java
| | |-- com
| | |-- example
| | |-- App.java
| |-- test
| |-- java
| |-- com
| |-- example
| |-- AppTest.java
|-- pom.xml
3) App.java 代码
1 package com.example; 2 3 public class App { 4 public static void main( String[] args ) { 5 System.out.println( "Hello World!" ); 6 } 7 }
4) AppTest.java 代码
1 package com.example; 2 3 import static org.junit.Assert.assertTrue; 4 5 import org.junit.Test; 6 7 public class AppTest { 8 9 @Test 10 public void shouldAnswerWithTrue() { 11 assertTrue( true ); 12 } 13 }
5) pom.xml 代码
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.example</groupId> 8 <artifactId>SpringBasic</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <name>SpringBasic</name> 12 <!-- FIXME change it to the project's website --> 13 <url>http://www.example.com</url> 14 15 <properties> 16 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 17 <maven.compiler.source>1.7</maven.compiler.source> 18 <maven.compiler.target>1.7</maven.compiler.target> 19 </properties> 20 21 <dependencies> 22 <dependency> 23 <groupId>junit</groupId> 24 <artifactId>junit</artifactId> 25 <version>4.11</version> 26 <scope>test</scope> 27 </dependency> 28 </dependencies> 29 30 <build> 31 <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> 32 <plugins> 33 <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle --> 34 <plugin> 35 <artifactId>maven-clean-plugin</artifactId> 36 <version>3.1.0</version> 37 </plugin> 38 <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging --> 39 <plugin> 40 <artifactId>maven-resources-plugin</artifactId> 41 <version>3.0.2</version> 42 </plugin> 43 <plugin> 44 <artifactId>maven-compiler-plugin</artifactId> 45 <version>3.8.0</version> 46 </plugin> 47 <plugin> 48 <artifactId>maven-surefire-plugin</artifactId> 49 <version>2.22.1</version> 50 </plugin> 51 <plugin> 52 <artifactId>maven-jar-plugin</artifactId> 53 <version>3.0.2</version> 54 </plugin> 55 <plugin> 56 <artifactId>maven-install-plugin</artifactId> 57 <version>2.5.2</version> 58 </plugin> 59 <plugin> 60 <artifactId>maven-deploy-plugin</artifactId> 61 <version>2.8.2</version> 62 </plugin> 63 <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle --> 64 <plugin> 65 <artifactId>maven-site-plugin</artifactId> 66 <version>3.7.1</version> 67 </plugin> 68 <plugin> 69 <artifactId>maven-project-info-reports-plugin</artifactId> 70 <version>3.0.0</version> 71 </plugin> 72 </plugins> 73 </pluginManagement> 74 </build> 75 </project>
2. 编译运行
1) 可以选择 Java SDK 版本
在IDE项目列表 -> External Libraries -> <1.8>, 在 "<1.8>" 上点击鼠标右键 跳出菜单,选择 Open Library Settings 菜单项,跳出对话框。
在对话框上 Platform Settings -> SDKS 选择或添加 <1.8>
Name: 1.8
JDK home path: select 1.8 SDK or download a SDK
2) Run App.main()
Open App.java, click mouse right button, select Run "App.main()"
3) Edit Configurations
Click "+" add new configuration -> Select "Application"
Name: SpringBasic
Main class: com.example.App
-> Apply / OK
Click Run "SpringBasic"
Hello World!
3. 导入 Spring, log4j, slf4j 依赖包
访问 http://www.mvnrepository.com/,查询 Spring, log4j, slf4j
本文选择了 Spring 4.3.9.RELEASE、log4j 1.2.17、slf4j 1.7.36, log4j 和 slf4j 用于把 Spring 输出的 log 信息保存到 log 文件。
修改 pom.xml,添加 Spring 依赖:
<project ... >
...
<dependencies>
...
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.36</version>
<type>pom</type>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
...
</dependencies>
...
</project>
在IDE中项目列表 -> 点击鼠标右键 -> Maven -> Reload Project
4. 创建/修改 Java 类
在 src/main/java/com/example 目录下,内容如下:
1 package com.example; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 public class App { 7 public static void main(String[] args) { 8 9 // System.out.println( "Hello World!" ); 10 11 ApplicationContext context1 = new ClassPathXmlApplicationContext("spring-beans.xml"); 12 Test01 obj1 = context1.getBean("Test01", Test01.class);
13 obj1.getMessage(); 14 } 15 } 16 17 class Test01 {
18 private String message; 19 20 public void setMessage(String message) { 21 this.message = message; 22 } 23 24 public void getMessage() { 25 System.out.println("Message: " + message); 26 } 27 }
5. 创建 Spring 配置文件 spring-beans.xml
在 src/main/resources 目录下,创建 spring-beans.xml (通常命名为 applicationContext.xml,不强制要求),内容如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-4.0.xsd" > 9 10 <bean id="Test01" class="com.example.Test01"> 11 <property name="message" value="Test01 Hello World!" /> 12 </bean> 13 14 </beans>
*注:src/main/resources 目录不存在,先创建这个目录,再添加 spring-beam.xml (下文如遇到目录不存在,自行创建)
6. 创建 log4j.properties 文件
在 src/main/resources 目录下,创建 log4j.properties,内容如下:
# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.FILE.File=d:\\temp\\spirng-basic_log4j.txt
# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug
# Set the append to false, overwrite
log4j.appender.FILE.Append=false
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
7. 运行
参考 "2. 编译运行"
运行 App.java,IDEA 控制台中显示信息如下:
Message: Test01 Hello World!