springboot学习笔记(六)
@ImportResource注解
? ? 作用:
? ? ? ? ? ?spring boot會(huì) 默認(rèn)的 自動(dòng)的 將spring等的配置文件配置好。
? ? ? ? ? ?但是如果要自己編寫spring等配置文件,spring boot默認(rèn)不能識(shí)別。
? ? ? ? ? ?如果需要識(shí)別,則需要在springboot主配置類上使用該注解指定配置文件的路徑。(但是不推薦手寫配置文件)
示例:
手動(dòng)編寫spring配置文件,并配置一個(gè)bean
測(cè)試能不能獲取到:會(huì)提示沒有這個(gè)bean
但是在主配置類加上@ImportResource注解之后,繼續(xù)測(cè)試
測(cè)試方法:
獲取成功
?
推薦的配置方式:xml配置文件,通過(guò)注解配置
? ? ? ? ? ?springboot推薦使用注解方式(配置類的方式)進(jìn)行配置:
? ? ? ? ? ? ? ? @Configuration? :聲明這是一個(gè)配置類
? ? ? ? ? ? ? ? @Bean:配置一個(gè)bean就加一個(gè)@Bean
示例:(這個(gè)小示例不知道為什么有一個(gè)小bug,等調(diào)試成功之后再更新)
首先編寫一個(gè)配置類
package com.example.conf;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;import com.example.service.StudentService;@Configuration//聲明該類是一個(gè)配置類 public class StudentConf {@Bean//配置一個(gè)beanpublic StudentService stuService2() {StudentService studentService = new StudentService();return studentService;} }?
springboot全局配置文件中的占位符、表達(dá)式
隨機(jī)占位符:
| ${random.uuid} | uuid |
| ${random.int} | 隨機(jī)整數(shù) |
| ${random.value} | 隨機(jī)字符串 |
| ${random.long} | 隨機(jī)長(zhǎng)整形數(shù) |
| ${random.int(10)} | 10以內(nèi)整數(shù) |
| ${random.int[1024,65536]} | 指定隨機(jī)數(shù)范圍 |
使用方式------直接在默認(rèn)配置文件中使用即可?:
測(cè)試:
引用變量值
兩個(gè)默認(rèn)的配置文件之間可以相互引用
例如:在application.properties中可以引用application.yml中的值
示例:
application.properties文件:
?application.yml文件中引用application.properties的值:
?
總結(jié)
以上是生活随笔為你收集整理的springboot学习笔记(六)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 面试题,产品方案开发说实现不了,作为产品
- 下一篇: 为何boss上的HR要了简历就不回复了?