java中一些入门级技巧
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
1、eclipase中生成自動(dòng)屬性:首先定義好私有字段,然后右鍵選擇source,然后選擇Generate Getters and Setters然后出現(xiàn)一個(gè)框,選擇你需要生成的字段完工。
2、設(shè)置固定jdk版本
?? ?<!-- 將運(yùn)行時(shí)環(huán)境設(shè)置成1.8 -->
?? ?<properties>
?? ??? ?<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
?? ??? ?<java.version>1.8</java.version>
?? ?</properties>
?? ?<build>
?? ??? ?<finalName>spring</finalName>
?? ??? ?<plugins>
?? ??? ??? ?<!-- jdk編譯插件 -->
?? ??? ??? ?<plugin>
?? ??? ??? ??? ?<groupId>org.apache.maven.plugins</groupId>
?? ??? ??? ??? ?<artifactId>maven-compiler-plugin</artifactId>
?? ??? ??? ??? ?<configuration>
?? ??? ??? ??? ??? ?<source>${java.version}</source>
?? ??? ??? ??? ??? ?<target>${java.version}</target>
?? ??? ??? ??? ?</configuration>
?? ??? ??? ?</plugin>
?? ??? ?</plugins>
有時(shí)候文件出現(xiàn)亂碼,右鍵這個(gè)文件->properties然后可以看到有個(gè)編碼,選擇utf-8即可
?
3、新建maven項(xiàng)目,沒有web.xml問題,如果是創(chuàng)建網(wǎng)站,選擇war,如果是執(zhí)行程序則選擇jar,web.xml所在的路徑為src/main/webapp/WEB-INF/web.xml
?
4、eclipse的git相關(guān)教程:https://my.oschina.net/songxinqiang/blog/192567
?
5、lombok便捷工具使用
?
6、熱啟動(dòng)模塊,有時(shí)候項(xiàng)目變更過,需要重新編譯啟動(dòng)起來,java有這么個(gè)模塊,自動(dòng)編譯然后自動(dòng)啟動(dòng)起來。
?? ??? ?<!-- 熱啟動(dòng)模塊 -->
?? ??? ?<dependency>
?? ??? ??? ?<groupId>org.springframework.boot</groupId>
?? ??? ??? ?<artifactId>spring-boot-devtools</artifactId>
?? ??? ??? ?<optional>true</optional>
?? ??? ?</dependency>
?
然后在build的標(biāo)簽下的configuration標(biāo)簽添加一個(gè)<fork>true</fork>即可,位置如下
?
7、springboot讀取配置文件
方法一
?? ?import org.springframework.core.env.Environment;
?? ?//@Autowired
?? ?//private Environment env;
方法二
?? ?import org.springframework.beans.factory.annotation.Value;
? ? @Value("${jdbc.user}")
? ? private String user;?
讀取自定義配置文件,在類上添加如下注解然后使用方法二來讀取配置即可
@PropertySource("classpath:config/config.properties")
?
8、將異常拋到上層:throw new RuntimeException(e);或者方法后面加一個(gè)throws exception
9、try-with-resource來捕獲異常,c#釋放資源可以使用using關(guān)鍵字,但是java代碼中經(jīng)常看到catch中再來try,一直覺得比較搞笑的寫法,現(xiàn)在發(fā)現(xiàn)是有語法糖的,來個(gè)偽代碼吧
FileInputStream fis = null; try {fis = new FileInputStream("aa"); } catch (Exception ex) {if (fis != null) {try {fis.close();} catch (Exception ex1) {throw ex1;}} }來個(gè)語法糖,其實(shí)就是try關(guān)鍵字多用,和c#中的using關(guān)鍵字功能一樣了
try (FileInputStream fis = new FileInputStream("aa")) {System.out.println("xxxx"); } catch (Exception ex) {throw ex; }?
轉(zhuǎn)載于:https://my.oschina.net/uwith/blog/1609501
總結(jié)
以上是生活随笔為你收集整理的java中一些入门级技巧的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [PHP] 通用网关接口CGI 的运行原
- 下一篇: 你知道socket.io中connect