File tree Expand file tree Collapse file tree 8 files changed +294
-0
lines changed
main/java/com/baeldung/dagger/intro
test/java/com/baeldung/dagger/intro Expand file tree Collapse file tree 8 files changed +294
-0
lines changed Original file line number Diff line number Diff line change 1+ ### Relevant articles
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance" xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
3+ <modelVersion >4.0.0</modelVersion >
4+ <artifactId >dagger</artifactId >
5+ <name >dagger</name >
6+
7+ <parent >
8+ <artifactId >parent-modules</artifactId >
9+ <groupId >com.baeldung</groupId >
10+ <version >1.0.0-SNAPSHOT</version >
11+ </parent >
12+
13+ <dependencies >
14+ <dependency >
15+ <groupId >junit</groupId >
16+ <artifactId >junit</artifactId >
17+ <version >${junit.version} </version >
18+ <scope >test</scope >
19+ </dependency >
20+
21+ <!-- Dagger 2 -->
22+ <dependency >
23+ <groupId >com.google.dagger</groupId >
24+ <artifactId >dagger</artifactId >
25+ <version >${dagger.version} </version >
26+ </dependency >
27+ </dependencies >
28+
29+ <build >
30+ <plugins >
31+ <!-- Annotation processor for Dagger 2 -->
32+ <plugin >
33+ <groupId >org.apache.maven.plugins</groupId >
34+ <artifactId >maven-compiler-plugin</artifactId >
35+ <version >3.6.1</version >
36+ <configuration >
37+ <annotationProcessorPaths >
38+ <path >
39+ <groupId >com.google.dagger</groupId >
40+ <artifactId >dagger-compiler</artifactId >
41+ <version >2.16</version >
42+ </path >
43+ </annotationProcessorPaths >
44+ </configuration >
45+ </plugin >
46+ </plugins >
47+ </build >
48+
49+ <properties >
50+ <junit .version>4.12</junit .version>
51+ <dagger .version>2.16</dagger .version>
52+ </properties >
53+
54+ </project >
Original file line number Diff line number Diff line change 1+ package com .baeldung .dagger .intro ;
2+
3+ /**
4+ * Brand of a {@link Car}.
5+ *
6+ * @author Donato Rimenti
7+ *
8+ */
9+ public class Brand {
10+
11+ /**
12+ * The name of the brand.
13+ */
14+ private String name ;
15+
16+ /**
17+ * Instantiates a new Brand.
18+ *
19+ * @param name
20+ * the {@link #name}
21+ */
22+ public Brand (String name ) {
23+ this .name = name ;
24+ }
25+
26+ /**
27+ * Gets the {@link #name}.
28+ *
29+ * @return the {@link #name}
30+ */
31+ public String getName () {
32+ return name ;
33+ }
34+
35+ /**
36+ * Sets the {@link #name}.
37+ *
38+ * @param name
39+ * the new {@link #name}
40+ */
41+ public void setName (String name ) {
42+ this .name = name ;
43+ }
44+
45+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .dagger .intro ;
2+
3+ import javax .inject .Inject ;
4+
5+ /**
6+ * Represents a car.
7+ *
8+ * @author Donato Rimenti
9+ *
10+ */
11+ public class Car {
12+
13+ /**
14+ * The car's engine.
15+ */
16+ private Engine engine ;
17+
18+ /**
19+ * The car's brand.
20+ */
21+ private Brand brand ;
22+
23+ /**
24+ * Instantiates a new Car.
25+ *
26+ * @param engine
27+ * the {@link #engine}
28+ * @param brand
29+ * the {@link #brand}
30+ */
31+ @ Inject
32+ public Car (Engine engine , Brand brand ) {
33+ this .engine = engine ;
34+ this .brand = brand ;
35+ }
36+
37+ /**
38+ * Gets the {@link #engine}.
39+ *
40+ * @return the {@link #engine}
41+ */
42+ public Engine getEngine () {
43+ return engine ;
44+ }
45+
46+ /**
47+ * Sets the {@link #engine}.
48+ *
49+ * @param engine
50+ * the new {@link #engine}
51+ */
52+ public void setEngine (Engine engine ) {
53+ this .engine = engine ;
54+ }
55+
56+ /**
57+ * Gets the {@link #brand}.
58+ *
59+ * @return the {@link #brand}
60+ */
61+ public Brand getBrand () {
62+ return brand ;
63+ }
64+
65+ /**
66+ * Sets the {@link #brand}.
67+ *
68+ * @param brand
69+ * the new {@link #brand}
70+ */
71+ public void setBrand (Brand brand ) {
72+ this .brand = brand ;
73+ }
74+
75+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .dagger .intro ;
2+
3+ /**
4+ * Engine of a {@link Car}.
5+ *
6+ * @author Donato Rimenti
7+ *
8+ */
9+ public class Engine {
10+
11+ /**
12+ * Starts the engine.
13+ */
14+ public void start () {
15+ System .out .println ("Engine started" );
16+ }
17+
18+ /**
19+ * Stops the engine.
20+ */
21+ public void stop () {
22+ System .out .println ("Engine stopped" );
23+ }
24+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .dagger .intro ;
2+
3+ import javax .inject .Singleton ;
4+
5+ import dagger .Component ;
6+
7+ /**
8+ * Dagger component for building vehicles.
9+ *
10+ * @author Donato Rimenti
11+ *
12+ */
13+ @ Singleton
14+ @ Component (modules = VehiclesModule .class )
15+ public interface VehiclesComponent {
16+
17+ /**
18+ * Builds a {@link Car}.
19+ *
20+ * @return a {@link Car}
21+ */
22+ public Car buildCar ();
23+
24+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .dagger .intro ;
2+
3+ import javax .inject .Singleton ;
4+
5+ import dagger .Module ;
6+ import dagger .Provides ;
7+
8+ /**
9+ * Dagger module for providing vehicles components.
10+ *
11+ * @author Donato Rimenti
12+ *
13+ */
14+ @ Module
15+ public class VehiclesModule {
16+
17+ /**
18+ * Creates an {@link Engine}.
19+ *
20+ * @return an {@link Engine}
21+ */
22+ @ Provides
23+ public Engine provideEngine () {
24+ return new Engine ();
25+ }
26+
27+ /**
28+ * Creates a {@link Brand}.
29+ *
30+ * @return a {@link Brand}
31+ */
32+ @ Provides
33+ @ Singleton
34+ public Brand provideBrand () {
35+ return new Brand ("Baeldung" );
36+ }
37+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .dagger .intro ;
2+
3+ import org .junit .Assert ;
4+ import org .junit .Test ;
5+
6+ /**
7+ * Unit test for building a {@link Car} using Dagger.
8+ *
9+ * @author Donato Rimenti
10+ *
11+ */
12+ public class DaggerUnitTest {
13+
14+ /**
15+ * Builds two {@link Car} and checks that the fields are injected correctly.
16+ */
17+ @ Test
18+ public void givenGeneratedComponent_whenBuildingCar_thenDependenciesInjected () {
19+ VehiclesComponent component = DaggerVehiclesComponent .create ();
20+
21+ Car carOne = component .buildCar ();
22+ Car carTwo = component .buildCar ();
23+
24+ Assert .assertNotNull (carOne );
25+ Assert .assertNotNull (carTwo );
26+ Assert .assertNotNull (carOne .getEngine ());
27+ Assert .assertNotNull (carTwo .getEngine ());
28+ Assert .assertNotNull (carOne .getBrand ());
29+ Assert .assertNotNull (carTwo .getBrand ());
30+ Assert .assertNotEquals (carOne .getEngine (), carTwo .getEngine ());
31+ Assert .assertEquals (carOne .getBrand (), carTwo .getBrand ());
32+ }
33+
34+ }
You can’t perform that action at this time.
0 commit comments