Hello,SpringBoot--从0创建一个SpringBoot项目

1.Idea新建项目
File --> New --> Maven Project,
下一步,输入groupId 和 ArtifactId ,
打开pom文件,可以看到Idea已经创建了一个Maven工程的基本信息。输入groupId 和 ArtifactId

  1. 增加web支持
    使用springBoot,仅仅需要在pom文件中声明使用SpringBoot,并添加一个spring-boot-starter-web的依赖即可。
    SpringBoot会使用内置的Tomcat作为web server,并且自动配置好Spring应用所需要的一切配置文件。
<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bee.sample</groupId>
    <artifactId>ch1.helloworld</artifactId>
    <version>1.0-SNAPSHOT</version>

<!--    在pom文件中声明使用SpringBoot-->
    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.1.6.RELEASE</version>
    </parent>
<!--    因为搭建的是web应用,需要添加spring-boot-starter-web依赖-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>


</project>

3.Hello SpringBoot示例
在工程中创建一个有main方法的类,类名是Ch1Application
在这里插入图片描述

package com.bee.sample.ch1;

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

//在类上添加注解SpringBootApplication,表明这是一个Spring Boot应用
@SpringBootApplication
public class Ch1Application {
    public static void main(String[] args) {
        SpringApplication.run(Ch1Application.class,args);
    }
}

这个类就是一个Spring Boot应用,但是没有写Controller,不能通过浏览器访问应用,因此再创建一个类,名为HelloWorldController.

package com.bee.sample.ch1.controller;

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

// @Controller是Spring MVC注解,表示这个类用于负责处理web请求
@Controller
public class HelloWorldController {
    // @RequestMapping是Spring MVC注解,表示如果请求路由匹配,被注解的方法将被调用
    @RequestMapping("/say.html")
    // @ResponseBody表示此方法返回的是文本而不是视图名称。
    public @ResponseBody String say(){
        return "Hello Spring Boot";
    }
}

注意⚠️:对于SpringBoot应用,建议启动程序的包名层次最高,其他类均在其下,这样SpringBoot默认自动搜索启动程序之下的所有类。

4.编写完后,重新启动Ch1Application程序:
在这里插入图片描述

打开浏览器,访问http://127.0.0.1:8080/say.html
在这里插入图片描述
至此,一个SpringBoot项目算是搭建完成了。

5.使用热部署
SpringBoot提供了spring-boot-devtools,它能在修改类或者配置文件的时候自动重新加载SpringBoot应用,只需要在pom文件中添加如下依赖。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

然后重启应用,修改say方法,改成如下:

    @RequestMapping("/sayhello.html")

启动应用,
在这里插入图片描述
在应用启动日志中,LiveReload server用于监控Spring Boot应用文件变化,这是因为加了spring-boot-devtools;

6.添加REST支持
如果你的系统不是一个单一的web应用,而是由多个系统构成的,系统之间的调用方式有很多,RESTFul是一种很好的方式,SpringBoot能方便地支持RESTFul应用。
在这里插入图片描述

package com.bee.sample.ch1.rest;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//@RestController可以理解为相当于@Controller和@ResponseBody
@RestController
public class UserReditRestController {
    @RequestMapping(value = "/usercredit/{id}")
    public Integer getCreditLevel(@PathVariable String id){
        // 模拟id用户的等级
        return 3;
    }
}

此时访问路径为 http://127.0.0.1:8080/usercredit/123, 数字123是任意的,对应getCreditLevel的参数id.

RESTFul 只是一种架构风格,并不是一种特别的技术体系。REST的具体知识将在后面继续学习。

注意⚠️:对于多个系统互相访问,最好不要直接访问对方的数据库,而应该采用类似RESTFul架构,封装了逻辑的接口。这样,即使对方系统的数据库变更,业务逻辑变化或者版本升级都不会影响其他系统。

7 AOP , 在Spring Boot中使用AOP
引入AOP依赖

<!--        引入AOP依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

编写AOP切面类

package com.bee.sample.ch1;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

@Configuration    //声明这是一个Spring管理配置的Bean
@Aspect     // 声明这是一个AOP类
public class AOPConfig {
    // @Around声明个表达式描述要织入的目标的特性,@within表示目标类型带有注解,有Controller注解的方法被调用的时候,都会执行
    //Around注解的方法,即simpleAOP方法
    @Around("@within(org.springframework.stereotype.Controller)")
    // simpleAOP方法,是用来织入的代码,参数为ProceedingJoinPoint
    public Object simpleAOP(final ProceedingJoinPoint pjp) throws  Throwable{
        try {
            Object[] args = pjp.getArgs();
            System.out.println("args:" + Arrays.asList(args));
            // 执行完切面代码,还要继续执行原有的应用代码,proceed()方法会继续调用原有的业务逻辑
            Object o = pjp.proceed();
            System.out.println("return:" + o);
            return o;
        }
        // 处理业务代码如果有异常,直接抛出给调用者
        catch (Throwable e) {
            throw e;
        }
    }
}

注意,要在前面的controller 的say方法中加个传参

public @ResponseBody String say(String name){
        return "Hello Spring Boot";
    }

访问http://127.0.0.1:8080/sayhello.html?name=a
控制台会有如下输出:
args:[a]
return:Hello Spring Boot
说明aop织入的代码生效了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值