JiaHe

相遇即是缘

This is a complete guide to Java 8 features, enhancements, date and time API, and coding examples. The examples from this tutorial are tested in our local development environment. You can simply clone from Github and try to use it in your projects or practice.

New Tutorials added (2020)

Key Features of Java 8

In this tutorial, we will learn the following important keys features that came in Java 8:

img

阅读全文 »

https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

Maven 中的依赖作用范围概述

Maven中使用 scope 来指定当前包的依赖范围和依赖的传递性。常见的可选值有:compile, provided, runtime, test, system 等。scope 主要是用在 pom.xml 文件中的依赖定义部分,例如:

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.1.RELEASE</version>
<scope>test</scope>
</dependency>

scope 各种取值详解

scope 取值有效范围(compile, runtime, test)依赖传递例子
compileallspring-core
providedcompile, testservlet-api
runtimeruntime, testJDBC 驱动
testtestJUnit
system(弃用)compile, test
阅读全文 »

一、介绍

/etc/profile

此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行。并从 /etc/profile.d 目录的配置文件中收集 shell 的设置。如果你有对 /etc/profile 有修改的话必须得 source 一下你的修改才会生效,此修改对每个用户都生效。

/etc/bashrc(ubuntu为 /etc/bash.bashrc)

为每一个运行 bash shell 的用户执行此文件。当 bash shell 被打开时,该文件被读取。如果你想对所有的使用 bash 的用户修改某个配置并在以后打开的 bash 都生效的话可以修改这个文件,修改这个文件不用重启,重新打开一个 bash 即可生效。
Ubuntu没有此文件,与之对应的是/ect/bash.bashrc。

~/.bash_profile(ubuntu为 ~/.profile)

每个用户都可使用该文件输入专用于自己使用的 shell 信息,当用户登录时,该文件仅仅执行一次!默认情况下,它设置一些环境变量,执行用户的~/ .bashrc 文件。 此文件类似于 /etc/profile,也是需要需要 source 才会生效,/etc/profile 对所有用户生效,~/.bash_profile 只对当前用户生效。~/.profile(由Bourne Shell和Korn Shell使用)和.login(由C Shell使用)两个文件是.bash_profile的同义词,目的是为了兼容其它Shell。

阅读全文 »

PROBLEM

While migrating a portion of my Java code to Groovy code, I got bitten by the Groovy operator loading feature that I should have known better… and my pride hurts, but hey, I admit I write shitty code.

Consider this simple POGO with custom equals() and hashCode(), both implemented using Google Guava libraries:-

@Canonical
class Person {
String firstName
String lastName
String email

@Override
boolean equals(Object o) {
if (this == o) {
return true
}
if (o == null || getClass() != o.getClass()) {
return false
}

final Person other = (Person) o

return Objects.equal(email, other.email)
}

@Override
int hashCode() {
return Objects.hashCode(email)
}
}

What is wrong with the above code? Well, if you are mostly a Java developer like me, this look pretty much correct. However, when I perform an equality check, I get java.lang.StackOverflowError exception. I tend to see this exception when I write my too-smart-for-production recursion API that couldn’t seem find its way to end the recursion, causing the JVM stack to blow up.

SOLUTION

The reason we are getting java.lang.StackOverflowError exception is because Groovy overloads == with equals(). So, if (this == o) { ... } becomes if (this.equals(o)) { ... }. When we perform an equality check, it will call itself again and again until it chokes itself and dies.

To fix this, we have to use if (this.is(o)) { ... } to perform an identity check:-

@Canonical
class Person {
String firstName
String lastName
String email

@Override
boolean equals(Object o) {
if (this.is(o)) {
return true
}
if (o == null || getClass() != o.getClass()) {
return false
}

final Person other = (Person) o

return Objects.equal(email, other.email)
}

@Override
int hashCode() {
return Objects.hashCode(email)
}
}