springboot的优势及开发第一个springboot应用程序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41723615/article/details/88169349

优点:

1.springboot使编码变得简单

2.springboot使配置变得简单

3.springboot使部署变得简单

4.springboot使监控变得简单

5.springboot帮助开发者快速启动一个容器

springboot的主要特性:

1.遵循“习惯优于配置”的原则,使用Spring Boot只需要很少的配置,大部分的时候我们直接使用默认的配置即可;

2.项目可以快速的搭建,无需自动配置整合第三方框架;

3.可以不使用xml配置文件,只需要自动配置和Java Config;

4.内嵌Servlet容器,降低了对环境的要求,可以使用命令直接执行项目,应用可用jar包执行:java -jar;

5.提供了starter POM, 能够非常方便的进行包管理, 很大程度上减少了jar hell或者dependency hell;

6.运行中应用状态的监控;

7.对主流开发框架的无配置集成;

8.提供嵌入式HTTP服务器,如Tomcat、Jetty,以开发和测试Web应用程序非常简单

9.提供很多插件,以便与嵌入式和数据库工作非常容易

springboot核心功能:

1.独立运行的spring项目

2.内嵌的servlet容器

3.提供starter简化maven配置

4.自动配置spring 

5.应用监控

springboot是伴随着spring4.0诞生的

下面开发第一个springboot应用程序:

本次将使用到CLI,安装步骤参考文章:https://blog.csdn.net/qq_41723615/article/details/88173130

使用IDEA中的Initializr创建springboot项目:

如何创建参考文章:https://blog.csdn.net/qq_41723615/article/details/88177052

     项目目录结构:

本程序用到Web,Thymeleaf,JPA,需要在pom.xml中添加依赖;

DemoApplication类

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args);
    }

}

DemoApplicationTests类

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoApplication.class)
@WebAppConfiguration
public class DemoApplicationTests {

    @Test
    public void contextLoads() {
    }
}

@SpringBootTest注解:通过Spring Boot加载上下文

首先在org.test.demo下创建实体类Book.java

@Entity
public class Book {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String reader;
    private String isbn;
    private String title;
    private String author;
    private String description;
//setter、getter略

@Entity注解表明它是一个JPA实体,id属性加了@Id和@GeneratedValue注解,说明这个字段是实体唯一标识,

并且这个字段是自动生成的。

    定义仓库接口

public interface ReadingListRepository extends JpaRepository<Book, Long> {

    List<Book> findByReader(String reader);
}

        通过扩展JpaRepository,ReadingListRepository直接继承了18个执行常用持久化操作
的方法。JpaRepository是个泛型接口,有两个参数:仓库操作的领域对象类型,及其ID属性的
类型。此外,我还增加了一个findByReader()方法,可以根据读者的用户名来查找阅读列表。
        如果你好奇谁来实现这个ReadingListRepository及其继承的18个方法,请不用担心,
Spring Data提供了很神奇的魔法,只需定义仓库接口,在应用程序启动后,该接口在运行时会自
动实现。

    创建Web界面

@Controller
@RequestMapping("/")
public class ReadingListController {

    private ReadingListRepository readingListRepository;

    @Autowired
    public ReadingListController(
            ReadingListRepository readingListRepository){
        this.readingListRepository = readingListRepository;
    }

    @RequestMapping(value = "/{reader}", method = RequestMethod.GET)
    public String readersBooks(
            @PathVariable("reader") String reader,
            Model model){

        List<Book> readingList = readingListRepository.findByReader(reader);
        if(readingList != null) {
            model.addAttribute("books",readingList);
        }
        return "readingList";
    }

    @RequestMapping(value = "/{reader}",method = RequestMethod.POST)
    public String addToReadingList(@PathVariable("reader") String reader,Book book){
        book.setReader(reader);
        readingListRepository.save(book);
        return "redirect:/{reader}";
    }
}

readersBooks():处理/{reader}上的HTTP GET请求,根据路劲里指定的读者,从(通过控制器的构造器注入的)

仓库获取Book列表。随后将这个列表塞入模型,用的键是books,最后返回readingList作为呈现模型的视图逻辑名称。

addToReadingList():处理/{reader}上的POST请求,将请求正文里的数据绑定到一个Book对象上。该方法

把Book对象的reader属性设置为读者的姓名,随后通过仓库的save()方法保存修改后的Book对象,最后重定向到

/{reader}(控制器中的另一个方法会处理该请求)。

readersBook()方法最后返回readingList作为逻辑视图名,为此创建视图。因为在项目开始之处我就决定要用

Thymeleaf来定义应用程序的视图,所以接下来就在src/main/resource/templates里创建一个名为radingList.html文件。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Reading List</title>
    <link rel="stylesheet" th:href="@{/style.css}"/>
</head>
<body>
<h2>Your Reading List</h2>
<div th:unless="${#lists.isEmpty(books)}">
    <dl th:each="book : ${books}">
        <dt class="bookHeadline">
            <span th:text="${book.title}">Title</span> by
            <span th:text="${book.author}">Author</span>
            (ISBN: <span th:text="${book.isbn}">ISBN</span>)
        </dt>
        <dd class="bookDescription">
 <span th:if="${book.description}"
       th:text="${book.description}">Description</span>
            <span th:if="${book.description eq null}">
 No description available</span>
        </dd>
    </dl>
</div>
<div th:if="${#lists.isEmpty(books)}">
    <p>You have no books in your book list</p>
</div>
<hr/>
<h3>Add a book</h3>
<form method="POST">
    <label for="title">Title:</label>
    <input type="text" name="title" size="50"></input><br/>
    <label for="author">Author:</label>
    <input type="text" name="author" size="50"></input><br/>
    <label for="isbn">ISBN:</label>
    <input type="text" name="isbn" size="15"></input><br/>
    <label for="description">Description:</label><br/>
    <textarea name="description" cols="80" rows="5">
 </textarea><br/>
    <input type="submit"></input>
</form>


</div>
</body>
</html>

在static目录下创建style.css

body{
    background: #cccccc;
    font-family: arial,helvetica,sans-serif;
}
.bookHeadline{
    font-size: 12pt;
    font-weight: bold;
}
.bookDesription{
    font-size: 10pt;
}
label{
    font-weight: bold;
}

运行程序

因为Classpath里有H2,所以会创建一个嵌入式的H2数据库Bean,它的类型是
javax.sql.DataSource,JPA实现(Hibernate)需要它来访问数据库。

因为Classpath里有Hibernate(Spring Data JPA传递引入的)的实体管理器,所以自动配置
会配置与 Hibernate 相关的 Bean ,包括 Spring 的 LocalContainerEntityManager-FactoryBean和JpaVendorAdapter。


因为Classpath里有Spring Data JPA,所以它会自动配置为根据仓库的接口创建仓库实现。


因为Classpath里有Thymeleaf,所以Thymeleaf会配置为Spring MVC的视图,包括一个
Thymeleaf的模板解析器、模板引擎及视图解析器。视图解析器会解析相对于Classpath根
目录的/templates目录里的模板。


因 为 Classpath 里 有 Spring MVC (归功于 Web 起 步 依 赖 ), 所 以 会 配 置 Spring 的
DispatcherServlet并启用Spring MVC。


因为这是一个Spring MVC Web应用程序,所以会注册一个资源处理器,把相对于Classpath
根目录的/static目录里的静态内容提供出来。(这个资源处理器还能处理/public、/resources
和/META-INF/resources的静态内容。)


因为Classpath里有Tomcat(通过Web起步依赖传递引用),所以会启动一个嵌入式的Tomcat
容器,监听8080端口

猜你喜欢

转载自blog.csdn.net/qq_41723615/article/details/88169349