springboot 知识点

1springBoot项目映入方式,
1,集成父 project
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
2,写dependencyManagement
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.8.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

2 springBoot 的启动
1 SpringApplication application = new SpringApplication();

Set<Object> set = new HashSet<>();
set.add(Main.class );
application.setSources(set);
ConfigurableApplicationContext context = application.run(args);

2 ConfigurableApplicationContext context = SpringApplication.run(Main.class, args);

3 springBoot 的启动 会把 Sources 当做 配置类,相当有了 @SpringBootConfiguration 注解,所以可以不写@SpringBootConfiguration

4 springBoot 的默认配置文件是 application.properties

4 springBoot 获取 配置文件的三种方式
1,通过context 回去环境对象
context.getEnvironment().getProperty("local.port")

2,注入环境对象
@Autowired
private Environment environment;

3,使用 @Value("${local.ip}") 在属性上面注入
备注: value的值 格式 ${key}


5 springBoot propertise 的 配置 支持 内部引用 (可以直接在value 中通过表达式接近应用key 对应的 value )
local.ip=127.0.0.1
local.port=8080
local.ipAndPort=${local.ip}:${local.port}


6 配置文件去默认值(如果key 不存在,且不设置默认值或报错) 但是通过 getEnvironment().getProperty("local.def","000000") 这种方式获取不会报错,只会返回null
${key:默认值} (在没有这个key 的时候生效)

例子:@Value("${local.df:99}") (local.df不存在)

7 修改配置文件的位置
默认配置文件才classpath: 根目录 或者 classpath:config/ 目录

8 修改配置文件名
1 修改main 方法启动参数 --spring.config.name=配置文件名(不要拓展名)

2 修改任意 的 路径和文件名 --spring.config.location=classpath:conf/app.properties

3 可以是 file:协议的地址 --spring.config.location=file:D:\eclipsej2ee\springBoot\src\main\resources\conf\app.properties

4 可以有多个 配置文件(逗号隔开) -spring.config.location=file:path,classpath:path2,,classpath:path2

5 可以通过 @PropertySource("classpath:conf/app.properties") 注解方式指定 配置文件 的路径
注:@PropertySources 可以写多个 @PropertySource
注2:可以直接写多个 @PropertySource


6 使用 @ConfigurationProperties(prefix="local") 直接指定 配置文件
注:若果 key 没有 前缀,那么不配置前缀就可以

9,springBoot 的 配置文件可以使yml

10 ,注入集合或者数组 (亲测,必要指定泛型类型),不能是set
key[0]=value0
key[1]=value1
key[2]=value2

11 通过编程方式动态指定 配置文件,需要实现 EnvironmentPostProcessor 接口 并且 在 META-INF/spring.factories 中注册
spring.factories --> org.springframework.boot.env.EnvironmentPostProcessor=com.zyk.springBoot.main.MyEnvironmentPostProcessor

代码实现 environment.getPropertySources().addLast(propertySource); propertySource好像就是以前spring 的placeHoder


spring的 profile


12 指定profile
默认会加载application.properties 设置类 app.setAdditionalProfiles("test"); 会额外加载 application-test.properties,如果 test环境配置和 默认环境配置有key重复,那么 用额外的,如果是数组,不会合并,而是只用额外环境

备注:setAdditionalProfiles 设置额外配置文件 (可以添加多个额外配置)
备注2:--spring.profiles.active=test 也可以设置环境(额外激活)
备注3:--spring.profiles=test 仅仅激活test,不激活默认的环境

13 @Profile 修饰的 Bean 只有在指定环境面 才会生效 (可以指定在类型前面)

spring的 Condition


14 Condition 接口配合 @Conditional 注解使用,修饰@Bean 只有条件成立才,spring才会 调用@Bean 的的方法,

自定义Condition 例子:
@Bean
@Conditional(MyCondition1.class)
public String gbk(){
return "GBK";
}

@Bean
@Conditional(MyCondition2.class)
public String utf(){
return "utf-8";
}

只有MyCondition2.class 接口函数的 方法放回true 的时候才会产生

spring 提供大量默认的Condition注解
备注1:@Conditional 成立需要全部条件 true

备注2:@Conditional 有大量 类似 @ConditionalOn* 的注解,只有单满足 的时候才转配 @Bean
@ConditionalOnBean
@ConditionalOnClass
@ConditionalOnJava
@ConditionalOnExpression
@ConditionalOnNotWebApplication
@ConditionalOnSingleCandidate



spring @Enable*注解
1,@EnableAsync 注解开启后 使用 异步运行后, @Async 修饰的方法 会异步执行(不是代理对象,估计spring是直接改了 这个方法的二进制码 )

15 @Import 导入一个需要spring管理的 类 (可以是配置类或者是普通bean)

注:@Import 可以指向 ImportSelector 接口的实现类,在接口函数中指定 导入的类(接口与方法返回值是 类名全称字符数组)
注2: 可以写 ImportBeanDefinitionRegistrar 的 实现类 用来注册bean

16 spring注解的特点,任意一个自定义注解写入spring 配置类里面,那么这个自定义注解的全部父注解(spring认识的)都会生效 (N层都生效)

17 @EnableAutoConfiguration 的作用 (classpath 下面的 META-INF 里面 spring 工厂文件里面的接口对应的 值都加入到 spring 管理)
1,可以在 META-INF 中写上 org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.zyk.springBoot.Parent
这样 spring会把 Parent 加入spring管理(如果 Parent 是 类加入管理,如果 Parent 是配置 同样),并且上面这种写法的申明的配置 可以被另一的jar(别的项目模块)包使用

注2: 这个注解可以通过 spring.boot.enableautoconfiguration=ture/false 启用或者关闭(可以卸载 application.properties 配置文件 或者 args )

18 springBoot 的事件监听 需要继承 ApplicationEvent ,实现 MyApplicationListener ,需要先注册监听器,然后发布事件
例子:
SpringApplication app = new SpringApplication(Main.class);
app.addListeners(new MyApplicationListener());
ConfigurableApplicationContext context = app.run(args);


启用这个监听器需要启用才能接受事件,启动的方式可以通过
1 任意方式吧监听器加入 spring 容器中 (当做普通bean 加入就可以)
2 在 SpringApplication app = new SpringApplication(Main.class); app.addListeners(new MyApplicationListener());
3 在 配置文件 application.properties 中写入 context.listener.classes=com.zyk.springBoot.event.MyApplicationListener (这种方法和 DelegatingApplicationListener 有关)
4 可以 不实现 ApplicationListener 直接在一个方法中写上注解 @EventListener (参数决定可以接受什么类型的 事件 ,参数类型任意,Object 可以接受任何类型 ) (和 EventListenerMethodProcessor 这个 处理进程类有关 )
@EventListener
public void listener1( MyApplicationEvent event){
System.out.println( " 直接接收: " + event.getSource() );
}

5 在工厂配置文件里面 去 org.springframework.boot.SpringApplicationRunListener=。。。。。




19 springBoot 的拓展分析

是一种方式: 实现 接口 ApplicationContextInitializer ,然后在 加入到容器 springApplication.addInitializers( new MyApplicationContextInitializer()); 这个接口函数中有 springcontext 可以在容器启动的 过程中做一些事情

备注1: 不能直接加入到spring容器(应为这个很早,不想 事件监听器)
备注2: 这时候容器管理的类好没有加载完毕,不能取到spring管理的类(只有最初始的6个类)

第二种方式 配置文件配置 context.initializer.classes=com.zyk.springBoot.event.MyApplicationContextInitializer

第三种: 写在工厂配置文件里面 去 org.springframework.context.ApplicationContextInitializer=。。。。。



20 CommandLineRunner在容器成功启动的最后一步回掉 (比ApplicationContextInitializer 靠后 ),这时候容器已经启动

1备注直接加入容器就生效
2@Order(100) 修改顺序 (数字大的后执行)


21 ApplicationRunner 类似( ApplicationRunner 的 参数 是 ApplicationArguments 的封装) ,CommandLineRunner 取得main的args 的原值


22 关闭 springBoot 的 banner app.setBannerMode(Banner.Mode.OFF);

23 @SpringBootApplication 默认扫描的是当前包,和子包 可以通过 scanBasePackages指定扫描包

24 自定义 banner 写一个banner.txt 位于classpath 就会制动 使用这个文件里面的 banner


25 可以通过 在配置文件里面写入 banner.location 指定banner 位置 banner.cherset 指定编码

26 spring 支出 图片的banner 只要命名为 banner.图片格式就可以

27 banner.img.location 指定位置


springBoot 的运行流程分析
没做记录

springBoot web方面

28 @RestController 和@Controller 类似,区别在于相当于在每个 方法都写上 @ResponseBody (springWeb 带有的 不是 springBoot独有) (4.0以后)

29 提供了大量的 @GetMapping 之类的注解用来代替 @RequestMapping 每次需要指定 method (4.3 以后)

30 springBoot 指定前缀和后缀 (springboot 默认不支持jsp ,需要映入 tomcat-embed-jasper 才能支持)
spring.mvc.view.prefix=/WEB_INF/jsp
spring.mvc.view.suffix=.jsp

31 指定端口 server.port=8080

32 对于参数的传递到 页面视图传统方式是通过 在reques 里面设置属性,现在可以通过 Model 或者 ModelAndView

33 springBoot 使用 freemarker (测试没有成功,)

34 访问静态资源 和 ResourceProperties 有关,里面定义了 那些目录可以直接访问
spring.resources.staticLocations=.... 可以修改


35 在 SpringBootApplication 上使用@ServletComponentScan 注解后,Servlet、Filter、Listener 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册,无需其他代码。这种仅支持servlet3.0以后

或者 通过 ServletRegistrationBean (这种可以支持servlet2.5以前)
@Bean
public ServletRegistrationBean servletRegistrationBean() {
return new ServletRegistrationBean(new MyServlet(), "/xs/*");
}

备注 :还有FilterRegistrationBean ServletListenerRegistrationBean

36 使用 jetty 启动 ,需要排除 tomcat 然后 加入 (不排除好像也可以)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

spring-boot-autoconfigure/1.5.8.RELEASE/spring-boot-autoconfigure-1.5.8.RELEASE-sources.jar 里面有很多的配置可以参考


37 springBoot 里面使用拦截器 (HandlerInterceptor,WebMvcConfigurerAdapter)
步骤如下:
1 写一个 类实现 HandlerInterceptor
2 写一个class 集成 WebMvcConfigurerAdapter
3 在 WebMvcConfigurerAdapter 的 方法中加入 添加 拦截器

38 自定义异常处理

1 方式1 (springBoot 才有)
实现 ErrorPageRegistrar(需要加入到spring管理) (ErrorPage 对象可以对应 不同状态码,不同异常跳转到不同地址(这个地址可以是页面或者action) )
在 ErrorPageRegistrar 里面添加 ErrorPage 对应的异常和异常处理地址的关系

方式 2 使用 SimpleMappingExceptionResolver 类,并且加入到 spring管理

方式 3 使用 实现 HandlerExceptionResolver 接口 ,并且加入到spring管理

方式4 @ExceptionHandler 注解方式 ( 默认只能处理当前controller 里面你的异常,在类前面 加上 @ControllerAdvice可以处理全部异常,优先级低于类同控制类里面的定义的 )


39 @ControllerAdvice 的作用

把@ControllerAdvice注解内部使用@ExceptionHandler、@InitBinder、@ModelAttribute注解的方法,应用到所有的 @RequestMapping注解的方法。不过只有当使用@ExceptionHandler最有用,另外两个用处不大。

40 继承 EmbeddedServletContainerCustomizer 可以 对 servlet 容器的启动进行设置
方法2 直接在@Bean 上返回一个 EmbeddedServletContainerCustomizer 对象

41 实现 AbstractRoutingDataSource 可以 对spring 数据源进行切换 (spring 会去这里面取数据源 )










猜你喜欢

转载自www.cnblogs.com/cxygg/p/9291305.html
今日推荐