Tuesday 15 January 2013

java - InvocationTargetException trying to make new instance of service class -



java - InvocationTargetException trying to make new instance of service class -

in current project, when method called:

public collection<? extends object> list_values() throws exception { string nome = classe().getsimplename(); string nome_service = nome+"service"; string local_service = "com.spring.model."+nome; class<?> clazz = class.forname(local_service.tolowercase()+"."+nome_service); object serv = clazz.newinstance(); collection<? extends object> list = (collection<? extends object>) serv.getclass().getmethod("lista").invoke(serv); homecoming list; }

the application triggers invocationtargetexception caused error:

caused by: java.lang.nullpointerexception @ com.spring.config.generic.service.basicservice.lista(basicservice.java:51)

where basicservice superclass class stored in variable clazz.

anyone can tell me doing wrong here? right way create new instance of class?

ps.: line 51 in basicservice placed within method:

@transactional public list<?> lista() { homecoming dao.findall(); }

and fellow member dao defined way:

@autowired protected dao<e> dao;

ok, solve problem adding project class named applicationcontextholder, code:

@component public class applicationcontextholder implements applicationcontextaware { private static applicationcontext context; @override public void setapplicationcontext(applicationcontext applicationcontext) throws beansexception { context = applicationcontext; } public static applicationcontext getcontext() { homecoming context; } }

the final code function list_values(...) that:

public list<?> list_values() throws exception { string nome = classe().getsimplename(); class<?> clazz = class.forname("com.spring.model."+nome.tolowercase()+"."+nome+"service"); object object = clazz.newinstance(); applicationcontextholder.getcontext().getautowirecapablebeanfactory().autowirebean(object); homecoming (list<?>) object.getclass().getmethod("lista").invoke(object); }

java spring reflection invocationtargetexception

No comments:

Post a Comment