Example usage for org.springframework.context.support PropertySourcesPlaceholderConfigurer postProcessBeanFactory

List of usage examples for org.springframework.context.support PropertySourcesPlaceholderConfigurer postProcessBeanFactory

Introduction

In this page you can find the example usage for org.springframework.context.support PropertySourcesPlaceholderConfigurer postProcessBeanFactory.

Prototype

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException 

Source Link

Document

Processing occurs by replacing ${...} placeholders in bean definitions by resolving each against this configurer's set of PropertySources , which includes:
  • all org.springframework.core.env.ConfigurableEnvironment#getPropertySources environment property sources , if an Environment #setEnvironment is present
  • #mergeProperties merged local properties , if #setLocation any #setLocations have #setProperties been #setPropertiesArray specified
  • any property sources set by calling #setPropertySources

If #setPropertySources is called, environment and local properties will be ignored.

Usage

From source file:de.thischwa.pmcms.conf.BasicConfigurator.java

private void init() {
    if (System.getProperty("data.dir") == null)
        throw new IllegalArgumentException("No data directory set!");
    dataDir = new File(System.getProperty("data.dir"));
    if (!dataDir.exists())
        throw new IllegalArgumentException(
                String.format("Data directory not found: %s", dataDir.getAbsolutePath()));

    // load and merge the properties 
    loadProperties();/*from w ww  . j  ava 2s . c  o m*/

    // build special props
    String baseUrl = String.format("http://%s:%s/", props.get("pmcms.jetty.host"),
            props.get("pmcms.jetty.port"));
    props.setProperty("baseurl", baseUrl);
    props.setProperty("data.dir", dataDir.getAbsolutePath());
    System.setProperty("content.types.user.table",
            new File(Constants.APPLICATION_DIR, "lib/content-types.properties").getAbsolutePath());

    // init log4j
    LogManager.resetConfiguration();
    PropertyConfigurator.configure(PropertiesTool.getProperties(props, "log4j"));
    Logger logger = Logger.getLogger(BasicConfigurator.class);
    logger.info("*** log4j initialized!");

    // init the spring framework
    try {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.scan("de.thischwa.pmcms");
        PropertySourcesPlaceholderConfigurer config = new PropertySourcesPlaceholderConfigurer();
        config.setProperties(props);
        config.postProcessBeanFactory(ctx.getDefaultListableBeanFactory());
        ctx.refresh();
        context = ctx;
        logger.info("*** Spring initialized!");
        PropertiesManager pm = context.getBean(PropertiesManager.class);
        pm.setProperties(props);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    logger.info("*** Basic configuration successful done.");
}