jiggy
jiggy

Reputation: 3836

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.

Upvotes: 5

Views: 3435

Answers (2)

Joel
Joel

Reputation: 106

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();

Upvotes: 7

JeeBee
JeeBee

Reputation: 17556

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.

Upvotes: 1

Related Questions