Java Code Examples for

Following code examples demonstrate how to use spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use various code implementation of this class.

	public static void main(String[] args) {
		Resource resource = new ClassPathResource("applicationContext.xml");
		BeanFactory factory = new XmlBeanFactory(resource);
		MessageBean bean = (MessageBean) factory.getBean("m");

	public static Object getService(String servicename){
			ClassPathResource res =  new ClassPathResource("spring-cfg.xml");
			bf = new XmlBeanFactory(res);
		Object service = bf.getBean(servicename);
		return service;

	public static String readStringByClasspathResource( String fileClassPath, String encoding ) throws IOException{
		Resource res = new ClassPathResource( fileClassPath );
		EncodedResource enRes = new EncodedResource( res, encoding );
		return FileCopyUtils.copyToString( enRes.getReader() );

	public EhCacheManagerFactoryBean ehCacheCacheManager() {
		EhCacheManagerFactoryBean factory = new EhCacheManagerFactoryBean();
		factory.setConfigLocation(new ClassPathResource("ehcache.xml"));
		return factory;

    public static void validateBySchema(Object jaxb, String filePath) throws SAXException, IOException, JAXBException, SAXParseException {

            SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = schemaFactory.newSchema(new ClassPathResource(filePath).getFile());
            JAXBContext jaxbContext = JAXBContext.newInstance(jaxb.getClass());
            Validator validator = schema.newValidator();
            Source source = new JAXBSource(jaxbContext, jaxb);


Resource implementation for class path resources. Uses either a given ClassLoader or a given Class f

or loading resources.

Supports resolution as if the class path resource resides in the file system, but not for resources in a JAR. Always supports resolution as URL. @author Juergen Hoeller @author Sam Brannen @since 28.12.2003 @see ClassLoader#getResourceAsStream(String) @see Class#getResourceAsStream(String)

Read More