org.greencheek.utils.environment.propertyplaceholder.examples.spring.profile.AppBootstrap.java Source code

Java tutorial

Introduction

Here is the source code for org.greencheek.utils.environment.propertyplaceholder.examples.spring.profile.AppBootstrap.java

Source

/*
 * Copyright 2012 dominictootell
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.greencheek.utils.environment.propertyplaceholder.examples.spring.profile;

import org.greencheek.utils.environment.propertyplaceholder.builder.EnvironmentSpecificPropertiesMergerBuilder;
import org.greencheek.utils.environment.propertyplaceholder.merger.PropertiesMerger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;

import java.util.Properties;

/**
 * User: dominictootell
 * Date: 10/06/2012
 * Time: 14:42
 */

@Configuration
public class AppBootstrap implements ApplicationContextInitializer {

    private static Properties environmentalProperties;
    static {
        PropertiesMerger merger = new EnvironmentSpecificPropertiesMergerBuilder().build();
        environmentalProperties = merger.getMergedProperties();
    }

    public static void initialise(ConfigurableApplicationContext applicationContext) {
        applicationContext.getEnvironment().getPropertySources()
                .addFirst(new PropertiesPropertySource("p", environmentalProperties));
    }

    @Autowired
    private EnvBean envBean;

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer config = new PropertySourcesPlaceholderConfigurer();

        MutablePropertySources sources = new MutablePropertySources();
        sources.addFirst(new PropertiesPropertySource("p", environmentalProperties));

        config.setPropertySources(sources);

        return config;
    }

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        initialise(applicationContext);
    }

}