SpringBoot开始

Maven搭建SpringBoot项目

#maven创建SpringBoot需要导入

<!-- 指定Spring Boot的版本 2.0.4.RELEASE  1.5.9-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
</parent>

<dependencies>
   <!-- 导入Spirng Boot  web 所需的jar包 -->
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
     </dependency>

    <!-- 导入Spirng Boot 的单元测试 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
 </dependencies>

    <!-- 可以将应用打成一个可执行的jar包 -->

    <build>
        <plugins>
            <plugin>
                <groupId> org.springframework.boot </groupId>
                <artifactId> spring-boot-maven-plugin </artifactId>
            </plugin>
        </plugins>
    </build>

#在src/java目录创建一个类名为HelloWorldTest的类

package com.xurujie;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloWorldTest {
    public static void main(String[] args) {
        SpringApplication.run(HelloWorldTest.class,args);
    }
}

#创建一个HelloController的类

package com.xurujie.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/hello")
    public  String hello(){
        return "Hello World";
    }
}

由于springboot中自带了tomcat,所以直接运行就好
点击运行HelloWorldTest的中的main方法可以启动tomcat服务器
启动的tomcat服务器默认端口为8080(可以改)
访问localhost:8080/hello
即可看到浏览器输出Hello World.

### 如何开始使用 Spring Boot 进行项目开发 #### 创建一个新的 Spring Boot 项目 为了启动并运行一个简单的 Spring Boot 应用程序,在集成开发环境(IDE)如 IntelliJ IDEA 中创建新项目时可以选择 Spring Initializr 来初始化。这会自动生成基本的项目结构以及必要的依赖项。 #### 配置 `pom.xml` 文件 Maven 是常用的构建工具之一,通过编辑项目的 `pom.xml` 可以为应用添加所需的库和插件。对于大多数标准应用程序来说,默认生成的内容已经足够满足需求[^2]。 ```xml <dependencies> <!-- 添加 spring-boot-starter-web 以便于 Web 开发 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 测试框架 JUnit 和 Mockito 的支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> ``` #### 编写主类 每一个 Spring Boot 应用都需要有一个入口点——通常是一个带有 `@SpringBootApplication` 注解的 Java 类,并且该类中定义了一个静态方法 `main()` 。这个注解包含了多个其他有用的元数据标签,简化了配置过程。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ``` #### 使用全局配置文件 在 src/main/resources 下可以找到名为 application.properties 或者 application.yml 的文件,这是用来存储应用程序设置的地方。这里不仅可以设定端口号、数据库连接字符串等基本信息,还可以利用占位符语法实现更复杂的场景下的参数替换功能[^1]。 例如: ```properties server.port=8090 spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=password ``` 或者 YAML 格式的相同配置如下所示: ```yaml server: port: 8090 spring: datasource: url: jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC username: root password: password ``` #### 属性值注入到 Bean 中 当希望将某些特定属性直接映射给某个对象实例的时候,则可以通过构造函数注入的方式完成这一操作;另外一种常见做法是在字段上加上相应的注释标记,比如 @Value("${property.name}") ,从而使得这些变量能够在编译期间被自动赋值。 ```java @RestController @RequestMapping("/api/v1") class ExampleController { private final String welcomeMessage; @Autowired public ExampleController(@Value("${app.welcome.message}") String message){ this.welcomeMessage = message; } @GetMapping("/hello") public ResponseEntity<String> sayHello(){ return new ResponseEntity<>(welcomeMessage, HttpStatus.OK); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值