Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject dependency into a taglib class?

I'm using Spring 3 and want to inject some dependencies into a class that is part of a taglib. I can imagine some kludge using constructor-arg, but I'm hoping someone else has a better idea.

like image 448
jiggy Avatar asked Dec 08 '10 16:12

jiggy


2 Answers

Should you decide to access a Service or DAO from a custom tag then you need to access the ApplicationContext from the tag and then get the Bean.

ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext()); 
MyService myService = applicationContext.getBean(MyService.class);
myService.doSomething();
like image 136
Joel Avatar answered Oct 04 '22 20:10

Joel


There is also http://www.shredzone.org/projects/jshred/wiki/Spring_supported_Tag_Libraries - this works by creating a proxy class for each taglib class handles the interaction with Spring, and uses Spring to generate an instance of the taglib when required.

like image 28
JeeBee Avatar answered Oct 04 '22 21:10

JeeBee