Bael 1366 dagger2 (#4288)

* BAEL-1366-dagger2: Added Dagger 2 example and test case. Added dependency and Dagger compiler plugin to pom.
This commit is contained in:
Donato Rimenti 2018-05-29 21:00:33 +02:00 committed by Josh Cummings
parent 98ad136e25
commit 8713e26b6f
8 changed files with 294 additions and 0 deletions

1
dagger/README.md Normal file
View File

@ -0,0 +1 @@
### Relevant articles

54
dagger/pom.xml Normal file
View File

@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<artifactId>dagger</artifactId>
<name>dagger</name>
<parent>
<artifactId>parent-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- Dagger 2 -->
<dependency>
<groupId>com.google.dagger</groupId>
<artifactId>dagger</artifactId>
<version>${dagger.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Annotation processor for Dagger 2 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>com.google.dagger</groupId>
<artifactId>dagger-compiler</artifactId>
<version>2.16</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<junit.version>4.12</junit.version>
<dagger.version>2.16</dagger.version>
</properties>
</project>

View File

@ -0,0 +1,45 @@
package com.baeldung.dagger.intro;
/**
* Brand of a {@link Car}.
*
* @author Donato Rimenti
*
*/
public class Brand {
/**
* The name of the brand.
*/
private String name;
/**
* Instantiates a new Brand.
*
* @param name
* the {@link #name}
*/
public Brand(String name) {
this.name = name;
}
/**
* Gets the {@link #name}.
*
* @return the {@link #name}
*/
public String getName() {
return name;
}
/**
* Sets the {@link #name}.
*
* @param name
* the new {@link #name}
*/
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,75 @@
package com.baeldung.dagger.intro;
import javax.inject.Inject;
/**
* Represents a car.
*
* @author Donato Rimenti
*
*/
public class Car {
/**
* The car's engine.
*/
private Engine engine;
/**
* The car's brand.
*/
private Brand brand;
/**
* Instantiates a new Car.
*
* @param engine
* the {@link #engine}
* @param brand
* the {@link #brand}
*/
@Inject
public Car(Engine engine, Brand brand) {
this.engine = engine;
this.brand = brand;
}
/**
* Gets the {@link #engine}.
*
* @return the {@link #engine}
*/
public Engine getEngine() {
return engine;
}
/**
* Sets the {@link #engine}.
*
* @param engine
* the new {@link #engine}
*/
public void setEngine(Engine engine) {
this.engine = engine;
}
/**
* Gets the {@link #brand}.
*
* @return the {@link #brand}
*/
public Brand getBrand() {
return brand;
}
/**
* Sets the {@link #brand}.
*
* @param brand
* the new {@link #brand}
*/
public void setBrand(Brand brand) {
this.brand = brand;
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.dagger.intro;
/**
* Engine of a {@link Car}.
*
* @author Donato Rimenti
*
*/
public class Engine {
/**
* Starts the engine.
*/
public void start() {
System.out.println("Engine started");
}
/**
* Stops the engine.
*/
public void stop() {
System.out.println("Engine stopped");
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.dagger.intro;
import javax.inject.Singleton;
import dagger.Component;
/**
* Dagger component for building vehicles.
*
* @author Donato Rimenti
*
*/
@Singleton
@Component(modules = VehiclesModule.class)
public interface VehiclesComponent {
/**
* Builds a {@link Car}.
*
* @return a {@link Car}
*/
public Car buildCar();
}

View File

@ -0,0 +1,37 @@
package com.baeldung.dagger.intro;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
/**
* Dagger module for providing vehicles components.
*
* @author Donato Rimenti
*
*/
@Module
public class VehiclesModule {
/**
* Creates an {@link Engine}.
*
* @return an {@link Engine}
*/
@Provides
public Engine provideEngine() {
return new Engine();
}
/**
* Creates a {@link Brand}.
*
* @return a {@link Brand}
*/
@Provides
@Singleton
public Brand provideBrand() {
return new Brand("Baeldung");
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.dagger.intro;
import org.junit.Assert;
import org.junit.Test;
/**
* Unit test for building a {@link Car} using Dagger.
*
* @author Donato Rimenti
*
*/
public class DaggerUnitTest {
/**
* Builds two {@link Car} and checks that the fields are injected correctly.
*/
@Test
public void givenGeneratedComponent_whenBuildingCar_thenDependenciesInjected() {
VehiclesComponent component = DaggerVehiclesComponent.create();
Car carOne = component.buildCar();
Car carTwo = component.buildCar();
Assert.assertNotNull(carOne);
Assert.assertNotNull(carTwo);
Assert.assertNotNull(carOne.getEngine());
Assert.assertNotNull(carTwo.getEngine());
Assert.assertNotNull(carOne.getBrand());
Assert.assertNotNull(carTwo.getBrand());
Assert.assertNotEquals(carOne.getEngine(), carTwo.getEngine());
Assert.assertEquals(carOne.getBrand(), carTwo.getBrand());
}
}