2

I'm working on building a custom "event" library that encapsulates the technical details of an event buffer we are planning to share with multiple consumers. Ideally, we want this library to use the spring framework (note: not spring boot), and be environmentally aware. Something I am not groking from the current docs is how to make the library environmentally aware.

For example, we want to include with the library a static configuration for the queue end points the library will publish / consume from; however, we want to enable "overriding" these queues when in the development or integration environments. Ideally, I do not want to make multiple builds that swap out what the config file is, but include them all and know to read the "right" one.

Some of the things I am not understanding; How to pass in a "profile" when debugging (it seems the Environment object won't honor the -Dspring.active.profiles property). How to structure the @Configuration classes so that you do not hard code @Profile(prod).

Total Spring n00b, thanks in advance!

---UPDATE: Trying to provide a more concrete example.

So I have create a basic configuration class to hold the details that would be populated by configuration files:

@Configuration
public class EventConfiguration implements EnvironmentAware{



private static Environment env = null;

    @Value("${events.queue1}")
    private String queue1;
    @Value("${events.queue2}")
    private String queue2;


    @Bean
    public EventDispatcher eventDispatcher() {
        return new EventDispatcher(this);
    }

    @Override
    public void setEnvironment(Environment environment) {
        env = environment;
    }
    ... getters and setters

Essentially I want to either go the yaml approach and define the queues by environment "dev", "integration", "prod"; or have 3 different files following the application-{env}.properties convention.

Then, to help me understand how this works, I threw together a quick test so I can inspect the configuration / environment:

@Test
public void testContext() {

    AnnotationConfigApplicationContext ctx =
             new AnnotationConfigApplicationContext();
    Environment env = ctx.getEnvironment();
    ctx.scan("com...events");
    ctx.refresh();

    EventDispatcher dispatcher = ctx.getBean(EventDispatcher.class);

}

I started the debugger with a -Dspring.profiles.active=dev, after having created an application-dev.profile available on the class path.

Am I on the right track? Seems weird to have to have that type of boiler plate code to instantiate the objects, plus it didnt work. The Environment object only showed "default" as the active profile.

Matthew Ward
  • 351
  • 3
  • 12
  • This is pretty broad. The current best practice, generally speaking, is to provide a Spring Boot starter, which essentially is just responsible for conditionally activating an `@Configuration` class. If the client doesn't want to use Boot, then they can choose how to import the class. If you provide a concrete example, you might be able to get a more specific answer. – chrylis -cautiouslyoptimistic- Oct 30 '15 at 05:45
  • http://stackoverflow.com/questions/9093768/can-we-override-the-spring-beans-declared-in-one-xml-config-in-another-config This might answer your question, something similar is done in Broadleaf commerce framework I just came across, there they have used some LateStageMergeBeanPostProcessor – Bilbo Baggins Oct 30 '15 at 05:46
  • Thanks @chrylis, I provided a more concrete example of what I have been toying with. – Matthew Ward Oct 30 '15 at 06:03
  • Just using a profile and expecting it to load `application-{profile}.properties` isn't going to work as that is a Spring Boot feature not a core Spring Framework feature. For the test use `@ActiveProfiles` instead of en environment variable. – M. Deinum Oct 30 '15 at 06:10
  • What would be the approach to take if I just wanted to use spring framework in this case? – Matthew Ward Oct 30 '15 at 06:11
  • It looks like you're reimplementing Spring Integration. Is there something significantly different in your setup? – chrylis -cautiouslyoptimistic- Oct 30 '15 at 08:02

0 Answers0