Java: What scenarios call for the use of reflection?

前端 未结 7 1150
无人共我
无人共我 2020-12-29 10:42

So from reading some of the articles, the message i got out of it was being able to modify fields and set values to classes in real time without recompiling.

so is i

7条回答
  •  被撕碎了的回忆
    2020-12-29 11:17

    Reflection is used when it is needed to get into the other classes in deeper level. So in most of the cases, these implementors have the container-behavior. For instance, dependency injection is mostly done with the use of reflection. If you need a framework as an example for that, Spring does its dependency injection jobs with the help of reflection API.

    You will also find reflections used behind the scenes in a large number of areas. For example, if you used JAXB, then a lot of the marshalling / unmarshalling of the XML will be done using reflections. Using Annotations in your code often results in reflections being used behind the scenes. When performing unit testing, particularly when mocking classes and/or methods, often has lots of reflections code being used.

提交回复
热议问题