jQuery in Action, 2nd edition*
The moose likes Spring and the fly likes Filters in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Filters in java" Watch "Filters in java" New topic
Author

Filters in java

Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
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


Man Suraj
Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Dec 20, 2007
Posts: 299
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

Did you read the Javadocs for GenericFilterBean?
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
yes David.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Does it make sense now? It seems pretty straight-forward.
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Dec 20, 2007
Posts: 299
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...
 
jQuery in Action, 2nd edition
 
subject: Filters in java
 
Similar Threads
Filters and GZip
config my-faces & tomahawk & Trinidad
Spring primer: application context won't initialize my beans
Blank page returned when filters are used with struts
Filters