spring boot entry helloWord simple example

1. Create a new project springboot

You can see here https://blog.csdn.net/qq_43560721/article/details/104653470

2. Start writing simple helloWord

Create a new controller

package com.example.officialwebsite.controller;

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

@RestController
public class TestController{
    @RequestMapping("/hello")
    public String hello(){
        return "hello word spring boot!";
    }
}

Note startup class to add scanning package

package com.example.officialwebsite;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.example.officialwebsite.*")
public class OfficialwebsiteApplication {

    public static void main(String[] args) {
        SpringApplication.run(OfficialwebsiteApplication.class, args);
        System.out.println("哈哈*=*");

    }

}

3. Run Project

Enter the browser: HTTP: // localhost: 8888 / the Hello

 

 

+ = @Controller @ResponseBody @RestController
@RequestMapping: Request for Comments for address mapping process, can be used for class or method. Can also be written GetMapping, PostMapping, PutMapping, DeleteMapping, PatchMapping

 

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

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

@EnableAutoConfiguration: can automatically configure context 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.

@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.

 

 

Published 141 original articles · won praise 33 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_43560721/article/details/104669851