What is reflection and why is it useful?

前端 未结 21 2875
春和景丽
春和景丽 2020-11-21 04:36

What is reflection, and why is it useful?

I\'m particularly interested in Java, but I assume the principles are the same in any language.

21条回答
  •  花落未央
    2020-11-21 04:59

    Reflection is a key mechanism to allow an application or framework to work with code that might not have even been written yet!

    Take for example your typical web.xml file. This will contain a list of servlet elements, which contain nested servlet-class elements. The servlet container will process the web.xml file, and create new a new instance of each servlet class through reflection.

    Another example would be the Java API for XML Parsing (JAXP). Where an XML parser provider is 'plugged-in' via well-known system properties, which are used to construct new instances through reflection.

    And finally, the most comprehensive example is Spring which uses reflection to create its beans, and for its heavy use of proxies

提交回复
热议问题