Add Example Code (#14521)
This commit is contained in:
parent
2879e21aeb
commit
80f4f5a7d0
7
di-modules/avaje/README.md
Normal file
7
di-modules/avaje/README.md
Normal file
@ -0,0 +1,7 @@
|
||||
# Introduction to Avaje Inject
|
||||
|
||||
This module contains articles about Avaje
|
||||
|
||||
### Relevant articles:
|
||||
|
||||
- [Introduction to Avaje Inject](https://www.baeldung.com/avaje-inject/intro)
|
37
di-modules/avaje/pom.xml
Normal file
37
di-modules/avaje/pom.xml
Normal file
@ -0,0 +1,37 @@
|
||||
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>inject-intro</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>avaje-inject-intro</name>
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<avaje.inject.version>9.5</avaje.inject.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.avaje</groupId>
|
||||
<artifactId>avaje-inject</artifactId>
|
||||
<version>${avaje.inject.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.avaje</groupId>
|
||||
<artifactId>avaje-inject-test</artifactId>
|
||||
<version>${avaje.inject.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Annotation processors -->
|
||||
<dependency>
|
||||
<groupId>io.avaje</groupId>
|
||||
<artifactId>avaje-inject-generator</artifactId>
|
||||
<version>${avaje.inject.version}</version>
|
||||
<scope>provided</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,18 @@
|
||||
package com.baeldung.avaje.intro;
|
||||
|
||||
import io.avaje.inject.Bean;
|
||||
import io.avaje.inject.Factory;
|
||||
|
||||
@Factory
|
||||
public class ArmsFactory {
|
||||
|
||||
@Bean
|
||||
public Sword provideSword() {
|
||||
return new Sword();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Shield provideShield() {
|
||||
return new Shield(25);
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.baeldung.avaje.intro;
|
||||
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.inject.Singleton;
|
||||
|
||||
@Singleton
|
||||
public class Knight {
|
||||
|
||||
private Sword sword;
|
||||
|
||||
private Shield shield;
|
||||
|
||||
@Inject
|
||||
public Knight(Sword sword, Shield shield) {
|
||||
this.sword = sword;
|
||||
this.shield = shield;
|
||||
}
|
||||
|
||||
public Sword sword() {
|
||||
return sword;
|
||||
}
|
||||
|
||||
public void sword(Sword engine) {
|
||||
this.sword = engine;
|
||||
}
|
||||
|
||||
public Shield shield() {
|
||||
return shield;
|
||||
}
|
||||
|
||||
public void shield(Shield brand) {
|
||||
this.shield = brand;
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.baeldung.avaje.intro;
|
||||
|
||||
import io.avaje.inject.BeanScope;
|
||||
import io.avaje.inject.PostConstruct;
|
||||
import io.avaje.inject.PreDestroy;
|
||||
import jakarta.inject.Singleton;
|
||||
|
||||
@Singleton
|
||||
public class Ninja {
|
||||
|
||||
private Sword sword;
|
||||
|
||||
@PostConstruct
|
||||
void equip(BeanScope scope) {
|
||||
sword = scope.get(Sword.class);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
void dequip() {
|
||||
sword = null;
|
||||
}
|
||||
|
||||
public Sword sword() {
|
||||
return sword;
|
||||
}
|
||||
|
||||
public void sword(Sword engine) {
|
||||
this.sword = engine;
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.baeldung.avaje.intro;
|
||||
|
||||
public class Shield {
|
||||
|
||||
private int defense;
|
||||
|
||||
public Shield(int defense) {
|
||||
this.defense = defense;
|
||||
}
|
||||
|
||||
public int defense() {
|
||||
return defense;
|
||||
}
|
||||
|
||||
public void defense(int defense) {
|
||||
this.defense = defense;
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.baeldung.avaje.intro;
|
||||
|
||||
public class Sword {
|
||||
|
||||
public void attack() {
|
||||
System.out.println("swing");
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.baeldung.avaje.intro;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import io.avaje.inject.BeanScope;
|
||||
|
||||
class AvajeUnitTest {
|
||||
|
||||
@Test
|
||||
void givenBeanScope_whenScopeGetsKnight_thenKnightShouldHaveDependencies() {
|
||||
|
||||
final var scope = BeanScope.builder().build();
|
||||
final var knight = scope.get(Knight.class);
|
||||
|
||||
assertNotNull(knight);
|
||||
assertNotNull(knight.sword());
|
||||
assertNotNull(knight.shield());
|
||||
assertEquals(25, knight.shield().defense());
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.baeldung.avaje.intro;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import io.avaje.inject.test.InjectTest;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
@InjectTest
|
||||
class ExampleInjectTest {
|
||||
|
||||
@Mock Shield shield;
|
||||
|
||||
@Inject Knight knight;
|
||||
|
||||
@Test
|
||||
void givenMockedShield_whenGetShield_thenShieldShouldHaveMockedValue() {
|
||||
|
||||
Mockito.when(shield.defense()).thenReturn(0);
|
||||
assertNotNull(knight);
|
||||
assertNotNull(knight.sword());
|
||||
assertNotNull(knight.shield());
|
||||
assertEquals(0, knight.shield().defense());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user