Skip to content

Commit 663a2f7

Browse files
authored
Merge pull request eugenp#5196 from mikewojtyna/BAEL-2067
Add BAEL-2067 DomainEvents examples
2 parents 20137eb + 7ba5b99 commit 663a2f7

18 files changed

Lines changed: 451 additions & 4 deletions

‎spring-data-jpa/pom.xml‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@
4141
<artifactId>guava</artifactId>
4242
<version>21.0</version>
4343
</dependency>
44+
45+
<!-- JUnit Jupiter dependencies -->
46+
<dependency>
47+
<groupId>org.junit.jupiter</groupId>
48+
<artifactId>junit-jupiter-api</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.junit.jupiter</groupId>
53+
<artifactId>junit-jupiter-engine</artifactId>
54+
<scope>test</scope>
55+
</dependency>
56+
<!-- JUnit platform launcher -->
57+
<!-- To be able to run tests from IDE directly -->
58+
<dependency>
59+
<groupId>org.junit.platform</groupId>
60+
<artifactId>junit-platform-launcher</artifactId>
61+
<version>${junit-platform.version}</version>
62+
<scope>test</scope>
63+
</dependency>
4464
</dependencies>
4565

4666
</project>

‎spring-data-jpa/src/main/java/com/baeldung/Application.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.baeldung;
22

3-
import com.baeldung.dao.repositories.impl.ExtendedRepositoryImpl;
43
import org.springframework.boot.SpringApplication;
54
import org.springframework.boot.autoconfigure.SpringBootApplication;
6-
import org.springframework.boot.autoconfigure.domain.EntityScan;
75
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
86

7+
import com.baeldung.dao.repositories.impl.ExtendedRepositoryImpl;
8+
99
@SpringBootApplication
1010
@EnableJpaRepositories(repositoryBaseClass = ExtendedRepositoryImpl.class)
1111
public class Application {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.ddd.event;
5+
6+
import javax.persistence.Entity;
7+
import javax.persistence.Id;
8+
import javax.persistence.Transient;
9+
10+
import org.springframework.context.ApplicationEventPublisher;
11+
12+
@Entity
13+
class Aggregate {
14+
@Transient
15+
private ApplicationEventPublisher eventPublisher;
16+
@Id
17+
private long id;
18+
19+
private Aggregate() {
20+
}
21+
22+
Aggregate(long id, ApplicationEventPublisher eventPublisher) {
23+
this.id = id;
24+
this.eventPublisher = eventPublisher;
25+
}
26+
27+
/* (non-Javadoc)
28+
* @see java.lang.Object#toString()
29+
*/
30+
@Override
31+
public String toString() {
32+
return "DomainEntity [id=" + id + "]";
33+
}
34+
35+
void domainOperation() {
36+
// some business logic
37+
if (eventPublisher != null) {
38+
eventPublisher.publishEvent(new DomainEvent());
39+
}
40+
}
41+
42+
long getId() {
43+
return id;
44+
}
45+
46+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.ddd.event;
5+
6+
import java.util.ArrayList;
7+
import java.util.Collection;
8+
9+
import javax.persistence.Entity;
10+
import javax.persistence.GeneratedValue;
11+
import javax.persistence.Id;
12+
import javax.persistence.Transient;
13+
14+
import org.springframework.data.domain.AfterDomainEventPublication;
15+
import org.springframework.data.domain.DomainEvents;
16+
17+
@Entity
18+
public class Aggregate2 {
19+
@Transient
20+
private final Collection<DomainEvent> domainEvents;
21+
@Id
22+
@GeneratedValue
23+
private long id;
24+
25+
public Aggregate2() {
26+
domainEvents = new ArrayList<>();
27+
}
28+
29+
@AfterDomainEventPublication
30+
public void clearEvents() {
31+
domainEvents.clear();
32+
}
33+
34+
public void domainOperation() {
35+
// some domain operation
36+
domainEvents.add(new DomainEvent());
37+
}
38+
39+
@DomainEvents
40+
public Collection<DomainEvent> events() {
41+
return domainEvents;
42+
}
43+
44+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.ddd.event;
5+
6+
import org.springframework.data.repository.CrudRepository;
7+
8+
public interface Aggregate2Repository extends CrudRepository<Aggregate2, Long> {
9+
10+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.ddd.event;
5+
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.Id;
9+
10+
import org.springframework.data.domain.AbstractAggregateRoot;
11+
12+
@Entity
13+
public class Aggregate3 extends AbstractAggregateRoot<Aggregate3> {
14+
@Id
15+
@GeneratedValue
16+
private long id;
17+
18+
public void domainOperation() {
19+
// some domain operation
20+
registerEvent(new DomainEvent());
21+
}
22+
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.ddd.event;
5+
6+
import org.springframework.data.repository.CrudRepository;
7+
8+
/**
9+
* @author goobar
10+
*
11+
*/
12+
public interface Aggregate3Repository extends CrudRepository<Aggregate3, Long> {
13+
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.ddd.event;
5+
6+
import org.springframework.data.repository.CrudRepository;
7+
8+
public interface AggregateRepository extends CrudRepository<Aggregate, Long> {
9+
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.ddd.event;
5+
6+
import org.springframework.boot.SpringBootConfiguration;
7+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
8+
import org.springframework.context.annotation.PropertySource;
9+
10+
@SpringBootConfiguration
11+
@EnableAutoConfiguration
12+
@PropertySource("classpath:/ddd.properties")
13+
public class DddConfig {
14+
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.ddd.event;
5+
6+
class DomainEvent {
7+
8+
}

0 commit comments

Comments
 (0)