`
234390216
  • 浏览: 10218584 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
博客专栏
A5ee55b9-a463-3d09-9c78-0c0cf33198cd
Oracle基础
浏览量:462008
Ad26f909-6440-35a9-b4e9-9aea825bd38e
springMVC介绍
浏览量:1774424
Ce363057-ae4d-3ee1-bb46-e7b51a722a4b
Mybatis简介
浏览量:1397488
Bdeb91ad-cf8a-3fe9-942a-3710073b4000
Spring整合JMS
浏览量:394699
5cbbde67-7cd5-313c-95c2-4185389601e7
Ehcache简介
浏览量:679501
Cc1c0708-ccc2-3d20-ba47-d40e04440682
Cas简介
浏览量:530288
51592fc3-854c-34f4-9eff-cb82d993ab3a
Spring Securi...
浏览量:1180700
23e1c30e-ef8c-3702-aa3c-e83277ffca91
Spring基础知识
浏览量:465990
4af1c81c-eb9d-365f-b759-07685a32156e
Spring Aop介绍
浏览量:151028
2f926891-9e7a-3ce2-a074-3acb2aaf2584
JAXB简介
浏览量:67732
社区版块
存档分类
最新评论

Spring Boot(01)——初体验

阅读更多

Spring Boot初体验

Spring Boot的理念是抛弃XML配置(当然,如果你想采用XML配置也是可以的,但是Spring Boot推荐使用基于Java的配置),采用纯Java配置和properties或yml文件配置,通过提供一系列的Starter可以使开发者快速的搭建起一套开发环境。Starter将某一工具相关的依赖整合到了一起,通过依赖一个Starter会间接的依赖该Starter相关的所有依赖。使用的Starter在启用了自动配置时是可以自识别的,即只要把它们加入到Classpath中,在程序启动的时候可以进行自动的识别和启动。比如想使用Spring Data MongoDB,只需加入spring-boot-starter-data-mongodb依赖,然后通过配置文件配置MongoDB对应的配置信息即可。使用Spring Boot时,通常会使用Maven进行项目管理,然后指定spring-boot-starter-parent为父项目。spring-boot-starter-parent中间接的提供了一些<dependencyManagement/>,将它作为父项目后在项目需要使用依赖时可以忽略版本号的配置,非常的方便。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
</parent>

定义好的<dependencyManagement/>可以从artifactId为spring-boot-dependencies的pom.xml中查看,它属于spring-boot-starter-parent的父项目。如果需要改变依赖项的版本号,可以在子项目中重写对应的properties中的版本号。

比如想要开发一个Web项目,在项目的pom.xml文件中加入spring-boot-starter-web的依赖,在项目启动时就会自动的按照SpringMVC的配置为我们配置好。这是由Spring boot的autoconfigure模块来实现的,autoconfigure模块中定义了一系列的自动配置类,它们的自动配置规则基本都差不多。比如在classpath下拥有某Class的时候进行自动配置,或者在配置文件中配置了某个属性后进行自动配置,等等。每个自动配置类都使用了@Configuration标注。需要启用自动配置,需要在Spring Boot的入口类上加上@EnableAutoConfiguration注解。

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

查看spring-boot-starter-web的pom.xml文件可以看到它打包了以下依赖项:

 <dependencies>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter</artifactId>
     <version>2.0.3.RELEASE</version>
     <scope>compile</scope>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-json</artifactId>
     <version>2.0.3.RELEASE</version>
     <scope>compile</scope>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-tomcat</artifactId>
     <version>2.0.3.RELEASE</version>
     <scope>compile</scope>
   </dependency>
   <dependency>
     <groupId>org.hibernate.validator</groupId>
     <artifactId>hibernate-validator</artifactId>
     <version>6.0.10.Final</version>
     <scope>compile</scope>
   </dependency>
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-web</artifactId>
     <version>5.0.7.RELEASE</version>
     <scope>compile</scope>
   </dependency>
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-webmvc</artifactId>
     <version>5.0.7.RELEASE</version>
     <scope>compile</scope>
   </dependency>
 </dependencies>

其它的Starter也是类似的,打包了相关的依赖项。

SpringMVC的自动配置类由DispatcherServletAutoConfiguration定义,其主要源码如下所示。我们可以看到类上面拥有各种各样的Conditional打头的注解,它们都是用来定义对应的配置生效的条件。

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass(DispatcherServlet.class)
@AutoConfigureAfter(ServletWebServerFactoryAutoConfiguration.class)
@EnableConfigurationProperties(ServerProperties.class)
public class DispatcherServletAutoConfiguration {

    /*
     * The bean name for a DispatcherServlet that will be mapped to the root URL "/"
     */
    public static final String DEFAULT_DISPATCHER_SERVLET_BEAN_NAME = "dispatcherServlet";

    /*
     * The bean name for a ServletRegistrationBean for the DispatcherServlet "/"
     */
    public static final String DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME = "dispatcherServletRegistration";

    @Configuration
    @Conditional(DefaultDispatcherServletCondition.class)
    @ConditionalOnClass(ServletRegistration.class)
    @EnableConfigurationProperties(WebMvcProperties.class)
    protected static class DispatcherServletConfiguration {

        private final WebMvcProperties webMvcProperties;

        public DispatcherServletConfiguration(WebMvcProperties webMvcProperties) {
            this.webMvcProperties = webMvcProperties;
        }

        @Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
        public DispatcherServlet dispatcherServlet() {
            DispatcherServlet dispatcherServlet = new DispatcherServlet();
            dispatcherServlet.setDispatchOptionsRequest(
                    this.webMvcProperties.isDispatchOptionsRequest());
            dispatcherServlet.setDispatchTraceRequest(
                    this.webMvcProperties.isDispatchTraceRequest());
            dispatcherServlet.setThrowExceptionIfNoHandlerFound(
                    this.webMvcProperties.isThrowExceptionIfNoHandlerFound());
            return dispatcherServlet;
        }

        @Bean
        @ConditionalOnBean(MultipartResolver.class)
        @ConditionalOnMissingBean(name = DispatcherServlet.MULTIPART_RESOLVER_BEAN_NAME)
        public MultipartResolver multipartResolver(MultipartResolver resolver) {
            // Detect if the user has created a MultipartResolver but named it incorrectly
            return resolver;
        }

    }

    @Configuration
    @Conditional(DispatcherServletRegistrationCondition.class)
    @ConditionalOnClass(ServletRegistration.class)
    @EnableConfigurationProperties(WebMvcProperties.class)
    @Import(DispatcherServletConfiguration.class)
    protected static class DispatcherServletRegistrationConfiguration {

        private final ServerProperties serverProperties;

        private final WebMvcProperties webMvcProperties;

        private final MultipartConfigElement multipartConfig;

        public DispatcherServletRegistrationConfiguration(
                ServerProperties serverProperties, WebMvcProperties webMvcProperties,
                ObjectProvider<MultipartConfigElement> multipartConfigProvider) {
            this.serverProperties = serverProperties;
            this.webMvcProperties = webMvcProperties;
            this.multipartConfig = multipartConfigProvider.getIfAvailable();
        }

        @Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
        @ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
        public ServletRegistrationBean<DispatcherServlet> dispatcherServletRegistration(
                DispatcherServlet dispatcherServlet) {
            ServletRegistrationBean<DispatcherServlet> registration = new ServletRegistrationBean<>(
                    dispatcherServlet,
                    this.serverProperties.getServlet().getServletMapping());
            registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
            registration.setLoadOnStartup(
                    this.webMvcProperties.getServlet().getLoadOnStartup());
            if (this.multipartConfig != null) {
                registration.setMultipartConfig(this.multipartConfig);
            }
            return registration;
        }

    }

    //...忽略部分代码,完整的代码请参考对应的源码

}

@EnableConfigurationProperties用来定义该自动配置内部可以使用的配置参数对应的配置类,配置参数对应的配置类上都会使用@ConfigurationProperties指定配置属性对应的前缀,然后配置类上的每一个属性加上对应的配置属性前缀组成一个完整的配置文件中的属性名。比如下面的WebMvcProperties指定的属性前缀是spring.mvc,其属性dateFormat将匹配配置文件中的spring.mvc.dateFormat属性值。如果配置类的属性仍然是一个对象,则可以进行多级嵌套,多个层级之间通过英文的句号进行连接。

@ConfigurationProperties(prefix = "spring.mvc")
public class WebMvcProperties {

    /**
     * Formatting strategy for message codes. For instance, `PREFIX_ERROR_CODE`.
     */
    private DefaultMessageCodesResolver.Format messageCodesResolverFormat;

    /**
     * Locale to use. By default, this locale is overridden by the "Accept-Language"
     * header.
     */
    private Locale locale;

    /**
     * Define how the locale should be resolved.
     */
    private LocaleResolver localeResolver = LocaleResolver.ACCEPT_HEADER;

    /**
     * Date format to use. For instance, `dd/MM/yyyy`.
     */
    private String dateFormat;

    /**
     * Whether to dispatch TRACE requests to the FrameworkServlet doService method.
     */
    private boolean dispatchTraceRequest = false;

    /**
     * Whether to dispatch OPTIONS requests to the FrameworkServlet doService method.
     */
    private boolean dispatchOptionsRequest = true;

    /**
     * Whether the content of the "default" model should be ignored during redirect
     * scenarios.
     */
    private boolean ignoreDefaultModelOnRedirect = true;

    /**
     * Whether a "NoHandlerFoundException" should be thrown if no Handler was found to
     * process a request.
     */
    private boolean throwExceptionIfNoHandlerFound = false;

    /**
     * Whether to enable warn logging of exceptions resolved by a
     * "HandlerExceptionResolver".
     */
    private boolean logResolvedException = false;

    /**
     * Path pattern used for static resources.
     */
    private String staticPathPattern = "/**";

    //...忽略部分代码

}

这样我们就可以直接在项目中定义Controller,并定义对应的处理器方法。

@Controller
public class SampleController {

    @RequestMapping("sample/helloworld")
    public void sample(Writer writer) throws IOException {
        writer.append("hello world!");
        writer.flush();
    }

}

接下来需要启动项目,项目可以按照纯Java项目进行启动,也可以打包为war包丢到Tomcat中。以下是直接按照纯Java项目进行启动的方式。通常建议在项目的根包路径下创建一个Class用于启动项目,而且在根包下面通常只有这样一个启动类。比如下面我们的Application如果是定义在com.elim.springboot包下面的,则项目在启动后Spring将自动将com.elim.springboot包作为根包进行bean定义扫描,这是由于类上加了@SpringBootApplication。启动类需要使用@SprintBootApplication注解进行标注,然后在main方法体中调用SpringApplication的run方法,然后传递需要作为配置类的Class作为第一个参数,如果有多个这样的Class,也可以传递一个数组,第二个参数为启动时传递的参数,通常就直接取main方法传递的参数,这样Spring Boot就启动了。@SprintBootApplication上是使用了@EnableAutoConfiguration注解的,所以下面的代码将启用Spring Boot的自动配置机制。另外@SpringBootApplication上使用了@Configuration@ComponentScan,这就相当于使用@SpringBootApplication标注的类等价于标注了@Configuration@ComponentScan。这允许我们在@SpringBootApplication类中定义Spring Bean、进行bean扫描等。

@SpringBootApplication
public class Application {

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

Spring Boot的核心是基于Java配置的Spring的进一步封装,所以我们在它的配置类上可以像在使用Java配置类时一样使用,比如加上@EnableAsync@ImportResource@Import等。当有多个配置类时,Spring Boot也是建议在主配置类上进行引入。

启动完成后就可以打开浏览器,通过/sample/helloworld访问上面定义的SampleController的sample方法了。

Spring Boot的核心配置文件是类根路径下的application.properties文件或application.yml文件。Spring Boot中需要的外部配置信息基本都从这里来,包括前面提到的自动配置的ConfigurationProperties。看下面代码中,类SampleController有一个appId属性,其值由test.appId这么一个占位符指定。

@Controller
public class SampleController {

    @Value("${test.appId}")
    private String appId;
    
    @RequestMapping("sample/helloworld")
    public void sample(Writer writer) throws IOException {
        writer.append("hello world!");
        writer.append(this.appId);
        writer.flush();
    }

}

那么在我们的application.properties文件中就可以指定这么一个占位符并进行赋值。程序运行时appId的值就将被替换为下面配置的Spring Boot

test.appId=Spring Boot

Spring Boot的配置文件其实除了application.properties或application.yml外,根据启动的profile的不同,还可以读取带profile后缀的配置文件。比如启动的profile是dev,则可以读取application-dev.properties或application-dev.yml文件。

(注:本文是基于Spring Boot 2.0.3所写)

0
0
分享到:
评论

相关推荐

    spring-boot-demo

    《Spring Boot基础与实战——以spring-boot-demo项目为例》 Spring Boot是Java开发中的一个热门框架,它由Pivotal团队提供,旨在简化Spring应用的初始搭建以及开发过程。Spring Boot通过提供开箱即用的设置,使得...

    一个基于Spring boot+mybatis客户管理系统

    本项目——“基于Spring Boot+MyBatis的CRM客户管理系统”就是这样一个实用的平台,适合作为开发者的学习模板或企业内部的CRM解决方案。 Spring Boot是Spring框架的简化版,它内置了Tomcat服务器,提供了快速开发...

    生成一个简单的Spring Boot应用程序.pdf

    本文档旨在指导读者通过具体步骤创建一个简单的Spring Boot Web应用程序——“Hello World!”示例。这个示例应用将帮助理解Spring Boot的一些核心特性,如自动配置、依赖管理和启动速度等。 #### 重要知识点 1. **...

    IDEA社区版SpringBoot插件Spring Assistant

    然而,对于Spring Boot的开发者来说,有一个重要的工具——Spring Assistant,在2019.3.5版本之后,IDEA社区版官方不再直接支持。这无疑给使用社区版的开发者带来了一些不便。庆幸的是,经过热心社区成员的努力,...

    SpringBlogSystem——基于Spring Boot的博客系统.zip

    SpringBlogSystem是一个基于Spring Boot技术栈的博客系统。它提供了用户注册、登录、创建博客、编辑博客、评论、点赞、标签分类、热门内容推荐、全文搜索等功能。该项目使用了Spring框架,Spring Boot,Spring mVC,...

    springboot176基于Spring Boot的装饰工程管理系统.zip

    在现代软件开发中,Spring Boot以其简洁的配置、快速的开发体验以及强大的生态系统,已经成为Java企业级应用的首选框架。本系统以"springboot176基于Spring Boot的装饰工程管理系统"为例,深入解析如何利用Spring ...

    就业信息管理系统-使用spring boot+layui

    《就业信息管理系统——基于Spring Boot与Layui的实现》 在现代信息技术的快速发展下,管理系统已经成为各行各业不可或缺的工具,特别是在教育领域,就业信息管理系统的应用尤为重要。本系统以"就业信息管理系统-...

    基于Spring Boot的网上购物系统设计与实现

    基于Spring Boot的网上购物系统设计与实现是一项重要的技术实践,它结合了现代Web开发的两大主流框架——Spring Boot和Vue.js,构建了一个高效、灵活且易于维护的电商平台。本论文详细探讨了如何利用这些技术来构建...

    就业信息管理(spring boot+layui)

    《就业信息管理系统的实现——基于Spring Boot与Layui》 在现代信息技术的快速发展中,管理系统已经成为企业管理和运营的重要工具。本系统以“就业信息管理”为主题,利用先进的Web开发框架Spring Boot和前端UI库...

    LayUI+Spring Boot+MySQL+JPA+Shiro——科研信息管理系统.zip

    《基于LayUI+Spring Boot+MySQL+JPA+Shiro的科研信息管理系统设计与实现》 本项目是一个集成了多种技术的科研信息管理系统,旨在为科研机构提供一个高效、安全的信息管理平台。该系统充分利用了现代Web开发框架和...

    spring-boot-2.4.3.tar.gz

    在本例中,我们讨论的是 Spring Boot 的特定版本——2.4.3,它是一个针对Linux操作系统优化的发行版。 2.4.3是Spring Boot的一个稳定版本,包含了多个改进和修复,旨在提高性能和用户体验。这个版本可能包括了以下...

    spring boot 项目,Meeting会议管理系统

    《Meeting会议管理系统——基于Spring Boot与JavaEE的实践探索》 在现代企业中,高效的会议管理是提升组织协同效率的关键。本项目“Meeting会议管理系统”是利用Spring Boot和JavaEE技术栈开发的一款实用工具,旨在...

    spring boot实现音乐系统-web课程设计

    在本课程设计中,我们将探讨如何使用Spring Boot技术来实现一个音乐系统——“小丽音乐系统”。这个系统涵盖了Web开发的基本要素,包括增删改查(CRUD)操作,以及额外的功能如音乐下载和排行榜展示。为了确保项目的...

    SpringBoot+Mybatis整合完整源码

    【Spring Boot + Mybatis 整合详解】 Spring Boot 和 Mybatis 的整合是现代 Java 开发中的常见实践,它简化了传统的 Spring 框架配置,提供了快速开发 Web 应用程序的能力,而 Mybatis 作为轻量级的持久层框架,...

    java毕业设计&课设-Spring Boot企业某信点can系统(视频+源码).doc

    根据提供的文件信息,我们可以推断出这是一份关于Java毕业设计项目的文档,主要涉及Spring Boot技术栈,并且针对的是一个类似企业微信的功能模块——“点can系统”。下面将基于这个项目的信息来提炼出相关的知识点。...

    基于Spring boot的婚纱摄影系统.rar

    本项目——“基于Spring Boot的婚纱摄影系统”便是一个典型的应用实例,旨在为用户提供高效、便捷的在线摄影服务体验。该项目不仅适合大学生作为毕业设计的实践课题,也为开发者提供了深入学习Java框架与Web开发的...

    【毕设项目推荐】基于Spring Boot+ Vue的人力资源管理系统设计与实现

    本项目是针对这一需求,采用先进的技术栈——Spring Boot和Vue.js,构建了一个完整的人力资源管理系统,旨在提升人力资源管理的效率和质量。 Spring Boot是Java领域的一个轻量级框架,它简化了传统Spring应用的初始...

    vue+spring boot+maven+SQLSERVER+REDIS+MINIO+JENKINS+dockers生产调度

    这个项目——"vue+spring boot+maven+SQLSERVER+REDIS+MINIO+JENKINS+dockers生产调度"——是一个综合性的解决方案,旨在实现高效、自动化的工作流管理。下面我们将详细探讨每个组件的关键作用及其相互间的协同工作...

    spring-boot-seckill-master.zip

    《Spring Boot Seckill Master——构建高效电商秒杀系统的利器》 在现代的互联网开发中,Spring Boot已经成为构建高效、简洁微服务的首选框架。而"spring-boot-seckill-master.zip"这个压缩包则提供了一个用于快速...

    实施与分析 Spring Boot中的软件开发.pdf

    本文档主要探讨了如何利用Java Spring Boot框架来构建一个稳健且可扩展的后端系统,并通过实际案例研究——在线教育平台的开发过程,进一步验证了这一框架的有效性及其在现实世界中的应用场景。 #### 关键概念解析 ...

Global site tag (gtag.js) - Google Analytics