欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

java中一些入门级技巧

发布时间:2025/3/20 37 豆豆
生活随笔 收集整理的这篇文章主要介绍了 java中一些入门级技巧 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

为什么80%的码农都做不了架构师?>>>   

1、eclipase中生成自动属性:首先定义好私有字段,然后右键选择source,然后选择Generate Getters and Setters然后出现一个框,选择你需要生成的字段完工。

2、设置固定jdk版本

    <!-- 将运行时环境设置成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>

有时候文件出现乱码,右键这个文件->properties然后可以看到有个编码,选择utf-8即可

 

3、新建maven项目,没有web.xml问题,如果是创建网站,选择war,如果是执行程序则选择jar,web.xml所在的路径为src/main/webapp/WEB-INF/web.xml

 

4、eclipse的git相关教程:https://my.oschina.net/songxinqiang/blog/192567

 

5、lombok便捷工具使用

 

6、热启动模块,有时候项目变更过,需要重新编译启动起来,java有这么个模块,自动编译然后自动启动起来。

        <!-- 热启动模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
 

然后在build的标签下的configuration标签添加一个<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);或者方法后面加一个throws exception

9、try-with-resource来捕获异常,c#释放资源可以使用using关键字,但是java代码中经常看到catch中再来try,一直觉得比较搞笑的写法,现在发现是有语法糖的,来个伪代码吧

FileInputStream fis = null; try {fis = new FileInputStream("aa"); } catch (Exception ex) {if (fis != null) {try {fis.close();} catch (Exception ex1) {throw ex1;}} }

来个语法糖,其实就是try关键字多用,和c#中的using关键字功能一样了

try (FileInputStream fis = new FileInputStream("aa")) {System.out.println("xxxx"); } catch (Exception ex) {throw ex; }

 

转载于:https://my.oschina.net/uwith/blog/1609501

总结

以上是生活随笔为你收集整理的java中一些入门级技巧的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。