Skip to content

First Spring Framework

🏷️ Spring

1. 安装 JDK

2. 安装 Maven 下载 ​ 安装 运行

bash
mvn -v

3. 创建目录结构

选择一个目录作为项目的根目录,并创建如下目录结构

bash
mkdir [项目根目录全路径]\src\main\java\hello

4. src/main/java/hello/HelloWorld.java

java
package hello;

public class HelloWorld {
    public static void main(String args) {
        Greeter greeter = new Greeter();
        System.out.println(greeter.sayHello());
    }
}

5. src/main/java/hello/Greeter.java

java
package hello;

public class Greeter {
    public String sayHello() {
        return "Hello world!";
    }
}

6. 创建 pom.xml 文件

在项目根目录创建 pom.xml 文件

xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.springframework</groupId>
    <artifactId>gs-maven</artifactId>
    <packaging>jar</packaging>
    <version>0.1.0</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>hello.HelloWorld</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

7. 编译 java 代码

生成的 class 文件在 \target\classes 目录

bash
mvn compile

8. 生成 jar 包

文件在 target 目录

bash
mvn package

9. 安装到 Maven 仓库

如果别的项目需要依赖该工程,可以安装到 Maven 的仓库。

bash
mvn install

10. 添加 springframework 的依赖

pom.xml 文件中添加 springframework 的依赖

xml
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.3.RELEASE</version>
    </dependency>
</dependencies>

11. hello/MessageService.java

java
package hello;

public interface MessageService {
        String getMessage();
}

12. hello/MessagePrinter.java

java
package hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MessagePrinter {

    final private MessageService service;

    @Autowired
    public MessagePrinter(MessageService service) {
        this.service = service;
    }

    public void printMessage() {
        System.out.println(this.service.getMessage());
    }
}

13. hello/Application.java

java
package hello;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;

@Configuration
@ComponentScan
public class Application {

    @Bean
    MessageService mockMessageService() {
        return new MessageService() {
            public String getMessage() {
                return "Hello World!";
            }
        };
    }

    public static void main(String args) {
        ApplicationContext context = 
                new AnnotationConfigApplicationContext(Application.class);
        MessagePrinter printer = context.getBean(MessagePrinter.class);
        printer.printMessage();
    }
}

14. 执行 mvn package 命令生成 jar 包

15. 执行 jar 包

bash
java -jar target/gs-maven-0.1.0.jar

参考

  1. Building Java Projects with Maven

  2. Spring Framework Quick Start