javascript
SpringBoot自学汇总
啟動彩蛋修改:
項目resources目錄下建立banner.txt文件就可替換原來的菜單
字符畫生成的網(wǎng)站http://www.network-science.de/ascii/ http://patorjk.com/software/taag/
切換不同環(huán)境配置
在idea 啟動配置program arguments加上–spring.profiles.active={profile},或在dos行加上–spring.profiles.active={profile};
或配置文件spring.profiles.active={profile}
各個環(huán)境公共的配置寫在application.properties中
各個模塊獨有的配置配置在自己的application-{xxx}.properties文件中
程序讀取的時候優(yōu)先讀取application.properties中選中的profile的配置,若讀不到才會從application.properties去讀
讀取配置
必須先@Component ?然后參數(shù)@Value("${cusvar}"
@Value("${app.name}")
private String cusvar ; 將${app.name}值賦予cusvar
name= HowieLi
age= 18
content= "name: ${name}, age: ${age}"
代碼中直接調(diào)用content就可以了,訪問啟動的應(yīng)用顯示name: HowieLi, age: 18。
@RestController該注解是Spring4之后新加的注解,等同于@Controller和@ResponseBody的組合。
@RequestMapping(value = "/hello", method = RequestMethod.GET)== @GetMapping("/hello")
@RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.GET)訪問/hello和/hi是一樣的效果
@GetMapping(value = "/say/{id}")
public String helloGet(@PathVariable("id") int id, @RequestParam("name") String name) {return "id: " + ?id + ",name:" + name;}訪問http://localhost:8080/say/5?name=howieli
獲得配置文件值
Preferences -> Editor -> General -> Appearance, uncheck "Show Spring Boot metadata panel"解決Spring Boot Configuration Annotation Processor not found in classpath
Usage of API documented as @since 1.6+ This inspection finds all usages of methods that have @since tag in their documentation. This may be useful when development is performed under newer SDK version as the target platform for production解決方法
File ->Project Structure->Project Settings -> Modules -> 你的Module名字 -> Sources -> Language Level->選個默認的就行。
1.5不支持diamond運算符,請使用source 7或更高版本以啟用diamond運算符,怎么辦?
? <properties>加上
? ? <maven.compiler.source>1.8</maven.compiler.source>
? ? <maven.compiler.target>1.8</maven.compiler.target>或
? ? <build>
? ? ?<plugins>
? ? ? <plugin>
? ? ? ? <groupId>org.apache.maven.plugins</groupId>
? ? ? ? <artifactId>maven-compiler-plugin</artifactId>
? ? ? ? <version>3.6.1</version>
? ? ? ? <configuration>
? ? ? ? ? <source>1.8</source>
? ? ? ? ? <target>1.8</target>
? ? ? ? </configuration>
? ? ? </plugin>
? ? ?</plugins>
? ? </build>
** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.?因為application.java 文件不能直接放在main/java文件夾下,必須要建一個包把他放進去
熱部署參看http://www.cnblogs.com/java-zhao/p/5502398.html
http://blog.csdn.net/jsshaojinjie/article/details/64125458
? <!-- 用于將應(yīng)用打成可直接運行的jar(該jar就是用于生產(chǎn)環(huán)境中的jar) 值得注意的是,如果沒有引用spring-boot-starter-parent做parent, 且采用了上述的第二種方式,這里也要做出相應(yīng)的改動 -->
<plugin>
? <groupId>org.springframework.boot</groupId>
? <artifactId>spring-boot-maven-plugin</artifactId>
? <configuration>
? ? <fork>true</fork><!-- 如果沒有該項配置,肯呢個devtools不會起作用,即應(yīng)用不會restart -->
? </configuration>
</plugin>
<!-- devtools可以實現(xiàn)頁面熱部署(即頁面修改后會立即生效,這個可以直接在application.properties文件中配置spring.thymeleaf.cache=false來實現(xiàn)),實現(xiàn)類文件熱部署(類文件修改后不會立即生效),實現(xiàn)對屬性文件的熱部署。即devtools會監(jiān)聽classpath下的文件變動,并且會立即重啟應(yīng)用(發(fā)生在保存時機),注意:因為其采用的虛擬機機制,該項重啟是很快的-->
<dependency> ? ? ? ? ?
?<groupId>org.springframework.boot</groupId>
?<artifactId>spring-boot-devtools</artifactId>
?<optional>true</optional><!-- optional=true,依賴不會傳遞,該項目依賴devtools;之后依賴myboot項目的項目如果想要使用devtools,需要重新引入 -->
</dependency>
CTRL + alt + s --> 查找make project automatically --> 選中?
ctrl+shift+alt+/ --> 查找Registry --> 找到并勾選compiler.automake.allow.when.app.running?
關(guān)閉熱部署spring.devtools.restart.enabled 屬性為false
System.setProperty("spring.devtools.restart.enabled","false");
?public static void main(String[] args) throws IOException {
? ?Properties properties= new Properties();
? ? ?//System.out.println(System.getProperty("user.dir"));
? ? ?//自己設(shè)置文件位置
? ? ?//InputStream in = new FileInputStream(System.getProperty("user.dir")
? ? ?//+"/isoft-manager/isoft-manager-pojo/app.properties");
? ? ?//Resources位置
? ? ?InputStream in=Application.class.getClassLoader().getResourceAsStream("app"
+ ".properties");
? ? ?properties.load(in);
? ? ?SpringApplication app=new SpringApplication(Application.class);
? ? ?app.setDefaultProperties(properties);
? ? ?app.run(args);
? ?}使用自己的啟動設(shè)置
使用fastjson
@Bean public?HttpMessageConverters?fastJsonHttpMessageConverters(){//1.需要定義一個convert轉(zhuǎn)換消息的對象;FastJsonHttpMessageConverter?fastJsonHttpMessageConverter?=?new?FastJsonHttpMessageConverter();//2:添加fastJson的配置信息;FastJsonConfig?fastJsonConfig?=?new?FastJsonConfig();fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);//3處理中文亂碼問題List<MediaType>?fastMediaTypes?=?new?ArrayList<MediaType>();fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);//4.在convert中添加配置信息.fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);HttpMessageConverter<?>?converter?=?fastJsonHttpMessageConverter;return?new?HttpMessageConverters(converter);}http://blog.csdn.net/xiaoyu411502/article/details/48049099application.properties配置說明
本文轉(zhuǎn)自whshurk 51CTO博客,原文鏈接:http://blog.51cto.com/shurk/1943959,如需轉(zhuǎn)載請自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的SpringBoot自学汇总的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RODC 的作用
- 下一篇: 高效的 JavaScript