Skip to main content

Posts

Showing posts with the label spring

Spring MVC: mvn sonar fails when mvn test passes

Stranger things have happened. But this blows many a mind. mvn test on a restful services project with spring mvc succeeds but mvn sonar:sonar fails with 404s on controllers. This happens when the controllers are marked @transactional for the restful service to be exposed as a unit of work. Don't fret. Its easy to solve this.

Enable alwaysUseFullPath with mvc:annotation-driven

mvc:annotation-driven doesnot have alwaysUseFullPath attribute. However, in instances this is needed, adding the following bean BEFORE mvc:annnotation-driven tag will set up the alwaysUseFullPath correctly <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" id="handlerMapping">         <property name="alwaysUseFullPath" value="true"> </property></bean> So the fuller xml should like this <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" id="handlerMapping">         <property name="alwaysUseFullPath" value="true">     </property></bean>  <mvc:annotation-driven>   <mvc:message-converters>    <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">     <property name="objectMapper" ref="jacks...

java turorials: Learn everything Java basic to enterprise

Java Programming basics Introduction to Java programming   ServerSide Java Download Tomcat Demo - Servlets and JSP JSP Tutorial IBM JSP tutorial   Enterprise Java JEE 5 tutorial Develop Web services with RAD Part1 Part2 Crash course in Enterprise JavaBeans 3   Struts IBM Tutorial Apache Wiki (List of Tutorials)   Spring Viral Patel’s turotial Reference Documentation   Ibatis JavaLobby Introduction Apache Tutorial   Hibernate JBoss Tutorial    

Hot Deploy resource file in Spring.

The general way to read a Resource file in Spring is something like this new ClassPathResource(pathToFile).getInputStream() However, this will cache the contents of the file (stream) in the class loader. Although this is not a major problem, there may be cases where reading the file fresh everytime (Hot deployable file) is required. That can be achieved with the following code new FileInputStream(new ClassPathResource(templateFile) .getFile().getAbsoluteFile()); This will gives cache free control on the file and its content. (Important) Note : * Doing the AbsoluteFile way, will always read the content of the file. So if it is being done on a High frequency, A Managed Caching layer must be considered * AbsoluteFile call will NOT work for resource files inside jars, they have to be in classpath folders (e.g.WEB-INF/classes)

Spring Framework 3.0 GA released!

SpringSource announced its GA release today. My personal reasons to "go for it" - REST and Huge improvements in Annotation support. It will be interesting to see how this release is accepted by the community. 2.5.6 seems just yesterday. People (like me) with roots (and lot of existing applications (with issues )) in Spring 2 are just getting their feet wet with migration to 2.5.6 and the magical world of annotation based configuration. Now, I have to calculate ROI on migrating my still-in-development applications (on 2.5.6) to 3.0. ah. I am so in a jinx.

Error creating bean with name 'tilesConfigurer'

While building a skeleton web app, using Spring and Tiles, I encountered this strange error. I am trying to configure Spring 2.5.6 and Tiles 2.2.1 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tilesConfigurer' defined i n ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.UnsupportedOperationException: Class org.apache.tiles.web.util.ServletContextAdapter not recognized a TilesApplicationContext at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) at java.security.AccessController....

MANIFEST.MF in Plain Java, EJB, OSGi - catching the drift

While reading about OSGi, I realised how the importance of MANIFEST.MF has grown over time. The once only-a-jar-information file has had its ups and downs. When I was in school, *chapter jar* was an "assignment" / "reference". It wasn't worth a class' time spent on it. Today a whole new technology relies on this humble text file. Past: Long long ago, in stone age (yeah I am exaggerating), the MANIFEST.MF was auto-generated while jar-ing. More or less it was not used. Well, yeah if you are developing a java application to be distributed to end-users (the jars you used to double click, which are so replaced by jnlp a.k.a WebStart apps now). This file would give a place to specify the class path Manifest-Version: 1.0 Created-By: 1.4.2 (Sun Microsystems Inc.) Main-Class: com.big.long.package.MyAppMain Class-Path: mod1.jar mod2.jar lib/lib.jar This will be set when running java -jar myapp.jar which other wise would have to be something like java -jar myap...

Mission OSGi

Finally some time for OSGi. With release of Spring dm Server, My interest for OSGi got two-fold. Although I did have some close-encounters with OSGi (aah its just a jar called differently - bundle; and MANIFEST.MF drives it all, o I know OSGi), I haven't read into a lot of it. So now, I am diving into it. To start I picked Neil's blog . He also has a draft of his upcoming book, OSGi in Practice . So It is a good place to start. That gives me to start a new tag (osgi) :)

classpath*: making your Modular Spring Resources

Spring gives multiple options to load XML resources for building contexts. the reference documentation does explain this feature quite well. However, I am taking my shot at explaining the different practical scenarios ( by order of growing modularisation) For Example, A simplest Spring based web Context Loader can be configured with resources like this <context-param> <param-name>contextConfigLocation</param-name> <param-value>applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> You just need to put applicationContext.xml in WEB-INF/ folder of your webapp. However, Typically an application is n-tiered. You can also have multiple files setup and in relative paths. like <param-value> context-files/applicationContext.xml context-files/dao.xml context-files/service.xml </param-value> ...

Using Spring XML Schema based bean definitions

Spring's bean definitions (and configuration files) may go very verbose after your projects goes two or three iterations old. By verbose I mean, too many bean definitions, and complex injection graph. And many a times, these xml files go fat because of common definition types - props, lists, etc. These beans are generally configured using FactoryBean s. To reduce this verbatim Spring provides XML schema based configuration, for various types of FactoryBeans. There are some shortcuts presented in that appendix, I frequently use. I recomend those to others too. Here are some of those. <!-- creates a java.util.Properties instance with values loaded from the supplied location --> <bean id="jdbcConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location" value="classpath:com/foo/jdbc-production.properties"/> </bean> Could be written as <!-- creates a java.uti...

Acegi onSuccessfulAuthentication

Some time back, I raised a jira in Acegi, which was turned down. There was no adequate response from spring team. Here I am writing about it, so other spring users (read gurus) may pitch in. //Edited to short and precise protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, Authentication authResult) throws IOException { SecurityContextHolder.getContext().setAuthentication(authResult); String targetUrl = (String) request.getSession() .getAttribute(ACEGI_SECURITY_TARGET_URL_KEY); request.getSession().removeAttribute(ACEGI_SECURITY_TARGET_URL_KEY); if (alwaysUseDefaultTargetUrl == true) { targetUrl = null; } if (targetUrl == null) { targetUrl = request.getContextPath() + defaultTargetUrl; } // user call back hook onSuccessfulAuthentication (request, response, authResult); //... response.sendRedirect(response.encodeRedirectURL(targetUrl)); } The Acegi fr...

Spring Security Advisory

Many people consider this is not new. But there is a security advisory out for spring users. http://www.springsource.com/securityadvisory Quote: For applications that bind request data to presentation-layer "form models", this is unlikely to be a problem since there is a one-to-one correspondence between a form backing object and a set of allowed request parameters. This issue is only relevant for applications that bind directly to a domain model which exposes properties that should not be updated by the client. To understand it more clearly, look at an Example: http://www.celerity.nl/blog/2008/04/security-implications-of-the-spring-databinder/ This is however an advanced usecase. If you are directly binding model to form objects you have a major design issue. If you observe carefully, There is another case that we dont often remember. Most Web apps have CRUD jsps and the Update jsp will generally include the id of the model that is being edited. Most times, The only security...

Success of Open Source Projects : all hail Spring

What makes an Open Source Project successful? When I started web frameworks with Struts 1.1 in 2004, I had a VERY hard time learning the framework. People who have been in the same boat would remember these key words "Struttin' with Struts" which would google to Rick Reumann 's 4 or 5 chapter tutorial - that was by far the only DECENT tutorial available at that time. There may be more now, but I am not in the hunt. At around the same time, Spring was coming up. And boy did it catch up like fire. The success story highlighted the importance of documentation. Rod Johnson's Team got it right in the first time. They realised that the popularity of an opensource project is directly proportional to the quality and quantity of its documentation. Ever since, you can see there are tons documentation for anything they release, in its own space. These include a lot of examples, code framgments etc. They still need centralise the individual projects to the main documentati...