spring boot学习一:intellij环境搭建

1.启动intellij,选择【Create New Project】



 2.选择【Spring Initializr】



 3.填写相关的项目信息



 4.选择需要依赖的模块,此处入门示例,只需要选择Web模块



 5.填写项目名称相关信息



 6.点击【Finish】后生成的项目架构如下图所示



 7.在默认提供的入口类中,修改相关代码,加入请求映射注解

package com.flagship.ch1_1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class Ch11Application {

	@RequestMapping("/")
	String index(){
		return "hello world,spring boot";
	}

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

 8.点击【Debug Ch11Application】



 9.启动成功后,浏览器访问:http://localhost:8080/,出现如下信息,表示启动成功



 10.代码解释:

上述Ch11Application类的示例代码中,用到的注解,解释如下

@RestController:可以看作@ResponseBody和@Controller的组合注解

@RequestMapping:用来响应url中"/"的请求映射

@SpringBootApplication:@Configuration,@EnableAutoConfiguration,@ComponentScan的组合注解,其中,@Configuration标识这个类可以使用Spring IoC容器作为bean定义的来源,@EnableAutoConfiguration会使Spring Boot根据jar包依赖对当前项目进行自动配置,@ComponentScan自动扫描指定包下的全部标有@Component的类(包括其子类),注册成bean

猜你喜欢

转载自yyddbull.iteye.com/blog/2394265