Uploaded April 2018 | Updated November 2026, 3 hours ago
Lets take a look at dependency injection, what it is and how to use it in Java Spring.
Dependency Injection is an inversion of control pattern that removes objects creating or finding the objects they need to simply requesting them. This means that we can create a system that requires a database or a paint-brush (or anything else you want) and have the framework decide which object to provide your classes.
There are two parts to building dependency injection in Spring.
1. A bean (or component) you can inject
2. An object which requires the bean
Once you have both of these things you can very easily pull the first, into the second. That is what Spring is here to help you do.
In Spring you solve defining an injectable Bean by using one of a few different interfaces. In this example we'll use `@Component`. You can define a required bean by adding `@Autowired` to a field which signifies to Spring that the component is required for that field.
I've created a video that introduces the Spring Framework and walks through setting up the basic Application and Controller we reuse in this video: youtu.be/K8OXyLKjKJg
If you're looking for more examples of how to use Spring's Dependency Injection features this link has both written and video examples: https://springframework.guru/dependency-injection-example-using-spring/
Join the World of Zero Discord Server: discord.gg/hU5Kq2u
Lets take a look at dependency injection, what it is and how to use it in Java Spring.
Dependency Injection is an inversion of control pattern that removes objects creating or finding the objects they need to simply requesting them. This means that we can create a system that requires a database or a paint-brush (or anything else you want) and have the framework decide which object to provide your classes.
There are two parts to building dependency injection in Spring.
1. A bean (or component) you can inject
2. An object which requires the bean
Once you have both of these things you can very easily pull the first, into the second. That is what Spring is here to help you do.
In Spring you solve defining an injectable Bean by using one of a few different interfaces. In this example we'll use `@Component`. You can define a required bean by adding `@Autowired` to a field which signifies to Spring that the component is required for that field.
I've created a video that introduces the Spring Framework and walks through setting up the basic Application and Controller we reuse in this video: youtu.be/K8OXyLKjKJg
If you're looking for more examples of how to use Spring's Dependency Injection features this link has both written and video examples: https://springframework.guru/dependency-injection-example-using-spring/
Join the World of Zero Discord Server: discord.gg/hU5Kq2u




 for recommending the use of `ContextMenu`s. You can find more of their content as a guest in videos at Infallible Code: https://www.youtube.com/user/charlesamat
### Code Examples
Some example uses of Unity 3Ds `ContextMenu` and `ContextMenuItem` Attributes:
#### `ContextMenu`
A `ContextMenu` Attribute is attached to functions in your `MonoBehavior`. When that menu item is clicked it will invoke the function the attribute is assigned to:
```csharp
[ContextMenu(Context Menu Example)]
void HelloContextMenu() {
Debug.Log(Context Menu Clicked!);
}
```
#### `ContextMenuItem`
A `ContextMenuItem` Attribute is attached to a field and requires you to provide the name of a function that will be invoked when the menu item is clicked.
```csharp
[ContextMenuItem(Context Menu Item Example, nameof(InvokeContextMenuItem))]
public float someVariable;
void InvokeContextMenuItem() {
Debug.Log(Context Menu Item Clicked!);
}
```
[`nameof`](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/nameof) is a C# function that returns a string representation for the name of the class, function, field, property etc that is referenced. Using this allows things like refactorings to function better and provides compiler errors if you spell a function name incorrectly (`nameof(FunctionName)` will throw an error if `FunctionName` is not defined).
Note: The functions invoked by both `ContextMenu` and `ContextMenuItem` should not have any arguments.
***
Unitys documentation on the `ContextMenu` Attribute: https://docs.unity3d.com/ScriptReference/ContextMenu.html
Unitys documentation on the `ContextMenuItem` Attribute: https://docs.unity3d.com/ScriptReference/ContextMenuItemAttribute.html
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Using ContextMenu and ContextMenuItem in Unity 3D](https://i.ytimg.com/vi/GKG-vUFt3D4/mqdefault.jpg)


![Building a Custom Dice Model with OpenSCAD
OpenSCAD is a tool that can compile programs into 3D models allowing you to code your own CAD designs instead of building them inside of a WYSIWYG type editor like Fusion 360, SketchUp etc.
The module we created for a basic dice looks like this:
```OpenSCAD
module CreateDice(size = 16) {
textSize = size * 0.75;
textLocation = size / 2 - 1;
textExtrusionSize = 2;
difference() {
cube([size, size, size], center = true);
translate([0,0,textLocation]) linear_extrude(textExtrusionSize) text(1, size=textSize, halign=center, valign=center);
rotate([180, 0, 0]) translate([0,0,textLocation]) linear_extrude(textExtrusionSize) text(6, size=textSize, halign=center, valign=center);
rotate([0, 90, 0]) translate([0,0,textLocation]) linear_extrude(textExtrusionSize) text(2, size=textSize, halign=center, valign=center);
rotate([0, 270, 0]) translate([0,0,textLocation]) linear_extrude(textExtrusionSize) text(5, size=textSize, halign=center, valign=center);
rotate([90, 0, 0]) translate([0,0,textLocation]) linear_extrude(textExtrusionSize) text(3, size=textSize, halign=center, valign=center);
rotate([270, 0, 0]) translate([0,0,textLocation]) linear_extrude(textExtrusionSize) text(4, size=textSize, halign=center, valign=center);
}
}
```
Get OpenSCAD: https://openscad.org/index.html
OpenSCADs documentation: https://openscad.org/documentation.html
OpenSCAD on GitHub: https://github.com/openscad/openscad/
You can get a copy of my models and the SCAD files that generate them here: https://github.com/runewake2/scad-examples
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Building a Custom Dice Model with OpenSCAD](https://i.ytimg.com/vi/HxiyUi4RQzw/mqdefault.jpg)


