spring boot学习入门篇-idea开发简单的hello world实例

最近学习spring Boot,用Idea写了一个简单的实例.

1.idea创建一个完整的maven项目
Image text
Image text
Image text
Image text
Image text
Image text
Image text
项目完整结构:
Image text
2.打开pom.xml添加spring Boot相应的jar包:
Image text
3.新建Application.java存于srcmainjavacomexamplecontroller
Image text
Image text
Image text
@EnableAutoConfiguration和SpringApplication。

@EnableAutoConfiguration用于自动配置。简单的说,它会根据你的pom配置(实际上应该是根据具体的依赖)来判断这是一个什么应用,并创建相应的环境。

在上面这个例子中,@EnableAutoConfiguration会判断出这是一个web应用,所以会创建相应的web环境。

SpringApplication则是用于从main方法启动Spring应用的类。默认,它会执行以下步骤:

创建一个合适的ApplicationContext实例 (取决于classpath)。
注册一个CommandLinePropertySource,以便将命令行参数作为Spring properties。
刷新application context,加载所有单例beans。
激活所有CommandLineRunner beans。

默认,直接使用SpringApplication的静态方法run()即可。但也可以创建实例,并自行配置需要的设置。

开始运行:
Image text
Image text
Image text