File tree 8 files changed +74
-39
lines changed
springboot-starter-id-generator/src
main/java/com/codingapi/springboot/generator
java/com/codingapi/springboot/generator
8 files changed +74
-39
lines changed Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ public GeneratorProperties generatorProperties() {
20
20
@ ConditionalOnMissingBean
21
21
public IdKeyDao idKeyDao (GeneratorProperties generatorProperties ) {
22
22
IdKeyDao keyDao = new IdKeyDao (generatorProperties .getJdbcUrl ());
23
- GeneratorContext .getInstance ().init (keyDao );
23
+ IdGeneratorContext .getInstance ().init (keyDao );
24
24
return keyDao ;
25
25
}
26
26
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 3
3
public interface IdGenerate {
4
4
5
5
default long generateLongId () {
6
- return GeneratorContext .getInstance ().generateId (getClass ());
6
+ return IdGeneratorContext .getInstance ().generateId (getClass ());
7
7
}
8
8
9
9
default int generateIntId () {
10
- return (int ) GeneratorContext .getInstance ().generateId (getClass ());
10
+ return (int ) IdGeneratorContext .getInstance ().generateId (getClass ());
11
11
}
12
12
13
13
default String generateStringId () {
14
- return String .valueOf (GeneratorContext .getInstance ().generateId (getClass ()));
14
+ return String .valueOf (IdGeneratorContext .getInstance ().generateId (getClass ()));
15
15
}
16
16
17
17
}
Original file line number Diff line number Diff line change
1
+ package com .codingapi .springboot .generator ;
2
+
3
+ import com .codingapi .springboot .generator .dao .IdKeyDao ;
4
+ import com .codingapi .springboot .generator .service .IdGenerateService ;
5
+
6
+ public class IdGeneratorContext {
7
+
8
+ private static IdGeneratorContext instance ;
9
+ private IdGenerateService idGenerateService ;
10
+
11
+ private IdGeneratorContext () {
12
+ }
13
+
14
+ public static IdGeneratorContext getInstance () {
15
+ if (instance == null ) {
16
+ synchronized (IdGeneratorContext .class ) {
17
+ if (instance == null ) {
18
+ instance = new IdGeneratorContext ();
19
+ }
20
+ }
21
+ }
22
+ return instance ;
23
+ }
24
+
25
+ long generateId (Class <?> clazz ) {
26
+ return idGenerateService .generateId (clazz );
27
+ }
28
+
29
+ void init (IdKeyDao idKeyDao ) {
30
+ idGenerateService = new IdGenerateService (idKeyDao );
31
+ }
32
+
33
+ }
Original file line number Diff line number Diff line change 1
- package com .codingapi .springboot .generator ;
1
+ package com .codingapi .springboot .generator . service ;
2
2
3
3
import com .codingapi .springboot .generator .dao .IdKeyDao ;
4
4
import com .codingapi .springboot .generator .domain .IdKey ;
5
5
6
- public class IdGenerateContext {
6
+ public class IdGenerateService {
7
7
8
8
private final IdKeyDao keyDao ;
9
9
10
- public IdGenerateContext (IdKeyDao keyDao ) {
10
+ public IdGenerateService (IdKeyDao keyDao ) {
11
11
this .keyDao = keyDao ;
12
12
}
13
13
Original file line number Diff line number Diff line change
1
+ package com .codingapi .springboot .generator ;
2
+
3
+ import lombok .extern .slf4j .Slf4j ;
4
+ import org .junit .jupiter .api .Test ;
5
+ import org .springframework .boot .test .context .SpringBootTest ;
6
+
7
+ import static org .junit .jupiter .api .Assertions .*;
8
+
9
+ @ Slf4j
10
+ @ SpringBootTest
11
+ class IdGenerateServiceTest {
12
+
13
+ @ Test
14
+ void generateId () {
15
+ for (int i =0 ;i <1000 ;i ++) {
16
+ long id = IdGeneratorContext .getInstance ().generateId (IdGenerateServiceTest .class );
17
+ log .info ("id=>{}" , id );
18
+ assertTrue (id > 0 , "id generator error." );
19
+ }
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ package com .codingapi .springboot .generator ;
2
+
3
+ import org .springframework .boot .SpringApplication ;
4
+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
5
+
6
+ @ SpringBootApplication
7
+ public class IdGeneratorApplication {
8
+
9
+ public static void main (String [] args ) {
10
+ SpringApplication .run (IdGeneratorApplication .class ,args );
11
+ }
12
+ }
Original file line number Diff line number Diff line change
1
+ codingapi.id.generator.jdbc-url =jdbc:h2:file:./test.db
You can’t perform that action at this time.
0 commit comments