Skip to main content

编译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

相关阅读

spring boot 基础

· 2 min read

背景

常用的spring boot 问题收集

注解

@Bean@Component 注解优先使用哪个注入

好像得看代码实现,可能和版本有关

bean 和 component 注解优先使用哪个注入

@Component 的使用

@Component 挂在类上面 , @Bean 挂在方法里面,@Bean 更加灵活

[Component ]https://www.baeldung.com/spring-component-annotation

spring boot 启动流程

defineProxyClass:558, Proxy$ProxyBuilder (java.lang.reflect)
build:670, Proxy$ProxyBuilder (java.lang.reflect)
lambda$getProxyConstructor$0:429, Proxy (java.lang.reflect)
apply:-1, Proxy$$Lambda$20/0x0000000800c73468 (java.lang.reflect)
get:329, AbstractClassLoaderValue$Memoizer (jdk.internal.loader)
computeIfAbsent:205, AbstractClassLoaderValue (jdk.internal.loader)
getProxyConstructor:427, Proxy (java.lang.reflect)
newProxyInstance:1037, Proxy (java.lang.reflect)
run:302, AnnotationParser$1 (sun.reflect.annotation)
run:300, AnnotationParser$1 (sun.reflect.annotation)
executePrivileged:776, AccessController (java.security)
doPrivileged:318, AccessController (java.security)
annotationForMap:300, AnnotationParser (sun.reflect.annotation)
parseAnnotation2:289, AnnotationParser (sun.reflect.annotation)
parseAnnotations2:121, AnnotationParser (sun.reflect.annotation)
parseAnnotations:73, AnnotationParser (sun.reflect.annotation)
createAnnotationData:4068, Class (java.lang)
annotationData:4057, Class (java.lang)
getDeclaredAnnotations:4024, Class (java.lang)
getDeclaredAnnotations:449, AnnotationsScanner (org.springframework.core.annotation)
isKnownEmpty:489, AnnotationsScanner (org.springframework.core.annotation)
from:259, TypeMappedAnnotations (org.springframework.core.annotation)
from:371, MergedAnnotations (org.springframework.core.annotation)
from:359, MergedAnnotations (org.springframework.core.annotation)
from:339, MergedAnnotations (org.springframework.core.annotation)
<init>:86, StandardAnnotationMetadata (org.springframework.core.type)
from:173, StandardAnnotationMetadata (org.springframework.core.type)
introspect:134, AnnotationMetadata (org.springframework.core.type)
<init>:58, AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation)
doRegisterBean:253, AnnotatedBeanDefinitionReader (org.springframework.context.annotation)
registerBean:147, AnnotatedBeanDefinitionReader (org.springframework.context.annotation)
register:137, AnnotatedBeanDefinitionReader (org.springframework.context.annotation)
load:161, BeanDefinitionLoader (org.springframework.boot)
load:136, BeanDefinitionLoader (org.springframework.boot)
load:129, BeanDefinitionLoader (org.springframework.boot)
load:698, SpringApplication (org.springframework.boot)
prepareContext:429, SpringApplication (org.springframework.boot)
run:322, SpringApplication (org.springframework.boot)
run:1342, SpringApplication (org.springframework.boot)
run:1331, SpringApplication (org.springframework.boot)
main:11, DemoApplication (com.example.demo)

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)

相关阅读