Lets assume that I have sequencer and the following Beans. Sequencer is responsible for taking the next value from a database sequence. OracleSequencer is used in the production mode, TestSequencer during JUnit-Tests.
public interface Sequencer{
long getNextValue();
}
@Named("OracleSequencer")
public class OracleSequencer implements Sequencer[
...
}
@Named("TestSequencer")
public class TestSequencer implements Sequencer[
...
}
And this is how I want to use it:
@Named
public class BusinessBean{
@Inject
private Sequencer sequencer;
}
My problem is: When I run the app in the production mode I want to inject OracleSequencer in BusinessBean, when I run JUnit-Test I want to inject TestSequencer. Is it possible with JSR-330 (@Inject, @Named) ?
I found 2 dirty solutions:
at the begining of a test, Sequencer can be replaced in the bean, which is taken from Spring-ApplicationContext. It is possible when prototype is appropriate and TestSequencer doesnt have any other dependencies:
BusinessBean bean = springContext.getBean(..); bean.setSequencer(new TestSequencer());
We can annotate OracleSequencer with @ProductionScope and TestSequencer with @TestScope and configure Spring that he scans appropiate annotations in the appropriate mode.
Do you have maybe any other idea how to solve this problem?
Spring 3.1.