Getting started with Spring Boot (with a small demo case)

1. What is Spring Boot

Spring was born as a lightweight alternative to Java Enterprise Edition (Java Enterprise Edition, JEE, also known as J2EE). Without the need to develop heavyweight Enterprise JavaBeans (EJBs), Spring provides a relatively simple method for enterprise-level Java development. Through dependency injection and aspect-oriented programming, EJBs are implemented with simple Java objects (Plain Old Java Object, POJO). function.

While Spring's component code is lightweight, its configuration is heavyweight. In the beginning, Spring used XML configuration, and a lot of XML configuration. Spring 2.5 introduces annotation-based component scanning, which eliminates a lot of explicit XML configuration for the application's own components. Spring 3.0 introduces Java-based configuration, a type-safe, reconfigurable configuration alternative to XML. All of these configurations represent development-time wear and tear. Because of the mental switch required between thinking about Spring feature configuration and solving business problems, writing configuration takes time away from writing application logic. Like all frameworks, Spring is useful, but at the same time it demands a lot in return.

In addition, project dependency management is also a thankless thing. Deciding which libraries to use in a project can be a headache, and knowing which versions of those libraries won't conflict with other libraries can be tricky. Moreover, dependency management is also a loss, adding dependencies is not writing application code. Once you choose the wrong version of a dependency, the ensuing incompatibility issues will undoubtedly be a productivity killer.

Spring Boot makes this a thing of the past.

   Spring Boot is a relatively new project in the Spring community. The purpose of this project is to help developers more easily create Spring-based applications and services, allow more people to get started with Spring faster, and provide a fixed, convention-over-configuration for the Spring ecosystem styled frame.

Spring Boot has the following features:

(1) Provide a faster entry experience for Spring-based development

(2) Out of the box, no code generation and no XML configuration. It is also possible to modify the default values ​​to meet specific needs.

(3) It provides some common non-functional features in large-scale projects, such as embedded server, security, indicators, health detection, external configuration, etc.

(4) Spring Boot does not enhance the functionality of Spring, but provides a fast way to use Spring.

2. Spring Boot Getting Started Small Demo

2.1 Starting dependencies

Create a Maven project springboot_demo (packaged jar) 

Add the following dependencies to pom.xml

  <parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

<version>1.4.0.RELEASE</version>

  </parent> 

  <dependencies>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

  </dependencies>

We will be surprised to find that our project automatically adds a lot of jar packages

    ........

And these jar packages are the jar packages that we need to import when we do development. Because these jar packages are referenced by the spring-boot-starter-web we just introduced, the dependencies will be automatically passed after we refer to the spring-boot-starter-web.

2.2 Change JDK version

We found that the JDK version of the project is 1.6 by default, and we usually use the 1.7 version, so we need to add the following configuration to pom.xml

  <properties>  

    <java.version>1.7</java.version>

  </properties>

After adding and updating the project, you will find that the version has been changed to 1.7

2.3 Bootstrap class

Just create a bootstrap class .

package cn.itcast.demo;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

publicclass Application {

    publicstaticvoid main(String[] args) {

       SpringApplication.run(Application.class, args);

    }

}

To briefly explain, @SpringBootApplication is actually the sum of the following three annotations

@Configuration: used to define a configuration class

@EnableAutoConfiguration : Spring Boot will automatically configure the project based on the dependencies of your jar package.

@ComponentScan: Tells Spring which packages annotated classes will be automatically scanned by spring and loaded into the bean container.

We directly execute this bootstrap class, and we will find the logo that appears in the console

Can you see what the picture above is?

2.4 Spring MVC实现Hello World输出

我们现在开始使用spring MVC框架,实现json数据的输出。如果按照我们原来的做法,需要在web.xml中添加一个DispatcherServlet的配置,再添加一个spring的配置文件,配置文件中需要添加如下配置

    <!-- 使用组件扫描,不用将controllerspring中配置 -->

         <context:component-scan base-package="cn.itcast.demo.controller" />

    <!-- 使用注解驱动不用在下边定义映射器和适配置器 -->

    <mvc:annotation-driven>

      <mvc:message-converters register-defaults="true">

        <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"

          <property name="supportedMediaTypes" value="application/json"/>

          <property name="features">

            <array>

              <value>WriteMapNullValue</value>

              <value>WriteDateUseDateFormat</value>

            </array>

          </property>

        </bean>

      </mvc:message-converters> 

    </mvc:annotation-driven>

但是我们用SpringBoot,这一切都省了。我们直接写Controller类

package cn.itcast.demo.controller;

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

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

@RestController

publicclass HelloWorldController {

    @RequestMapping("/info")

    public String info(){

       return"HelloWorld";    

    }     

}

我们运行启动类来运行程序

在浏览器地址栏输入http://localhost:8080/info 即可看到运行结果

2.5 修改tomcat启动端口

在src/main/resources下创建application.properties

server.port=8088

重新运行引导类。地址栏输入

http://localhost:8088/info

 2.6 读取配置文件信息

在src/main/resources下的application.properties 增加配置

url=http://www.itcast.cn

我要在类中读取这个配置信息,修改HelloWorldController 

    @Autowired

    private Environment env;

 

    @RequestMapping("/info")

    public String info(){

        return "HelloWorld~~"+env.getProperty("url");

    }

2.7 热部署

我们在开发中反复修改类、页面等资源,每次修改后都是需要重新启动才生效,这样每次启动都很麻烦,浪费了大量的时间,能不能在我修改代码后不重启就能生效呢?可以,在pom.xml中添加如下配置就可以实现这样的功能,我们称之为热部署。

    <dependency> 

        <groupId>org.springframework.boot</groupId> 

        <artifactId>spring-boot-devtools</artifactId> 

    </dependency> 

赶快试试看吧,是不是很爽。大笑






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324718167&siteId=291194637