Câu hỏi phỏng vấn Spring
Câu hỏi

What is bean auto wiring?

Câu trả lời

Bean autowiring in the Spring framework is a mechanism that allows you to automatically inject dependencies between beans without having to explicitly define the relationships in the configuration files. This feature simplifies the configuration process and reduces the amount of boilerplate code required to wire beans together.

Spring can autowire relationships between collaborating beans by inspecting the contents of the ApplicationContext. There are several modes of autowiring supported by Spring:

  1. no: This is the default mode where no autowiring is done. You have to set the dependencies manually using the ref attribute in your configuration file[1][6].

  2. byName: Autowiring by property name. If a bean's name matches the name of another bean's property, Spring will automatically wire the two beans together[1][6].

  3. byType: Autowiring by property data type. If the data type of a bean is compatible with the data type of another bean's property, Spring will autowire them[1][6].

  4. constructor: Similar to byType, but applies to constructor arguments. If there is exactly one bean of the constructor argument type in the container, Spring will use it for autowiring[1][6].

  5. autodetect: Spring first tries to autowire using constructor, if it fails, it tries byType. This mode has been deprecated from Spring 3.0 onwards[8].

While autowiring can significantly reduce the need to specify properties or constructor arguments, it also has some limitations and disadvantages:

  • Autowiring is less exact than explicit wiring, which can lead to ambiguity and unexpected results if multiple bean definitions match the type specified by the setter method or constructor argument[3].
  • Explicit dependencies in property and constructor-arg settings always override autowiring[3].
  • Autowiring cannot be used for primitive and string values[11].
  • The relationships between Spring-managed objects are no longer documented explicitly, which may affect the generation of documen...
middle

middle

Gợi ý câu hỏi phỏng vấn

middle

How would you relate Spring MVC Framework to MVC architecture?

junior

What in the world are Spring beans?

middle

Can we send an Object as the response of Controller handler method?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào