From the course: Java EE: Contexts and Dependency Injection
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Producers in action
From the course: Java EE: Contexts and Dependency Injection
Producers in action
- [Instructor] Okay, so let's have a look at an example that uses scope. Now here we have the random number generator that we saw earlier. And it generates a list of four numbers randomly, and it stores it in an array list. In the random number service, we inject the array list, and then we just simply print out the list of random numbers that it's chosen. So what I'm going to do, at the moment this has default scope, so whatever scope this class has, the random number class that it's injected into, will adopt that scope. So if I go to bootstrap, and I execute bootstrap, let's see what happens. And what you can see here is that it's generated four different sets of numbers. Which means that every time it's injected into the random number service, it creates a new list of numbers. And you can see here I'm using the random number service four times. And I'm calling the print method four times. So every time I'm using the random number service, it's generating a new list. So let's go to…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.