Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

There are several times when you need to specify a location in a Springframework configuration. An example is

<util:properties id="gsiProperties" location="classpath:gaming-adapter-default.properties"/>

A location is represented as a Resource. There are several ways to specify where to find the resource.

  • classpath: This represents a resource that should be retrieved from the classpath. It works the way a normal classloader does. The first match wins. Package names can also be represented as file directories (e.g. /this/that/my.properties
  • classpath:* This means that ALL resources that match the given name must be obtained, then merged to form a single resource. Meaning multiple property files could be mapped to. The sum of them would be used to supply the properties.
  • file: This is used to access a file system resource. This can be an absolute path or relative path.
  • http: This is used to access resources via HTTP protocol with a URL.
  • ftp: this is used to access a resource via FTP protocol.
  • blank This relies upon the implementation of the underlying context. For web applications, this utilizes the Servlet context.

July 28th, 2015

Posted In: java ninja, Javaninja, Spring

Tags: , , , , ,

Leave a Comment

I was loading a java resource via the classloader. I had the need to know where the file was on the file system.

    /**
     * Gets the absolute path of a file loaded via a classloader.
     * @param classpathFile file to be loaded from the classpath.
     * @return absolute file path of the file
     * @throws URISyntaxException
     */
    public static String getAbsoluteFilePath(String classpathFile) throws URISyntaxException {
        String absoluteFilePath = "";
        URL url = CommonUtil.class.getResource(classpathFile);
        if (url != null) {
            Path path = Paths.get(url.toURI());
            if (path != null) {
                absoluteFilePath = path.toString();
            }
        }
        return absoluteFilePath;
    }

February 14th, 2014

Posted In: Java, java ninja, Javaninja

Tags: , , , , , , , , ,

Leave a Comment

WP to LinkedIn Auto Publish Powered By : XYZScripts.com