Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Monday, December 17, 2012

Problems reading Log4j.xml from outside web application


Problems reading Log4j.xml from outside web application


If you try to read the log4j.xml from outside the Spring application/classpath, you might get the following errors, if you are deploying a war file:

Stack trace


org.springframework.web.util.Log4jConfigListener failed: java.lang.IllegalStateException: Cannot set web app root system property when WAR file is not expanded.
java.lang.IllegalStateException: Cannot set web app root system property when WAR file is not expanded
                        at org.springframework.web.util.WebUtils.setWebAppRootSystemProperty(WebUtils.java:137)
                        at org.springframework.web.util.Log4jWebConfigurer.initLogging(Log4jWebConfigurer.java:117)
                        at org.springframework.web.util.Log4jConfigListener.contextInitialized(Log4jConfigListener.java:45)


The explanation is given below:

Summary -  org.springframework.web.util.Log4jConfigListener only works if the WAR file is exploded
The same with HDIV

Configuration


log4jConfigLocation
file:/${CONFIG_LOCATION}/log4j.xml
 

Solution

To override this we need to add the following to the web.xml

<context-param></context-param>
    <param-name>log4jExposeWebAppRoot</param-name>
<param-value>false</param-value>

Thursday, January 12, 2012

Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEYS to Statement.executeUpdate() using Spring JdbcTemplate

My colleague was getting this exception for a Spring JdbcTemplate based code which I had written for some simple Mysql dao functionalities. The funny point was that we were both using the same version of Mysql drivers, but I was using 32bit Mysql 5.5 but my colleague was using the 64bit version.

This quick fix solves the problem:


KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator() {
@Override
 public PreparedStatement createPreparedStatement(Connection aConnection) throws SQLException {
                    PreparedStatement preparedStatement = aConnection.prepareStatement(SAVE_USER,
PreparedStatement.RETURN_GENERATED_KEYS);
                    preparedStatement.setString(....);
                    ....
                         preparedStatement.setString(....);
                    return preparedStatement;
                }
            }, keyHolder);
 user.setId(keyHolder.getKey().intValue());

A few other fellow bloggers had given suggestions like switching the Mysql dirvers, but it's didn't help me at all. I had tried all the last 10 Mysql driver releases. I am not sure if its a programming error or a Mysql bug, but the above code does fix the problem!

Happy Hacking :)

Wednesday, June 22, 2011

org.springframework.beans.FatalBeanException: Could not copy properties from source to target

I came across this irritating issue(While workign on a Spring based project).

org.springframework.beans.FatalBeanException: Could not copy properties from source to target; nested exception is java.lang.IllegalArgumentException: argument type mismatch
at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:599)
at org.springframework.beans.BeanUtils.copyProperties(BeanUtils.java:509)

Google took me to this bug which was already logged with SpringSource:

https://jira.springsource.org/browse/SPR-7693 (unfortunately it's still unfixed)

I made a quick fix/workaround for this.

Download/checkout : org.springframework.beans.BeanUtils.java

Add the below line after: writeMethod.invoke(target, value); (Around line 597) if using the 3.1 source.



if(logger.isDebugEnabled()){
logger.debug(MessageFormat.format("Source Property: {0}, Target Property:   {1}, Write method:{2}, Read Method {3} ", sourcePd.getName(),  targetPd.getName(), writeMethod.getName(), readMethod.getName()));
}

Add the file to your project. Now you can happily debug which property in the class is causing  problem.