Java Code Examples for org.springframework.context.ApplicationContext#getResource()

The following examples show how to use org.springframework.context.ApplicationContext#getResource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XmlViewResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Initialize the view bean factory from the XML file.
 * Synchronized because of access by parallel threads.
 * @throws BeansException in case of initialization errors
 */
protected synchronized BeanFactory initFactory() throws BeansException {
	if (this.cachedFactory != null) {
		return this.cachedFactory;
	}

	ApplicationContext applicationContext = obtainApplicationContext();

	Resource actualLocation = this.location;
	if (actualLocation == null) {
		actualLocation = applicationContext.getResource(DEFAULT_LOCATION);
	}

	// Create child ApplicationContext for views.
	GenericWebApplicationContext factory = new GenericWebApplicationContext();
	factory.setParent(applicationContext);
	factory.setServletContext(getServletContext());

	// Load XML resource with context-aware entity resolver.
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
	reader.setEnvironment(applicationContext.getEnvironment());
	reader.setEntityResolver(new ResourceEntityResolver(applicationContext));
	reader.loadBeanDefinitions(actualLocation);

	factory.refresh();

	if (isCache()) {
		this.cachedFactory = factory;
	}
	return factory;
}
 
Example 2
Source File: ResourceHttpRequestHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void resolveResourceLocations() {
	if (CollectionUtils.isEmpty(this.locationValues)) {
		return;
	}
	else if (!CollectionUtils.isEmpty(this.locations)) {
		throw new IllegalArgumentException("Please set either Resource-based \"locations\" or " +
				"String-based \"locationValues\", but not both.");
	}

	ApplicationContext applicationContext = obtainApplicationContext();
	for (String location : this.locationValues) {
		if (this.embeddedValueResolver != null) {
			String resolvedLocation = this.embeddedValueResolver.resolveStringValue(location);
			if (resolvedLocation == null) {
				throw new IllegalArgumentException("Location resolved to null: " + location);
			}
			location = resolvedLocation;
		}
		Charset charset = null;
		location = location.trim();
		if (location.startsWith(URL_RESOURCE_CHARSET_PREFIX)) {
			int endIndex = location.indexOf(']', URL_RESOURCE_CHARSET_PREFIX.length());
			if (endIndex == -1) {
				throw new IllegalArgumentException("Invalid charset syntax in location: " + location);
			}
			String value = location.substring(URL_RESOURCE_CHARSET_PREFIX.length(), endIndex);
			charset = Charset.forName(value);
			location = location.substring(endIndex + 1);
		}
		Resource resource = applicationContext.getResource(location);
		this.locations.add(resource);
		if (charset != null) {
			if (!(resource instanceof UrlResource)) {
				throw new IllegalArgumentException("Unexpected charset for non-UrlResource: " + resource);
			}
			this.locationCharsets.put(resource, charset);
		}
	}
}
 
Example 3
Source File: XmlViewResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Initialize the view bean factory from the XML file.
 * Synchronized because of access by parallel threads.
 * @throws BeansException in case of initialization errors
 */
protected synchronized BeanFactory initFactory() throws BeansException {
	if (this.cachedFactory != null) {
		return this.cachedFactory;
	}

	ApplicationContext applicationContext = obtainApplicationContext();

	Resource actualLocation = this.location;
	if (actualLocation == null) {
		actualLocation = applicationContext.getResource(DEFAULT_LOCATION);
	}

	// Create child ApplicationContext for views.
	GenericWebApplicationContext factory = new GenericWebApplicationContext();
	factory.setParent(applicationContext);
	factory.setServletContext(getServletContext());

	// Load XML resource with context-aware entity resolver.
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
	reader.setEnvironment(applicationContext.getEnvironment());
	reader.setEntityResolver(new ResourceEntityResolver(applicationContext));
	reader.loadBeanDefinitions(actualLocation);

	factory.refresh();

	if (isCache()) {
		this.cachedFactory = factory;
	}
	return factory;
}
 
Example 4
Source File: ResourceHttpRequestHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void resolveResourceLocations() {
	if (CollectionUtils.isEmpty(this.locationValues)) {
		return;
	}
	else if (!CollectionUtils.isEmpty(this.locations)) {
		throw new IllegalArgumentException("Please set either Resource-based \"locations\" or " +
				"String-based \"locationValues\", but not both.");
	}

	ApplicationContext applicationContext = obtainApplicationContext();
	for (String location : this.locationValues) {
		if (this.embeddedValueResolver != null) {
			String resolvedLocation = this.embeddedValueResolver.resolveStringValue(location);
			if (resolvedLocation == null) {
				throw new IllegalArgumentException("Location resolved to null: " + location);
			}
			location = resolvedLocation;
		}
		Charset charset = null;
		location = location.trim();
		if (location.startsWith(URL_RESOURCE_CHARSET_PREFIX)) {
			int endIndex = location.indexOf(']', URL_RESOURCE_CHARSET_PREFIX.length());
			if (endIndex == -1) {
				throw new IllegalArgumentException("Invalid charset syntax in location: " + location);
			}
			String value = location.substring(URL_RESOURCE_CHARSET_PREFIX.length(), endIndex);
			charset = Charset.forName(value);
			location = location.substring(endIndex + 1);
		}
		Resource resource = applicationContext.getResource(location);
		this.locations.add(resource);
		if (charset != null) {
			if (!(resource instanceof UrlResource)) {
				throw new IllegalArgumentException("Unexpected charset for non-UrlResource: " + resource);
			}
			this.locationCharsets.put(resource, charset);
		}
	}
}
 
Example 5
Source File: SpringResoucesTest.java    From spring-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@Test
public void testGetResource() {
	ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/*.xml");
	Resource resource = ctx.getResource("spring/spring-beans.xml");
	Assert.assertNotNull(resource);
	((ClassPathXmlApplicationContext) ctx).close();
}
 
Example 6
Source File: BuildConfig.java    From hasor with Apache License 2.0 5 votes vote down vote up
public Hasor build(Object parentObject, ApplicationContext applicationContext) throws IOException {
    Hasor hasorBuild = (parentObject == null) ? Hasor.create() : Hasor.create(parentObject);
    hasorBuild.parentClassLoaderWith(applicationContext.getClassLoader());
    //
    // make sure mainConfig
    String config = this.mainConfig;
    if (!StringUtils.isBlank(config)) {
        config = SystemPropertyUtils.resolvePlaceholders(config);
        Resource resource = StringUtils.isNotBlank(config) ? applicationContext.getResource(config) : null;
        if (resource != null) {
            hasorBuild.mainSettingWith(resource.getURI());
        }
    }
    //
    // merge Properties
    if (this.envProperties != null) {
        this.envProperties.forEach((k, v) -> {
            hasorBuild.addVariable(k.toString(), v.toString());
        });
    }
    if (this.refProperties != null) {
        this.refProperties.forEach((k, v) -> {
            hasorBuild.addVariable(k.toString(), v.toString());
        });
    }
    if (this.customProperties != null) {
        this.customProperties.forEach((k, v) -> {
            hasorBuild.addVariable(k.toString(), v.toString());
        });
    }
    //
    // import Properties to Settings
    if (this.useProperties) {
        hasorBuild.importVariablesToSettings();
    }
    //
    return hasorBuild.addModules(this.loadModules);
}
 
Example 7
Source File: WallRideResourceTemplateResource.java    From wallride with Apache License 2.0 5 votes vote down vote up
public WallRideResourceTemplateResource(final ApplicationContext applicationContext, final String location, final String characterEncoding) {
	super();

	Validate.notNull(applicationContext, "Application Context cannot be null");
	Validate.notEmpty(location, "Resource Location cannot be null or empty");
	// Character encoding CAN be null (system default will be used)

	this.resource = applicationContext.getResource(location);
	this.characterEncoding = characterEncoding;
}
 
Example 8
Source File: DataDictionary.java    From kfs with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * ApplicationContext aware version of method
 */
protected Resource getFileResource(String sourceName, ApplicationContext applicationContext) {
    return applicationContext.getResource(sourceName);
}