通常搭建一个基于spring的web应用,我们需要做以下工作:
- pom文件中引入相关jar包,包括spring、springmvc、redis、mybaits、log4j、mysql-connector-java 等等相关jar ...
- 配置web.xml,Listener配置、Filter配置、Servlet配置、log4j配置、error配置 ...
- 配置数据库连接、配置spring事务
- 配置视图解析器
- 开启注解、自动扫描功能
- 配置完成后部署tomcat、启动调试
- ......
而用springboot后,一切都变得很简便快速。
一、springboot的启动类入口
@SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
二、@SpringBootApplication注解分析
SpringBootApplication注解如下:
1 @Target(ElementType.TYPE) //注解的适用范围,其中Type用于描述类、接口或Enum声明 2 @Retention(RetentionPolicy.RUNTIME) //注解的生命周期,保留到Class文件中 3 @Documented //表明这个注解应该被javadoc记录 4 @Inherited //子类可以继承该注解 5 @SpringBootConfiguration //继承了Configuration,表示当前是注解类 6 @EnableAutoConfiguration //开启SpringBoot的注解功能,借助@import的支持,收集和注册依赖包中的bean定义 7 @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), 8 @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) //自动扫描并加载符合条件的组件 9 public @interface SpringBootApplication { 10 11 /** 12 * Exclude specific auto-configuration classes such that they will never be applied. 13 * @return the classes to exclude 14 */ 15 @AliasFor(annotation = EnableAutoConfiguration.class) 16 Class<?>[] exclude() default {}; 17 18 /** 19 * Exclude specific auto-configuration class names such that they will never be 20 * applied. 21 */ 22 @AliasFor(annotation = EnableAutoConfiguration.class) 23 String[] excludeName() default {}; 24 25 /** 26 * Base packages to scan for annotated components. Use {@link #scanBasePackageClasses} 27 * for a type-safe alternative to String-based package names. 28 */ 29 @AliasFor(annotation = ComponentScan.class, attribute = "basePackages") 30 String[] scanBasePackages() default {}; 31 32 /** 33 * Type-safe alternative to {@link #scanBasePackages} for specifying the packages to 34 * scan for annotated components. The package of each class specified will be scanned. 35 */ 36 @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses") 37 Class<?>[] scanBasePackageClasses() default {}; 38 39 /** 40 * The {@link BeanNameGenerator} class to be used for naming detected components 41 * within the Spring container. 42 */ 43 @AliasFor(annotation = ComponentScan.class, attribute = "nameGenerator") 44 Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class; 45 46 /** 47 * Specify whether {@link Bean @Bean} methods should get proxied in order to enforce 48 * bean lifecycle behavior 49 */ 50 @AliasFor(annotation = Configuration.class) 51 boolean proxyBeanMethods() default true; 52 53 }