首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >《Spring实战》摘录 - 30

《Spring实战》摘录 - 30

作者头像
用户1335799
发布2019-09-10 13:33:34
4270
发布2019-09-10 13:33:34
举报

301

Q: #21.1-1 | 在Spring家族中,Spring Boot是令人兴奋(也许我敢说它是改变游戏规则的)的新项目。它提供了四个主要的特性,能够改变开发Spring应用程序的方式

A:

  • Spring Boot Starter:它将常用的依赖分组进行了整合,将其合并到一个依赖中,这样就可以一次性添加到项目的Maven或Gradle构建中;
  • 自动配置:Spring Boot的自动配置特性利用了Spring 4对条件化配置的支持,合理地推测应用所需的bean并自动化配置它们;
  • 命令行接口(Command-line interface,CLI):Spring Boot的CLI发挥了Groovy编程语言的优势,并结合自动配置进一步简化Spring应用的开发;
  • Actuator:它为Spring Boot应用添加了一定的管理特性。

302

Q: #21.1.1-1 | Spring Boot Starter依赖将所需的常见依赖按组聚集在一起,形成单条依赖

A:

  • 左侧是启动器starter,右侧是所提供的依赖。
  • spring-boot-starter-actuator --- spring-boot-starter 、spring-boot-actuator 、spring-core
  • spring-boot-starter-amqp --- spring-boot-starter 、spring-boot-rabbit 、spring-core 、 spring-tx
  • spring-boot-starter-aop --- spring-boot-starter 、spring-aop 、AspectJ Runtime 、AspectJ Weaver 、spring-core
  • spring-boot-starter-batch --- spring-boot-starter 、HSQLDB 、spring-jdbc 、spring-batch-core 、spring-core
  • spring-boot-starter-elasticsearch --- spring-boot-starter、 spring-data-elasticsearch、 spring-core、 spring-tx
  • spring-boot-starter-gemfire --- spring-boot-starter、 Gemfire、 spring-core、 spring-tx、 spring-context、 spring-context-support、 spring-data-gemfire
  • spring-boot-starter-data-jpa --- spring-boot-starter、 spring-boot-starter-jdbc、 spring-boot-starter-aop、 spring-core、 Hibernate EntityManager、 spring-orm、 spring-data-jpa、 spring-aspects
  • spring-boot-starter-data-mongodb --- spring-boot-starter、 MongoDB Java 驱动 、 spring-core、 spring-tx、 spring-data-mongodb
  • spring-boot-starter-data-rest --- spring-boot-starter、 spring-boot-starter-web、 Jackson 注解 、 Jackson 数据绑定 、 spring-core、 spring-tx、 spring-data-rest-webmvc
  • spring-boot-starter-data-solr --- spring-boot-starter、 Solrj、 spring-core、 spring-tx、 spring-data-solr、 Apache HTTP Mime
  • spring-boot-starter-freemarker --- spring-boot-starter、 spring-boot-starter-web、 Freemarker、 spring-core、 spring-context-support
  • spring-boot-starter-groovy-templ-ates --- spring-boot-starter、 spring-boot-starter-web、 Groovy、 Groovy 模板、spring-core
  • spring-boot-starter-hornetq --- spring-boot-starter、 spring-core、 spring-jms、 Hornet JMS Client
  • spring-boot-starter-integration --- spring-boot-starter、 spring-aop、 spring-tx、 spring-web、 spring-webmvc、 spring-integration-core、 spring-integration-file、 spring-integration-http、 spring-integration-ip、 spring-integration-stream
  • spring-boot-starter-jdbc --- spring-boot-starter、 spring-jdbc 、tomcat-jdbc、 spring-tx
  • spring-boot-starter-jetty --- jetty-webapp、 jetty-jsp
  • spring-boot-starter-log4j --- jcl-over-slf4j、 jul-to-slf4j 、slf4j-log4j12、log4j
  • spring-boot-starter-logging --- jcl-over-slf4j、 jul-to-slf4j 、log4j-over-slf4j、 logback-classic
  • spring-boot-starter-mobile --- spring-boot-starter、 spring-boot-starter-web、 spring-mobile-device
  • spring-boot-starter-redis --- spring-boot-starter、 spring-data-redis、 lettuce
  • spring-boot-starter-remote-shell --- spring-boot-starter-actuator、 spring-context、 org.crashub.**
  • spring-boot-starter-security --- spring-boot-starter、 spring-security-config、 spring-security-web、 spring-aop、 spring-beans、 spring-context、 spring-core、 spring-expression、 spring-web
  • spring-boot-starter-social-facebook --- spring-boot-starter、 spring-boot-starter-web、 spring-core、 spring-social-config、 spring-social-core、 spring-social-web、 spring-social-facebook
  • spring-boot-starter-social-twitter --- spring-boot-starter、 spring-boot-starter-web、 spring-core、 spring-social-config、 spring-social-core、 spring-social-web、 spring-social-twitter
  • spring-boot-starter-social-linkedin --- spring-boot-starter、 spring-boot-starter-web、 spring-core、 spring-social-config、 spring-social-core、 spring-social-web、 spring-social-linkedin
  • spring-boot-starter --- spring-boot、 spring-boot-autoconfigure、 spring-boot-starter-logging
  • spring-boot-starter-test --- spring-boot-starter-logging、 spring-boot、 junit、mockito-core、 hamcrest-library、 spring-test
  • spring-boot-starter-thymeleaf --- spring-boot-starter、 spring-boot-starter-web、 spring-core、 thymeleaf-spring4、 thymeleaf-layout-dialect
  • spring-boot-starter-tomcat --- tomcat-embed-core、 tomcat-embed-logging-juli
  • spring-boot-starter-web --- spring-boot-starter、 spring-boot-starter-tomcat、 jackson-databind、 spring-web、 spring-webmvc
  • spring-boot-starter-websocket --- spring-boot-starter-web、 spring-websocket、 tomcat-embed-core、 tomcat-embed-logging-juli
  • spring-boot-starter-ws --- spring-boot-starter、 spring-boot-starter-web、 spring-core、 spring-jms、 spring-oxm、 spring-ws-core、 spring-ws-support

303

Q: #21.1.4-1 | Spring Boot Actuator为Spring Boot项目带来了很多有用的特性,包括

A:

  • 管理端点;
  • 合理的异常处理以及默认的“/error”映射端点;
  • 获取应用信息的“/info”端点;
  • 当启用Spring Security时,会有一个审计事件框架。

304

Q: #21.2-1 | Contacts应用所需的Gradle构建文件

A:

build script{
    repositories {
        mavenLocal();
    }
    dependencies{
        classpath(“org.springframework.boot:spring-boot-gradle-plugin:1.1.4.RELEASE")
    }
}
apply plugin: ‘java’
apply plugin: ’spring-boot’//使用Spring Boot插件
jar {//构建JAR文件
    baseName = ‘contacts’
    version = ‘0.1.0'
}
repositories{
    mavenCentral()
}
dependencies{//依赖将会放到这里
}
task wrapper(type: Wrapper){
    gradleVersion=‘1.8'
}

305

Q: #21.2-2 | Contacts应用所需的Maven构建文件

A:

<?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.hahuma</grooupId>
    <artifactId>contacts</artifactId>
    <version>0.1.0</version>
    <packaging>jar</packaging> //构建jar文件
    <parent>
        <groupId>org.springframework.boot</group> //继承自spring-boot-starter-parent
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.4</version>
    </parent>
    <dependencies>
                    //依赖将会放到这里
    </dependencies>
    <build>
        <plugins> //构建jar文件
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugins>
    </build>
</project>

306

Q: #21.2.1-1 | 因为我们要使用Spring MVC来开发应用的Web层,因此需要将Spring MVC作为依赖添加到构建中。Spring Boot的Web Starter能够将Spring MVC需要的所有内容一站式添加到构建中。如下是我们所需的依赖:

A:

##Gradle依赖

compile("org.springframework.boot:spring-boot-starter-web")

##Maven依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

307

Q: #21.2.2-2 | ContactController为Contacts应用处理基本的Web请求

A:

package contacts;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotypr.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(“/“)
public class ContactController{
    private ContactRepository contactRepo;
    
    @Autowired
    public ContactController(ContactRepository contactRepo){//注入ContactRepository
        this.contactRepo = contactRepo;
    }
    @RequestMapping(method=RequestMethod.GET)//处理GET“/”
    public String home(Map<String, Object> model){
        List<Contact> contacts = contactRepo.findAll();
        model.put(“contacts”,contacts);
        return “home”;
    }
    @RequestMapping(method=RequestMethod.POST)//处理POST“/”
    public String submit(Contact contact){
        contactRepo.save(contact);
        return “redirect:/“;
    }
}

308

Q: #21.2.2-1 | home视图渲染了一个创建新联系人的表单以及展现联系人的列表

A:

<!DOCTYPE html>
<html xmlns:th=“http://www.thymeleaf.org”>
    <head>
        <title>Spring Boot Contacts</title>
        <link rel=“stylesheet” th:href=“@(/style.css)” />//加载样式表
    </head>
    <body>
        <h2>Spring Boot Contacts</h2>
        <form method=“POST”>//新联系人的表单
            <label for=“firstName”>First Name:/</label>
            <input type=“text” name=“firstName”></input><br/>
                
            <label for=“lastName”>Last Name:/</label>
            <input type=“text” name=“lastName”></input><br/>
                
            <label for=“phoneNumber”>Phone #:/</label>
            <input type=“text” name=“phoneNumber”></input><br/>
            <label for=“emailAddress”>Email::/</label>
            <input type=“text” name=“emailAddress”></input><br/>
            <input type=“submit”></input>
        </form>
        <ul th:each=“contact:$(contacts)”>//渲染联系人表单
            <li>
                <span th:text=“$(contact.firstName)”>First</span>
                <span th:text=“$(contact.lastName)”>Last</span>
                <span th:text=“$(contact.phoneNumber)”>phoneNumber</span>
                <span th:text=“$(contact.emailAddress)”>emailAddress</span>
            </li>
        </ul>
    </body>
</html>

309

Q: #21.2.4-1 | ContactRepository能够从数据库中存取Contact

A:

package contacts;
import java.util.List;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
@Repository
public class ContactRepository{
    private JdbcTemplate jdbc;
    @Autowired
    public ContactRepository(JdbcTemplate jdbc){
        this.jdbc=jdbc;
    }
    public List<Contact> findAll(){
        retrun jdbc.query(
            “select id, firstName, lastName, phoneNumber, emailAddress “ + 
            “from contacts order by lastName”,
            new RowMapper<Contact>(){
                public Contact mapRow(ResultSet rs, int rowNum)throws SQLException {
                    Contact contact = new Contact();
                    contact.setId(rs.getLong(1));
                    contact.setFirstName(rs.getLong(2));
                    contact.setLastName(rs.getLong(3));
                    contact.setPhoneNumber(rs.getLong(4));
                    contact.setEmailAddress(rs.getLong(5));
                    return contact;
                }
            }
        );
    }
    public void save(Contact contact){
        jdbc.update(
            “insert into contacts “ + 
            “(firstName, lastName, phoneNumber, emailAddress) “ +
            “values (?, ?, ?, ?)”,
        contact.getFirstName(), contact.getLastName(),
        contact.getPhoneNumber(), contact.getEmailAddress());
    }
}

310

Q: #21.2.5-1 | 初始化Spring Boot配置的简单启动类

A:

package contacts;
import org.springframework.boot.autoconfigure.EnableAutoConfigureation;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
@EnableAutoConfiguration//启用自动配置
public class Application{
    public static void main(String[] args){
        SpringApplication.run(Application.class, args);//运行应用
    }
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-09-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 怀英的自我修炼 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档