BAEL-2354 - An Introduction to the Event Notification Model in CDI 2.0 (#5685)

* Initial Commit

* Update pom.xml

* Update pom.xml

* Update LowercaseTextServiceUnitTest.java

* Update UppercaseTextServiceUnitTest.java

* Delete TextApplication.java

* Update source files

* Update BootstrappingApplication.java

* Add AnotherSimpleEventObserver class

* Rename event classes

* Update ExampleEventSource.java

* Update UppercaseTextService.java

* Delete TextService.java

* Delete LowercaseTextService.java

* Delete LowercaseTextServiceUnitTest.java

* Delete UppercaseTextServiceUnitTest.java

* Fixed text service unit test

* Update TextService.java
This commit is contained in:
Alejandro Gervasio 2018-11-24 18:02:25 -03:00 committed by KevinGilmore
parent 59869435c8
commit 3c3d484eeb
8 changed files with 105 additions and 10 deletions

View File

@ -5,7 +5,7 @@
<groupId>com.baeldung</groupId>
<artifactId>cdi</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-spring-4</artifactId>
@ -14,6 +14,16 @@
</parent>
<dependencies>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>${cdi-api.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<version>${weld-se-core.version}</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
@ -42,11 +52,6 @@
<artifactId>aspectjweaver</artifactId>
<version>${aspectjweaver.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<version>${weld-se-core.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
@ -54,13 +59,13 @@
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<aspectjweaver.version>1.8.9</aspectjweaver.version>
<weld-se-core.version>2.4.1.Final</weld-se-core.version>
<cdi-api.version>2.0.SP1</cdi-api.version>
<weld-se-core.version>3.0.5.Final</weld-se-core.version>
<aspectjweaver.version>1.9.2</aspectjweaver.version>
<hamcrest-core.version>1.3</hamcrest-core.version>
<assertj-core.version>3.10.0</assertj-core.version>
<junit.version>4.12</junit.version>
<spring.version>5.1.2.RELEASE</spring.version>
</properties>
</project>

View File

@ -0,0 +1,15 @@
package com.baeldung.cdi.cdi2observers.application;
import com.baeldung.cdi.cdi2observers.events.ExampleEvent;
import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.se.SeContainerInitializer;
public class BootstrappingApplication {
public static void main(String... args) {
SeContainerInitializer containerInitializer = SeContainerInitializer.newInstance();
try (SeContainer container = containerInitializer.initialize()) {
container.getBeanManager().fireEvent(new ExampleEvent("Welcome to Baeldung!"));
}
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.cdi.cdi2observers.events;
public class ExampleEvent {
private final String eventMessage;
public ExampleEvent(String eventMessage) {
this.eventMessage = eventMessage;
}
public String getEventMessage() {
return eventMessage;
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.cdi.cdi2observers.events;
import javax.enterprise.event.Event;
import javax.inject.Inject;
public class ExampleEventSource {
@Inject
Event<ExampleEvent> exampleEvent;
public void fireEvent() {
exampleEvent.fireAsync(new ExampleEvent("Welcome to Baeldung!"));
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.cdi.cdi2observers.observers;
import com.baeldung.cdi.cdi2observers.events.ExampleEvent;
import javax.annotation.Priority;
import javax.enterprise.event.Observes;
public class AnotherExampleEventObserver {
public String onEvent(@Observes @Priority(2) ExampleEvent event) {
return event.getEventMessage();
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.cdi.cdi2observers.observers;
import com.baeldung.cdi.cdi2observers.events.ExampleEvent;
import com.baeldung.cdi.cdi2observers.services.TextService;
import javax.annotation.Priority;
import javax.enterprise.event.Observes;
public class ExampleEventObserver {
public String onEvent(@Observes @Priority(1) ExampleEvent event, TextService textService) {
return textService.parseText(event.getEventMessage());
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.cdi.cdi2observers.services;
public class TextService {
public String parseText(String text) {
return text.toUpperCase();
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.cdi.cdi2observers.tests;
import com.baeldung.cdi.cdi2observers.services.TextService;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class TextServiceUnitTest {
@Test
public void givenTextServiceInstance_whenCalledparseText_thenCorrect() {
TextService textService = new TextService();
assertThat(textService.parseText("Baeldung")).isEqualTo("BAELDUNG");
}
}