Skip to main content

100 posts tagged with "java"

View All Tags

gradle 使用

· 2 min read

背景

spring boot 使用gradle 构建 , 需要了解gradle的使用

下载安装

这里可以下载

配置环境变量

  • windows

解压前文件是

gredle zip

解压后路径:

gradle

gradle envirnment

测试安装成功

$ gradle -version

Welcome to Gradle 7.6!

Here are the highlights of this release:
- Added support for Java 19.
- Introduced `--rerun` flag for individual task rerun.
- Improved dependency block for test suites to be strongly typed.
- Added a pluggable system for Java toolchains provisioning.

For more details see https://docs.gradle.org/7.6/release-notes.html


------------------------------------------------------------
Gradle 7.6
------------------------------------------------------------

Build time: 2022-11-25 13:35:10 UTC
Revision: daece9dbc5b79370cc8e4fd6fe4b2cd400e150a8

Kotlin: 1.7.10
Groovy: 3.0.13
Ant: Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM: 11 (Oracle Corporation 11+28)
OS: Windows 10 10.0 amd64

第一个gradle 项目

来源文档

$ mkdir demo
$ cd demo
$ gradle init
Starting a Gradle Daemon (subsequent builds will be faster)
<-------------> 0% INITIALIZING [2s]77ms] <-------------> 0% INITIALIZING [783ms]<

Select type of project to generate:
1: basic
2: application
3: library
4: Gradle plugin
Enter selection (default: basic) [1..4] 2

Select implementation language:
1: C++ 2: Groovy [11s]]7s]
3: Java
4: Kotlin
5: Scala
6: Swift
Enter selection (default: Java) [1..6] 3

Split functionality across multiple subprojects?:
1: no - only one application project 2: yes - application and library projects
Enter selection (default: no - only one application project) [1..2] 1

Select build script DSL:
1: Groovy 2: Kotlin
Enter selection (default: Groovy) [1..2] 1

Generate build using new APIs and b
Select test framework:
1: JUnit 4 2: TestNG
3: Spock
4: JUnit Jupiter
Enter selection (default: JUnit Jupiter) [1..4] 1

Project name (default: demo):
Source package (default: demo):

> Task :init EGet more help with your project: https://docs.gradle.org/7.6/samples/sample_building_java_applications.html

BUILD SUCCESSFUL in 1m 13s
2 actionable tasks: 2 executed

目录结构

├─.gradle
├─app
│ └─src
│ ├─main
│ │ ├─java
│ │ │ └─demo
│ │ └─resources
│ └─test
│ ├─java
│ │ └─demo
│ └─resources
└─gradle
└─wrapper

编译sping boot

· One min read

背景

编译spring boot

流程

github 主页有写怎么编译

  • 下载代码
## 下载代码
git clone https://github.com/spring-projects/spring-boot.git
## 切换目录
cd spring-boot
## 编译
./gradlew


如果下载国外的包比较慢,可以添加代理

vim build.gradle

编译好的jar包在哪呢? 在每个子模块的build/libs 里面

$ tree spring-boot-project/spring-boot/build/libs/
spring-boot-project/spring-boot/build/libs/
├── spring-boot-3.0.1-SNAPSHOT.jar
└── spring-boot-3.0.1-SNAPSHOT-sources.jar

spring boot 启动

maven 的启动: spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractRunMojo.java

	@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (this.skip) {
getLog().debug("skipping run as per configuration.");
return;
}
String startClass = (this.mainClass != null) ? this.mainClass
: SpringBootApplicationClassFinder.findSingleClass(this.classesDirectory); // 查找main类
run(startClass); // 启动
}

相关阅读

ConcurrentHashMap npe

· One min read

背景

线上遇到ConcurrentHashMap 空指针异常,发现ConcurrentHashMap 不能getput 一个 null的值

spring boot repackage 和入口

· One min read

背景

了解java打包的过程和入口

例子

我工作环境的spring boot jar 包打包后是这样的:

Manifest-Version: 1.0
Created-By: Maven Jar Plugin 3.2.0
Build-Jdk-Spec: 11
Implementation-Title: mdp-biz-engine-rest
Implementation-Version: 3.0.0-SNAPSHOT
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.xxx.Application
Spring-Boot-Version: 2.3.12.RELEASE
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx

相关阅读

java int overflow 探究

· 2 min read

背景

遇到java int overflow的问题,想了解java的数字类型溢出是怎么处理的

jshell> 2147483647 + 1
$3 ==> -2147483648

jls确认规则

对于+操作,如果结果溢出会怎么处理?

数字类型有两部分:符号位数字位 , 对于溢出的数字,规则如下

数字有两部分:

  • 符号位 : 符号位和数学上的结果的符号相反
  • 数字位 : 2进制补码的低位

jls 文档

If an integer addition overflows, then the result is the low-order bits of the
mathematical sum as represented in some sufficiently large two's-complement
format. If overflow occurs, then the sign of the result is not the same as the sign of
the mathematical sum of the two operand values.

例子解释

2147483647 + 1 

这里面 21474836471 都是int 的字面量 , +操作之后会溢出,

10进制的值2147483647 对应的16进制是 7fffffff

扩展之后的值 2147483647 +1 对应的16进制是 ...000 10000000 然后 2147483647 +1 2的补码 ...111 011111111111111 ,

所以: 低位就是 11111111111 符号位: 和之前相反,所以是 1 所以 int 的每一位都是 1 , 所以是 -2147483648

相关阅读

mockito 使用

· One min read

背景

我们新的项目使用mockito来mock数据,所以需要学习mockito的使用

使用

如何使用?

可以去官网https://site.mockito.org/ 查看如何使用

create a maven plugin

· One min read

背景

如何创建maven 扩展

步骤

使用maven创建一个叫hello-maven-plugin 的插件

    mvn archetype:generate \
-DgroupId=sample.plugin \
-DartifactId=hello-maven-plugin \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DarchetypeArtifactId=maven-archetype-plugin

构建的tree

$ tree  .
.
└── hello-maven-plugin
├── pom.xml
└── src
├── it
│ ├── settings.xml
│ └── simple-it
│ ├── pom.xml
│ └── verify.groovy
└── main
└── java
└── sample
└── plugin
└── MyMojo.java

可以看到创建了一个hello-maven-plugin 目录, 其中pom.xml文件

这是核心的pom内容:

<groupId>sample.plugin</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<name>hello-maven-plugin Maven Plugin</name>

相关阅读

spring boot

· 3 min read

背景

需要做到以下几步:

  • 搭建spring boot ,
  • 使用spring boot
  • 打包spring boot

开始

下载spring boot demo ,链接在

https://start.spring.io/

spring boot download

一个可用的例子

https://start.spring.io/#!type=maven-project&language=java&platformVersion=3.0.0&packaging=jar&jvmVersion=17&groupId=com.example&artifactId=demo&name=demo&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.demo

解压

然后下载下来名字叫demo.zip, 然后需要解压

unzip  demo.zip 

安装maven

maven 是java的一个包管理工具

对于ubuntu 来说 ,使用下面的命令安装maven

sudo apt install maven

添加tomcat

pom.xml 添加tomcat相关内容

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

编译成一个fatjar

使用命令 mvn spring-boot:repackage 编译成一个fat-jar

mvn package

启动jar包

命令为java -jar ./target/demo-0.0.1-SNAPSHOT.jar

$ java -jar ./target/demo-0.0.1-SNAPSHOT.jar 

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.0.0)

2022-12-09T00:43:52.343+08:00 INFO 1459280 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT using Java 17.0.5 with PID 1459280 (/home/dai/spring/demo/target/demo-0.0.1-SNAPSHOT.jar started by dai in /home/dai/spring/demo)
2022-12-09T00:43:52.346+08:00 INFO 1459280 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to 1 default profile: "default"
2022-12-09T00:43:53.153+08:00 INFO 1459280 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-12-09T00:43:53.162+08:00 INFO 1459280 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-12-09T00:43:53.163+08:00 INFO 1459280 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.1]
2022-12-09T00:43:53.232+08:00 INFO 1459280 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-12-09T00:43:53.234+08:00 INFO 1459280 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 835 ms
2022-12-09T00:43:53.537+08:00 INFO 1459280 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-12-09T00:43:53.549+08:00 INFO 1459280 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.522 seconds (process running for 1.859)

相关阅读

java redis client

· One min read

背景

背景是需要了解java的redis是怎么使用的

redission

redisson 是java的一个redis客户端

接入spring boot 遇到的问题

spring boot 启动遇到了问题redirection loop detected , 是因为测试环境redis是cluster 模式 , 但是本地配置是单个节点,所以会有问题

org.springframework.dao.InvalidDataAccessApiUsageException: MOVED redirection loop detected. Node redis://10.2.26.106:6379 has further redirect to redis://xxx:6379; nested exception is org.redisson.client.RedisException: MOVED redirection loop detected. Node redis://10.2.26.106:6379 has further redirect to redis://xxx:6379

解决方案 : 在配置文件改成cluster 就可以正常get 和set 了

相关阅读