Wednesday, October 21, 2009

Set Character Encoding to log4j's File Log

Working with east asian characters? Do you want log4j to print out utf-8 characters on your log file? Here is how:

When you're using a log4j.properties file:


log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.encoding=UTF-8

When youre using a log4j.xml file:

<appender name="R"
              class="org.apache.log4j.DailyRollingFileAppender">
        <param name="File" value="info-log.log"/>
        <param name="Encoding" value="UTF-8"/>
        ...
        ...
</appender>
Enjoy!

Wednesday, October 7, 2009

Loading a file from classpath

There are instances that you want to load a certain file from your classpath, the following code does that:

private InputStream getFileFromClassPath(String fileName) throws IOException {

        ClassLoader cl = ClassLoader.getSystemClassLoader();
        URL url = cl.getResource(fileName);
        if (url == null) {
            throw new NullPointerException("File with filename " + fileName + " not found on classpath");
        } else {
            return url.openStream();
        }
       
 }

Tuesday, October 6, 2009

How to define multiple PropertyPlaceholderConfigurer beans

I came across a problem today regarding the app that I am working on wherein I have to define a different PropertyPlaceholderConfigurer bean for each of my context xml files. (Each context xml file is imported in another xml file so it became a little bit tricky).

What I did was, I defined a PropertyPlaceholderConfigurer bean on the first context xml file and I defined another one on the second, so that each context xml can have it's own distinct PropertyPlaceholderConfigurer bean. But upon doing so, I got the following error

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'myClass' defined in class path resource [spring-config.xml]: Could not resolve placeholder 'props.value.2'

After google-ing a bit I found the answer to my problem on an entry on Parth Barot's blog. Please take a look!