Random as a dependency

Report a typo

Suppose that you are writing a Spring application and need to declare a class that requires a predefined object of the Random class as a dependency. Select what to put instead of each (N) to make it work.

Java
// (1)
public class RandomBasedComponent {
    private final Random random;

    // (2)
    public RandomBasedComponent(Random random) {
        this.random = random;
    }

    // (3)
    public void performAction() {
        // do something with random
        System.out.println(random.nextInt());
    }
}

// (4)
class RandomConfig {
    private static final long RANDOM_SEED = 1000L;

    // (5)
    public Random predefinedRandom() {
        return new Random(RANDOM_SEED);
    }
}
Kotlin
// (1)
class RandomBasedComponent /* (2) */ constructor(private val random: Random) {
    // (3)
    fun performAction() {
        // do something with random
        println(random.nextInt())
    }

}

// (4)
class RandomConfig {
    companion object {
        private const val RANDOM_SEED = 1000L
    }

    // (5)
    fun predefinedRandom(): Random {
        return Random(RANDOM_SEED)
    }
}
Match the items from left and right columns
1
2
3
4
5
@Autowired or nothing
@Bean
@Configuration
@Component
Nothing
___

Create a free account to access the full topic