@Test public void testWithPropertiesFile() throws Exception { PropertiesFactoryBean pfb = new PropertiesFactoryBean(); pfb.setLocation(TEST_PROPS); pfb.afterPropertiesSet(); Properties props = pfb.getObject(); assertEquals("99", props.getProperty("tb.array[0].age")); }
/** * Template method that subclasses may override to construct the object * returned by this factory. The default implementation returns the * plain merged Properties instance. * <p>Invoked on initialization of this FactoryBean in case of a * shared singleton; else, on each {@link #getObject()} call. * @return the object returned by this factory * @throws IOException if an exception occurred during properties loading * @see #mergeProperties() */ protected Properties createProperties() throws IOException { return mergeProperties(); }
@Bean public Properties propertyFactory() throws IOException { List<Resource> resources = loadPropertyResource(); PropertiesFactoryBean factory = new PropertiesFactoryBean(); factory.setLocations(resources.toArray(new Resource[resources.size()])); factory.afterPropertiesSet(); Properties props = factory.getObject(); new ConfigAccessor().get() .getProperties() .stream() .forEach(e -> { if (props.getProperty(e._1()) == null) { props.put(e._1(), e._2()); } }); System.getProperties() .entrySet() .forEach(e -> props.put(e.getKey(), e.getValue())); return props; }
@Bean(name = "mapper") public PropertiesFactoryBean mapper() { PropertiesFactoryBean bean = new PropertiesFactoryBean(); bean.setLocation(new ClassPathResource("com/foo/jdbc-production.properties")); return bean; }
@Bean(name = "versionProperties") public PropertiesFactoryBean versionProperties(@Value("classpath:META-INF/build.properties") Resource source) { PropertiesFactoryBean properties = new PropertiesFactoryBean(); properties.setLocation(source); properties.setIgnoreResourceNotFound(true); return properties; }
@Bean public PropertiesFactoryBean cvlExternalProperties(@Value("${MY_ENV_VAR}") String envVar) { PropertiesFactoryBean res = new PropertiesFactoryBean(); res.setFileEncoding("UTF-8"); res.setLocation(new FileSystemResource("file:" + envVar + "/applicationConfiguration/external.propert ies")); return res; }
@Bean(name = { BEAN_LOGSNIFFER_PROPS }) @Autowired public PropertiesFactoryBean logSnifferProperties(final ApplicationContext ctx) throws IOException { if (ctx.getEnvironment().acceptsProfiles("!" + ContextProvider.PROFILE_NONE_QA)) { final File qaFile = File.createTempFile("logsniffer", "qa"); qaFile.delete(); final String qaHomeDir = qaFile.getPath(); logger.info("QA mode active, setting random home directory: {}", qaHomeDir); System.setProperty("logsniffer.home", qaHomeDir); } final PathMatchingResourcePatternResolver pathMatcher = new PathMatchingResourcePatternResolver(); Resource[] classPathProperties = pathMatcher.getResources("classpath*:/config/**/logsniffer-*.properties"); final Resource[] metainfProperties = pathMatcher .getResources("classpath*:/META-INF/**/logsniffer-*.properties"); final PropertiesFactoryBean p = new PropertiesFactoryBean(); for (final Resource r : metainfProperties) { classPathProperties = (Resource[]) ArrayUtils.add(classPathProperties, r); } classPathProperties = (Resource[]) ArrayUtils.add(classPathProperties, new FileSystemResource(System.getProperty("logsniffer.home") + "/" + LOGSNIFFER_PROPERTIES_FILE)); p.setLocations(classPathProperties); p.setProperties(System.getProperties()); p.setLocalOverride(true); p.setIgnoreResourceNotFound(true); return p; }
@Bean public PropertiesFactoryBean configProperties() throws Exception{ PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean(); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); propertiesFactoryBean.setLocations(resolver.getResources("classpath*:application.properties")); return propertiesFactoryBean; }
return new PropertiesFactoryBean(); return new PropertiesFactoryBean();
super.setLocations(locations);
/** * Version. * * @return the properties factory bean */ @Bean(name = "stdout") public FactoryBean<Properties> stdout() { logger.debug("Instantiated stdout"); PropertiesFactoryBean bean = new PropertiesFactoryBean(); bean.setLocation(new ClassPathResource("stdout.properties")); return bean; }
public void setLocations(final Resource[] locations) { this.locations = locations; lastModified = new long[locations.length]; super.setLocations(locations); }
@Test public void testWithPropertiesXmlFile() throws Exception { PropertiesFactoryBean pfb = new PropertiesFactoryBean(); pfb.setLocation(TEST_PROPS_XML); pfb.afterPropertiesSet(); Properties props = pfb.getObject(); assertEquals("99", props.getProperty("tb.array[0].age")); }
/** * Version. * * @return the properties factory bean */ @Bean(name = "version") public PropertiesFactoryBean version() { logger.debug("Instantiated version"); PropertiesFactoryBean bean = new PropertiesFactoryBean(); bean.setLocation(new ClassPathResource("version.properties")); return bean; }
/** * Instantiates a new application context state. * * @throws IOException * Signals that an I/O exception has occurred. */ protected ApplicationContextState(boolean allowInitAccess ) throws IOException { this.allowInitAccess = allowInitAccess; // Load the property defaults PropertiesFactoryBean factory = new PropertiesFactoryBean(); factory.setPropertiesPersister(getPersister()); factory.setLocations(getParent().getResources( ChildApplicationContextFactory.CLASSPATH_PREFIX + getCategory() + '/' + getTypeName() + ChildApplicationContextFactory.PROPERTIES_SUFFIX)); factory.afterPropertiesSet(); this.properties = (Properties) factory.getObject(); }
/** * Template method that subclasses may override to construct the object * returned by this factory. The default implementation returns the * plain merged Properties instance. * <p>Invoked on initialization of this FactoryBean in case of a * shared singleton; else, on each {@link #getObject()} call. * @return the object returned by this factory * @throws IOException if an exception occurred during properties loading * @see #mergeProperties() */ protected Properties createProperties() throws IOException { return mergeProperties(); }
super.setLocations(locations);
@Test public void testWithLocalProperties() throws Exception { PropertiesFactoryBean pfb = new PropertiesFactoryBean(); Properties localProps = new Properties(); localProps.setProperty("key2", "value2"); pfb.setProperties(localProps); pfb.afterPropertiesSet(); Properties props = pfb.getObject(); assertEquals("value2", props.getProperty("key2")); }
@Bean(name = "myProperties") public static PropertiesFactoryBean mapper() { PropertiesFactoryBean bean = new PropertiesFactoryBean(); bean.setLocation(new ClassPathResource( "user-form-validation-configuration.properties")); return bean; }
/** * Instantiates a new application context state. * * @throws IOException * Signals that an I/O exception has occurred. */ protected ApplicationContextState(boolean allowInitAccess ) throws IOException { this.allowInitAccess = allowInitAccess; // Load the property defaults PropertiesFactoryBean factory = new PropertiesFactoryBean(); factory.setPropertiesPersister(getPersister()); factory.setLocations(getParent().getResources( ChildApplicationContextFactory.CLASSPATH_PREFIX + getCategory() + '/' + getTypeName() + ChildApplicationContextFactory.PROPERTIES_SUFFIX)); factory.afterPropertiesSet(); this.properties = (Properties) factory.getObject(); }