diff --git a/cdi/src/main/java/com/baeldung/cdi2observers/application/BootstrappingApplication.java b/cdi/src/main/java/com/baeldung/cdi2observers/application/BootstrappingApplication.java index 4896408502..dc0bdeb951 100644 --- a/cdi/src/main/java/com/baeldung/cdi2observers/application/BootstrappingApplication.java +++ b/cdi/src/main/java/com/baeldung/cdi2observers/application/BootstrappingApplication.java @@ -1,15 +1,16 @@ -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!")); - } - } -} +package com.baeldung.cdi2observers.application; + +import com.baeldung.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!")); + } + } +} diff --git a/cdi/src/main/java/com/baeldung/cdi2observers/events/ExampleEvent.java b/cdi/src/main/java/com/baeldung/cdi2observers/events/ExampleEvent.java index a2329d2ef1..9adfad4d39 100644 --- a/cdi/src/main/java/com/baeldung/cdi2observers/events/ExampleEvent.java +++ b/cdi/src/main/java/com/baeldung/cdi2observers/events/ExampleEvent.java @@ -1,14 +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; - } -} +package com.baeldung.cdi2observers.events; + +public class ExampleEvent { + + private final String eventMessage; + + public ExampleEvent(String eventMessage) { + this.eventMessage = eventMessage; + } + + public String getEventMessage() { + return eventMessage; + } +} diff --git a/cdi/src/main/java/com/baeldung/cdi2observers/events/ExampleEventSource.java b/cdi/src/main/java/com/baeldung/cdi2observers/events/ExampleEventSource.java index f37030778a..5a0aa0b5e3 100644 --- a/cdi/src/main/java/com/baeldung/cdi2observers/events/ExampleEventSource.java +++ b/cdi/src/main/java/com/baeldung/cdi2observers/events/ExampleEventSource.java @@ -1,14 +1,14 @@ -package com.baeldung.cdi.cdi2observers.events; - -import javax.enterprise.event.Event; -import javax.inject.Inject; - -public class ExampleEventSource { - - @Inject - Event exampleEvent; - - public void fireEvent() { - exampleEvent.fireAsync(new ExampleEvent("Welcome to Baeldung!")); - } -} +package com.baeldung.cdi2observers.events; + +import javax.enterprise.event.Event; +import javax.inject.Inject; + +public class ExampleEventSource { + + @Inject + Event exampleEvent; + + public void fireEvent() { + exampleEvent.fireAsync(new ExampleEvent("Welcome to Baeldung!")); + } +} diff --git a/cdi/src/main/java/com/baeldung/cdi2observers/observers/AnotherExampleEventObserver.java b/cdi/src/main/java/com/baeldung/cdi2observers/observers/AnotherExampleEventObserver.java index 34520c2b3d..3af48af13f 100644 --- a/cdi/src/main/java/com/baeldung/cdi2observers/observers/AnotherExampleEventObserver.java +++ b/cdi/src/main/java/com/baeldung/cdi2observers/observers/AnotherExampleEventObserver.java @@ -1,12 +1,13 @@ -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(); - } -} +package com.baeldung.cdi2observers.observers; + +import com.baeldung.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(); + } +} diff --git a/cdi/src/main/java/com/baeldung/cdi2observers/observers/ExampleEventObserver.java b/cdi/src/main/java/com/baeldung/cdi2observers/observers/ExampleEventObserver.java index b3522b2ad0..33fdc43bbb 100644 --- a/cdi/src/main/java/com/baeldung/cdi2observers/observers/ExampleEventObserver.java +++ b/cdi/src/main/java/com/baeldung/cdi2observers/observers/ExampleEventObserver.java @@ -1,13 +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()); - } -} +package com.baeldung.cdi2observers.observers; + +import com.baeldung.cdi2observers.events.ExampleEvent; +import com.baeldung.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()); + } +} diff --git a/cdi/src/main/java/com/baeldung/cdi2observers/services/TextService.java b/cdi/src/main/java/com/baeldung/cdi2observers/services/TextService.java index 47788a0657..eabe031223 100644 --- a/cdi/src/main/java/com/baeldung/cdi2observers/services/TextService.java +++ b/cdi/src/main/java/com/baeldung/cdi2observers/services/TextService.java @@ -1,8 +1,8 @@ -package com.baeldung.cdi.cdi2observers.services; - -public class TextService { - - public String parseText(String text) { - return text.toUpperCase(); - } -} +package com.baeldung.cdi2observers.services; + +public class TextService { + + public String parseText(String text) { + return text.toUpperCase(); + } +} diff --git a/cdi/src/main/resources/META-INF/beans.xml b/cdi/src/main/resources/META-INF/beans.xml index d41b35e7d9..144e9e567f 100644 --- a/cdi/src/main/resources/META-INF/beans.xml +++ b/cdi/src/main/resources/META-INF/beans.xml @@ -1,7 +1,8 @@ - + + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" + bean-discovery-mode="all"> com.baeldung.interceptor.AuditedInterceptor diff --git a/cdi/src/test/java/com/baeldung/test/cdi2observers/tests/TextServiceUnitTest.java b/cdi/src/test/java/com/baeldung/test/cdi2observers/tests/TextServiceUnitTest.java index deecf13f9a..1b976144aa 100644 --- a/cdi/src/test/java/com/baeldung/test/cdi2observers/tests/TextServiceUnitTest.java +++ b/cdi/src/test/java/com/baeldung/test/cdi2observers/tests/TextServiceUnitTest.java @@ -1,14 +1,15 @@ -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"); - } -} +package com.baeldung.test.cdi2observers.tests; + +import com.baeldung.cdi2observers.services.TextService; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class TextServiceUnitTest { + + @Test + public void givenTextServiceInstance_whenCalledparseText_thenCorrect() { + TextService textService = new TextService(); + assertThat(textService.parseText("Baeldung")).isEqualTo("BAELDUNG"); + } +} diff --git a/cdi/src/test/java/com/baeldung/test/dependencyinjection/TimeLoggerFactoryUnitTest.java b/cdi/src/test/java/com/baeldung/test/dependencyinjection/TimeLoggerFactoryUnitTest.java index caf2ed32b5..b22f189373 100644 --- a/cdi/src/test/java/com/baeldung/test/dependencyinjection/TimeLoggerFactoryUnitTest.java +++ b/cdi/src/test/java/com/baeldung/test/dependencyinjection/TimeLoggerFactoryUnitTest.java @@ -6,7 +6,6 @@ import static org.assertj.core.api.Assertions.assertThat; import org.junit.Test; public class TimeLoggerFactoryUnitTest { - @Test public void givenTimeLoggerFactory_whenCalledgetTimeLogger_thenOneAssertion() { TimeLoggerFactory timeLoggerFactory = new TimeLoggerFactory();