I use the depends-on
attribute in our production system to control the shutdown order of the beans. Thus, Bean1
doesn't have a compile-time dependency on Bean2
, but it should be shutdown first to ensure correct operation. I'd like to test Bean1
in isolation by injecting it into my unit tests. Because Bean2
isn't part of the test, I don't want it to be loaded by Spring when I run the test. Is it possible to express this dependency outside of the definition of Bean1
and Bean2
?
The hacky solution I've come up with is to simply define a dummy Bean2
in my test context, which will be loaded to satisfy the dependency, but I'd like a more elegant solution.
You can define Bean1 in it's own application context file. Include this file in the main application context like this:
<import resource="classpath:/applicationContext-bean1.xml" />
Now, create two almost identical files named "applicationContext-bean1.xml". Store one in your main application directory, and the other one in a test
directory. Don't put depends-on
in the file in the test
directory. Then, when you run your test, make sure the classpath include the test
directory before the main directory.
I don't know if this is a more elegant solution, but it is another way of addressing the problem. :)
精彩评论