• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Filters in java

 
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have configured Filters in my web.xml. This filter has a referenct to one proeprty say, TextData txd, I want txd as a singleton, i.e only one object of TextData. So my Filter looks something like

txd is of type say TextData. I am injecting txd using Spring. I want "txd" tp be Singleton.


but the problem is:

As expected, for my every request "ABCFilter" intercepts the request which is perfectly OK. But the reference for ABCFilter is new, so i am unable to retrieve the singleton object "txd" , which i injected during server startup.
So somehow, i want to make my ABCFilter also as singleton. However, i am unable to do it.

I tried the below code in my bean cfg file, but of no use.


Please help
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Spring is not a part of SCWCD. I'll move it to Spring forum where there are better chances that your problem will get solved...
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order for dependencies to be injected the class must be loaded via Spring.

The servlet container doesn't (normally, anyway) know anything about Spring.

You'd probably need to set up a Srping context loader listener, put the singleton in application context, and in the filter grab it from the application context. Or make the filter Spring-event-aware and on startup get the singleton. There are probably several other solutions as well, perhaps something a bit cleaner--I just don't know what it is.

Now I do, after about 20 seconds in the Spring API Javadocs (so you know how to find things in the future: I went to the Spring API. I looked in the "web" package. I noticed there was a "servlet" sub-package. I scrolled up to see if there was a "filter" subpackage; there was. "GenericFilterBean" stood out immediately as having potential.)

org.springframework.web.filter.GenericFilterBean

Being able to discover API functionality like this is a critical skill, and in this case was really easy.
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry David, I am not getting what your solutions is.

Iam new to Spring and i need to solve this for my project(limited time) . So if you could elaborate this a little more(how to do it exactly), then it would be great
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you read the Javadocs for GenericFilterBean?
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes David.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it make sense now? It seems pretty straight-forward.
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No David,Its not very clear, somehow iam unable to implement this.

If possible, could you give a good example.
Also, is it possible that , for every request, same Filter, (here ABC filter) is called. What i mean by "same Filter", is same refernece of Filter.( As iam not clear how to use it )

As of now, i see my Filter references changing(while debugging). Say when i start the server, "ABCFilter" has value somehting like @1234, and when i hit some button,ABCFilter intercepts the request again, but this time i see some other address for ABCFilter and not@1234. So can i retrieve the same Filter?Does it make sense?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's easier to understand what's happening when the thread follows a linear conversation. By going back and editing the original post, contextual information is lost, and some of the posts no longer make any sense.
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i dont think i am deviating from my original question,its just that i thought of a work around. It would be great if you could answer either of my question elaborately,(preferably the first one, as acc to you i am deviating, i will create a new thread for the 2 nd one) . So ,could you please tell me how to use this through some example (instead of just one line answers :-)).Before posting here i had gone through this the API.But i couldnot get how to implement it...
 
reply
    Bookmark Topic Watch Topic
  • New Topic