深入学习--SpringBoot
一、@SpringBootApplication源码
@SpringBootApplication注解是Spring Boot的核心注解,它其实是一个组合注解
@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.1 @SpringBootConfiguration--是一个组合注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}
小结:在里面我们看到了@Configuration,所以在以后的注解中,我们使用@SpringBootConfiguration来代替@Configuration
1.2 @EnableAutoConfiguration--自动配置
@SuppressWarnings("deprecation")
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
....................
}
小结:启用自动配置,该注解会使Spring Boot根据项目中依赖的jar包自动配置项目的配置项,我们添加了spring-boot-starter-web的依赖,项目中也就会引入SpringMVC的依赖,Spring Boot就会自动配置tomcat和SpringMVC
二、SpringBoot简单配置以及图案配置
2.1 关闭 SpringBoot的自动配置
查看工程中JAR包
spring-boot-autoconfigure-1.5.2.RELEASE.jar里面有很多自动配置的JAR包
例如:我们不想用sole(搜索框架)的自动配置
@SpringBootApplication(exclude= {SolrAutoConfiguration.class})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2.2 启动时的图案--Spring》》
让我们来看一张图片:相信大家很熟悉
如何替换呢?
找到网址:
http://patorjk.com/software/taag/#p=display&h=3&v=3&f=4Max&t=itcast%20Spring%20Boot
resource下新建banner.txt-->在网址中输入想要的字符(LOVE)-->复制字符到banner.txt-->重新启动
打印效果:
88 dP"Yb Yb dP 888888 ________ ________ 88 dP Yb Yb dP 88__ ________ ________ """""""" """""""" 88 .o Yb dP YbdP 88"" """""""" """""""" 88ood8 YbodP YP 888888
2.3 关闭启动的图案--Spring》》
调用setBanberModel方法
public class Application {
public static void main(String[] args) {
SpringApplication spr=new SpringApplication(Application.class);
spr.setBannerMode(Banner.Mode.OFF);
spr.run(args);
}
}
三、application.properties文件
3.1 目录
3.2 例:配置端口号
在application.properties文件写上修改tomcat的端口为8945
server.port=8945
运行后结果。
访问地址为:
http://localhost:8945/boot
3.3 更多配置(有点多。。上一部分)
# BANNER
banner.charset=UTF-8 # Banner file encoding.
banner.location=classpath:banner.txt # Banner file location.
# LOGGING
logging.file= # Log file name. For instance `myapp.log`
# AOP
spring.aop.auto=true # Add @EnableAspectJAutoProxy.
# Email (MailProperties)
spring.mail.default-encoding=UTF-8 # Default MimeMessage encoding.
spring.mail.host= # SMTP server host. For instance `smtp.example.com`
# SPRING MVC (WebMvcProperties)
spring.mvc.view.prefix= # Spring MVC view prefix.
spring.mvc.view.suffix= # Spring MVC view suffix.
# SPRING SESSION (SessionProperties)
spring.session.hazelcast.flush-mode=on-save # Sessions flush mode.
spring.session.hazelcast.map-name=spring:session:sessions # Name of the map used to store sessions.
# DAO (PersistenceExceptionTranslationAutoConfiguration)
spring.dao.exceptiontranslation.enabled=true # Enable the PersistenceExceptionTranslationPostProcessor.
spring.datasource.name=testdb # Name of the datasource.
spring.datasource.password= # Login password of the database.
四、xml+log4j
4.1 xml--特殊配置文件
在SpringBoot中基本用不倒配置文件,在特殊情况下,也可以配置
@ImportResource({"classpath:one.xml","classpath:two.xml"})
4.2 日志
logging.level.org.springframework=DEBUG
logging.level.*=
# Log levels severity mapping.
For instance`logging.level.org.springframework=DEBUG`
欢迎订阅关注公众号(JAVA和人工智能)
获取更多免费书籍、资源、视频资料