3

Working on a large size project, we started to use spring to manage our dependency injection. Since most dev are migrate from coding stateful class, we found some of stateless bean actually contain instance variables which themself are stateful.

Correct me if I am wrong, it shouldn't be too tough to write a unit test to verify that all the stateless bean defined in spring xml actually is stateless (i.e. bean shouldn't have any instance variable which never defined in spring xml). Before I will try myself to write one, wonder if there is any existing tool out there to check that?

Thx

bing
  • 31
  • 1
  • Why do you mean by "stateless"? A java class with no fields? Wouldn't that just be a bunch of Spring beans that don't talk to each other? – skaffman Dec 11 '09 at 12:38
  • Do you mean that you want to assure that your instance variables never change? – bertolami Dec 11 '09 at 13:18
  • Bean surely can have field, however, my understand is spring bean will be reused in-container, so fields in bean potentially can carry state forward, so those field should be either final (constant) or defined in spring xml too (it can be stateless bean or prototype.., at least their definition is well thought) – bing Dec 11 '09 at 14:20
  • 2
    bing, how about using constructor injection and insisting that every member should be final? – Binil Thomas Dec 11 '09 at 22:48

1 Answers1

1

Check out spring context analyzer. It detects a few of the common spring scope/wiring mistakes, including missing scope proxies and suspicious post-construct field state in your beans. I know many projects have been saved by it ;)

krosenvold
  • 75,535
  • 32
  • 152
  • 208