0

So on an spring 3 app I'm building, it would be convenient to be able to read a value from configuration, and then based off of the value read, choose between two implementations of an interface and then build a bean of that object.

I have the config file setup (using util:properties), and other java code is accessing it just fine, but I'm uncertain about how to reference it in my application context xml file, and how to code this conditional logic. Or am I going about this incorrectly?

Nixx
  • 370
  • 3
  • 20

1 Answers1

0

You probably want something similar to this:

@Configuration
public class MyConfiguration {

  @Autowired
  ApplicationContext applicationContext;

  @Value("${someProperty}")
  String someProperty = "B";

  @Bean(name="myBean")
  public MyInterface ehCacheManager() {
    if ("A".equals(someProperty)) {
        return new MyInterfaceA();
    } else {
        return new MyInterfaceB();
    }
  }
}

You can then differentiate by some property which interface implementation to use.

Bruce Lowe
  • 6,063
  • 1
  • 36
  • 47
  • I was thinking about doing java config, but would it be possible to write it into the xml config instead though? – Nixx Sep 09 '13 at 16:29