Spring vs. Java Reflection

I need to dynamically create objects. I use Spring to create a class name map. Now i can use

Spring ApplicationContext.getbean(className)

or

Java Class.forName(className).newInstance().

Which method is more efficient?

Thank.

+5
source share
3 answers

If the spring bean is a prototype of the bean, spring will have to instantiate it with reflection and therefore will have to do what your second snippet does. But requesting a spring context to get a bean doesn't just give you a new instance of the class. It returns a spring bean on which you could apply aspects (security, transactional, etc.), Nested dependencies, etc.

, , , , . , , , ( , ..).

+5

Spring bean, ApplicationContext, . Spring bean, Java, , .

+1

spring . Spring

+1

All Articles