Science & Technology

#10 Constructor and Setter Injection in Spring

by Telusko

Share:

📚 Main topics

  • Spring XML configurationThe Spring container creates beans defined in spring.xml, including Dev and Laptop objects, and stores them in the IoC container. 0:30
  • Setter injection for valuesA property in a bean definition can inject a simple value, such as age, through its setter. 6:13
  • Constructor injection for valuesA constructor-arg can pass a value to a parameterized constructor; index can clarify the order when there are multiple parameters. 8:17
  • Injecting object referencesUse ref rather than value to wire the Laptop bean into Dev, either through a property or a constructor argument. 11:26

✨ Key takeaways

  • Setters must be availableSetter injection requires the corresponding setter method; without it, Spring reports that the property is not writable. 13:31
  • Choose injection based on needConstructor injection suits dependencies that are required when the object is created, while setter injection suits optional dependencies that can be assigned later. 15:02

🧠 Lessons learned

  • Bean IDs identify referencesThe ref value must match the ID of the bean in the Spring container so Spring can connect the objects. 11:57
  • Check the configuration and classWhen injection fails, verify that the bean property has a matching setter and that the reference points to an existing bean. 13:31

🏁 Conclusion/next steps

  • Injection supports values and objectsSpring setter and constructor injection can both handle simple values and reference types; the choice depends on whether the dependency is mandatory or optional. 15:02

Transcript excerpt

0:00 welcome back aliens let's continue the series on Spring framework Target for this video is 150 comments so to this point we were able to work with spring boot but then we want to also explore how do you work with spring framework without spring Boot and then uh to achieve that what we have done is we got this Dev we got this app uh basically we were trying to create the object for Dev so when I say we are trying to create the object basically I'm asking spring framework to give me the object as you can see uh we have done that uh in the configuration

0:30 so if I show you the configuration which is spring.xml we got three objects here so every time you want the object from Spring framework if you are not using Spring Boot we have to use some configuration we are using XML here you can also do that in different ways but in XML if you are doing that we can use Bean tags uh you can see we got a bean tag with the ID Dev and the class name is Dev uh we got another Bean tag which is Dev one and the class is dev then we got laptop of class laptop so in total

🔒 The full, searchable transcript is available with Pro.

🔒 Unlock Premium Features

This is a premium feature. Upgrade to unlock unlimited Q&A, transcripts, mindmaps, and translations.

🔒 Unlock Premium Features

Access to Chat is a premium feature. Upgrade now to unlock unlimited studying tools.

🔒 Unlock Premium Features

Access to Mindmap is a premium feature. Upgrade now to unlock unlimited studying tools.

🔒 Unlock Premium Features

Access to Translation is a premium feature. Upgrade now to unlock unlimited studying tools.

Get unlimited summaries, Q&A, transcripts and more with Pro

Upgrade to Pro

Suggestions

🔒 Unlock Premium Features

Access to AI Suggestions is a premium feature. Upgrade now to unlock unlimited studying tools.