Skip to content

Commit bfc13e5

Browse files
committed
xmoleculesGH-11 - Add Spring module.
1 parent 55b8899 commit bfc13e5

File tree

8 files changed

+179
-7
lines changed

8 files changed

+179
-7
lines changed

jddd-examples/jddd-spring-data-jpa/pom.xml

+12-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<dependency>
1717
<groupId>org.springframework.boot</groupId>
1818
<artifactId>spring-boot-dependencies</artifactId>
19-
<version>2.3.0.M1</version>
19+
<version>2.3.0.RC1</version>
2020
<scope>import</scope>
2121
<type>pom</type>
2222
</dependency>
@@ -58,6 +58,14 @@
5858
<groupId>${project.groupId}</groupId>
5959
<artifactId>jddd-jpa</artifactId>
6060
<version>${project.version}</version>
61+
<optional>true</optional>
62+
</dependency>
63+
64+
<dependency>
65+
<groupId>${project.groupId}</groupId>
66+
<artifactId>jddd-spring</artifactId>
67+
<version>${project.version}</version>
68+
<optional>true</optional>
6169
</dependency>
6270

6371
<dependency>
@@ -89,6 +97,9 @@
8997
</executions>
9098
<configuration>
9199
<transformations>
100+
<transformation>
101+
<plugin>org.jddd.spring.JDddSpringPlugin</plugin>
102+
</transformation>
92103
<transformation>
93104
<plugin>org.jddd.jpa.JDddJpaPlugin</plugin>
94105
</transformation>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.jddd.examples.jpa.customer;
17+
18+
import org.jddd.core.annotation.Service;
19+
20+
/**
21+
* @author Oliver Drotbohm
22+
*/
23+
@Service
24+
public class CustomerManagement {
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
spring.jpa.show-sql=true

jddd-examples/jddd-spring-data-jpa/src/test/java/org/jddd/examples/jpa/ApplicationIntegrationTests.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,29 @@
1515
*/
1616
package org.jddd.examples.jpa;
1717

18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import lombok.RequiredArgsConstructor;
21+
22+
import org.jddd.examples.jpa.customer.CustomerManagement;
1823
import org.junit.jupiter.api.Test;
1924
import org.springframework.boot.test.context.SpringBootTest;
25+
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
26+
import org.springframework.context.ConfigurableApplicationContext;
2027

2128
/**
2229
* @author Oliver Drotbohm
2330
*/
2431
@SpringBootTest
32+
@RequiredArgsConstructor
2533
class ApplicationIntegrationTests {
2634

35+
private final ConfigurableApplicationContext context;
36+
2737
@Test
2838
void bootstrapsContainer() {
29-
39+
40+
assertThat(AssertableApplicationContext.get(() -> context)) //
41+
.hasSingleBean(CustomerManagement.class);
3042
}
3143
}

jddd-jpa/pom.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<artifactId>jddd-core</artifactId>
1919
<version>${project.version}</version>
2020
</dependency>
21-
21+
2222
<dependency>
2323
<groupId>jakarta.persistence</groupId>
2424
<artifactId>jakarta.persistence-api</artifactId>
@@ -30,11 +30,11 @@
3030
<artifactId>byte-buddy</artifactId>
3131
<version>${bytebuddy.version}</version>
3232
</dependency>
33-
33+
3434
<dependency>
3535
<groupId>org.slf4j</groupId>
3636
<artifactId>slf4j-api</artifactId>
37-
<version>1.7.30</version>
37+
<version>${slf4j.version}</version>
3838
</dependency>
3939

4040
<dependency>
@@ -43,7 +43,7 @@
4343
<version>${lombok.version}</version>
4444
<scope>provided</scope>
4545
</dependency>
46-
46+
4747
</dependencies>
4848

4949
</project>

jddd-spring/pom.xml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.jddd</groupId>
7+
<artifactId>jddd</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
</parent>
10+
11+
<artifactId>jddd-spring</artifactId>
12+
<name>jDDD - Spring plugin</name>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>${project.groupId}</groupId>
17+
<artifactId>jddd-core</artifactId>
18+
<version>${project.version}</version>
19+
</dependency>
20+
21+
<dependency>
22+
<groupId>org.springframework</groupId>
23+
<artifactId>spring-context</artifactId>
24+
<version>5.2.6.RELEASE</version>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>net.bytebuddy</groupId>
29+
<artifactId>byte-buddy</artifactId>
30+
<version>${bytebuddy.version}</version>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.slf4j</groupId>
35+
<artifactId>slf4j-api</artifactId>
36+
<version>${slf4j.version}</version>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.projectlombok</groupId>
41+
<artifactId>lombok</artifactId>
42+
<version>${lombok.version}</version>
43+
<scope>provided</scope>
44+
</dependency>
45+
</dependencies>
46+
47+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.jddd.spring;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import net.bytebuddy.build.Plugin.NoOp;
5+
import net.bytebuddy.description.annotation.AnnotationDescription;
6+
import net.bytebuddy.description.type.TypeDescription;
7+
import net.bytebuddy.dynamic.ClassFileLocator;
8+
import net.bytebuddy.dynamic.DynamicType.Builder;
9+
10+
import java.lang.annotation.Annotation;
11+
import java.util.Arrays;
12+
import java.util.HashSet;
13+
import java.util.Set;
14+
15+
import org.jddd.core.annotation.Repository;
16+
import org.jddd.core.annotation.Service;
17+
import org.springframework.stereotype.Component;
18+
19+
@Slf4j
20+
public class JDddSpringPlugin extends NoOp {
21+
22+
private static Set<Class<?>> ANNOTATIONS = new HashSet<>(
23+
Arrays.asList(Service.class, Repository.class, Factory.class));
24+
25+
@Override
26+
public boolean matches(TypeDescription target) {
27+
return ANNOTATIONS.stream().anyMatch(it -> isAnnotatedWith(target, it));
28+
}
29+
30+
/*
31+
* (non-Javadoc)
32+
* @see net.bytebuddy.build.Plugin#apply(net.bytebuddy.dynamic.DynamicType.Builder, net.bytebuddy.description.type.TypeDescription, net.bytebuddy.dynamic.ClassFileLocator)
33+
*/
34+
@Override
35+
public Builder<?> apply(Builder<?> builder, TypeDescription type, ClassFileLocator classFileLocator) {
36+
37+
if (isAnnotatedWith(type, Service.class)) {
38+
builder = addAnnotationIfMissing(org.springframework.stereotype.Service.class, builder, type);
39+
}
40+
41+
if (isAnnotatedWith(type, Repository.class)) {
42+
builder = addAnnotationIfMissing(org.springframework.stereotype.Repository.class, builder, type);
43+
}
44+
45+
if (isAnnotatedWith(type, org.jddd.core.annotation.Factory.class)) {
46+
builder = addAnnotationIfMissing(Component.class, builder, type);
47+
}
48+
49+
return builder;
50+
}
51+
52+
private static Builder<?> addAnnotationIfMissing(Class<? extends Annotation> annotation, Builder<?> builder,
53+
TypeDescription type) {
54+
55+
if (isAnnotatedWith(type, annotation)) {
56+
return builder;
57+
}
58+
59+
log.info("jDDD Spring Plugin - Annotating {} with {}.", type.getName(), annotation.getName());
60+
61+
return builder.annotateType(getAnnotation(annotation));
62+
}
63+
64+
private static AnnotationDescription getAnnotation(Class<? extends Annotation> type) {
65+
return AnnotationDescription.Builder.ofType(type).build();
66+
}
67+
68+
private static boolean isAnnotatedWith(TypeDescription type, Class<?> annotationType) {
69+
70+
return type.getDeclaredAnnotations() //
71+
.asTypeList() //
72+
.stream() //
73+
.anyMatch(it -> it.isAssignableTo(annotationType)); // || isAnnotatedWith(it, annotationType));
74+
}
75+
}

pom.xml

+2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818
<module>jddd-examples</module>
1919
<module>jddd-jpa</module>
2020
<module>jddd-archunit</module>
21+
<module>jddd-spring</module>
2122
</modules>
2223

2324
<properties>
2425
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2526
<bytebuddy.version>1.10.8</bytebuddy.version>
2627
<lombok.version>1.18.12</lombok.version>
28+
<slf4j.version>1.7.30</slf4j.version>
2729
</properties>
2830

2931
<build>

0 commit comments

Comments
 (0)