Thinesh
Thinesh

Reputation: 37

Can we typecast the application context get bean as Class<?>

In my application I want to load dynamic classes. I added the code snippet like below,

private static Class clazz;

In the method of the implemented class,

clazz = (Class) applicationContext.getBean("arrived class name for thirty party");

Is approach valid to get the class in the application?

Upvotes: 1

Views: 809

Answers (2)

Dinesh Arora
Dinesh Arora

Reputation: 2255

You can use - clazz = applicationContext.getBean("arrived class name for thirty party",.class);

ex- Employee emp = ctx.getBean("arrived class name for thirty party", Employee.class);

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240870

if its object reistered and you need class name of that bean

clazz = applicationContext.getBean("arrived class name for thirty party").getClass();

if its String value registered as bean and you need its class instance

clazz = Class.forName(applicationContext.getBean("arrived class name for thirty party"));

Upvotes: 1

Related Questions