一、主程序类,SpringBoot项目的入口
eg:
/**
* @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
*/
@SpringBootApplication
public class hello {
// Spring应用启动起来,run方法的参数只能是@SpringBootApplication注解标注的类
public static void main(String[] args) throws Exception{
SpringApplication.run(hello.class,args);
}
}
二、关于 @SpringBootApplication
@SpringBootApplication 是一个组合注解,详细见下文代码:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
1.注解@SpringBootConfiguration:表示SpringBoot的配置类,实际上也是一个组合类,底层是由Spring框架的配置注解@Configuration等注解组合而成。标注有@Configuration的类就属于配置类,可替代配置文件,而实际上配置类也是容器中的一个组件,底层标注了@Component。
2.注解@EnableAutoConfiguration:表示开启自动配置功能;即以前需要我们程序员自己配置的功能,均可交给SpringBoot帮我们配置。它下面的底层注解@AutoConfigurationPackage(自动配置包)可以将主配置类,即启动类所在的包之下的所有的子包及其所有的组件全部扫描到Spring容器中。也因此,一般情况我们都将启动类放在项目中最外层的包下。
3.注解@ComponentScan:会自动扫描包路径下面的所有@Controller、@Service、@Repository、@Component 的类。
综上所诉@SpringBootApplication=@SpringBootConfiguration+@EnableAutoConfiguration+@ComponentScan。
三、使用idea快速生成一个SpringBoot项目(必须是联网状态下)
网上有很多教程,此处不详述。此处大致说一下resources文件夹中目录结构。
--resources:主要放置配置文件
--static:保存所有的静态资源; js css images;
--templates:保存所有的模板,页面(html);(SpringBoot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面);可以使用模板引擎(freemarker、thymeleaf);
--application.properties:SpringBoot应用的配置文件;可以修改一些默认设置;