Create a simple spring boot project

Brief introduction

Use spring boot can easily create stand-alone, production-level applications Spring Framework. Spring boot application requires very little configuration of spring

Feature

  • Create stand-alone applications Spring
  • Directly embedded tomcat
  • Providing starter dependencies simplify the build configuration
  • Spring-party libraries and auto-configuration as possible
  • Provide production-ready features, such as indicators, health checks and external configuration
  • No code generation and does not require XML configuration

Set out

Next, how to use simple idea to create a new Spring Boot project

  • New Project

Create a project

  • Enter the project name and package name

Enter the project name and package name

  • Edit pom.xml

    <!-- 将项目打包成jar -->
    <packaging>jar</packaging>
  • Dependencies

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    
    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
  • View startup class

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

    @ SpringBootApplication = (default property) @Configuration + @EnableAutoConfiguration + @ComponentScan.

    1, @ Configuration: @Configuration mentioned we should mention his partner @Bean. These two notes can create a simple spring configuration class can be used to replace the corresponding xml configuration files.

    <beans> 
        <bean id = "car" class="com.test.Car"> 
            <property name="wheel" ref = "wheel"></property> 
        </bean> 
        <bean id = "wheel" class="com.test.Wheel"></bean> 
    </beans>

    Equivalent to

    @Configuration 
    public class Conf { 
        @Bean 
        public Car car() { 
            Car car = new Car(); 
            car.setWheel(wheel()); 
            return car; 
        } 
        @Bean  
        public Wheel wheel() { 
            return new Wheel(); 
        } 
    }

    @Configuration annotation identifies the class type may be used as a source Spring IoC container bean definition. @Bean annotation tells Spring, annotated with a @Bean method returns an object that should be registered as a bean in the Spring application context.

    2, @ EnableAutoConfiguration: can automatically configure the context of the spring, trying to guess what you want and configure the bean class, usually automatically configured automatically based on your class path and your bean definitions.

    3, @ComponentScan: automatically scans all designated under the package marked with @Component class and registered as bean, of course, including sub under @Component notes @ Service, @ Repository, @ Controller. (Note: By default only the current scan Bean in the package and its subpackages)

  • Controller Editor

    Create a HelloWorldController

    @RestController
    public class HelloWorldController {
        @RequestMapping("hello")
        String hello(){
            return "hello world";
        }
    }
  • Bale

    mvn clean package
  • start up

    ## 后台运行
    nohup java -jar **.jar &
    ## maven方式运行
    mvn spring-boot:run
  • access

Guess you like

Origin www.cnblogs.com/missj/p/12176558.html