Friday, 12th March 2010
The new frontier for learning Java

Best Pratices and Tweaks in Spring Framework

From WikiJava

Jump to: navigation, search




This page is or contains a derivative version of a portion of this GFDL content
The article was originally sourced from Ganesh Gowtham.

Ganesh Gowtham's Website
Pre requisite for spring can be found at Sping Basics

Contents

Summary

In this article we will see some best of practices that we can incorporate using theSpring Framework for the folks who had just crossed beginner level (or) who are very enthusiastic to know more @ younger age (Beginner) :-)

What is Spring Framework

1

Main Modules of Spring Framework

2

Design patterns used in Spring Framework

3

Usage of JNDI,JdbcTemplate,DelegatingActionProxy,Junit

For Junit which used AbstractTransactionalSpringContextTests api , Please refer my another article AbstractTransactionalSpringContextTests Example
struts-config.xml

<struts-config>
   ...
   <action-mappings>
      <action path="/searchWikiUser"
         type="org.springframework.web.struts.DelegatingActionProxy">
         <forward name="success" path="/pages/displayWikiUsers.jsp" />
      </action>
      ...
   </action-mappings>
   ....
</struts-config>

spring-persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
   <!-- data source -->
   <bean class="org.apache.commons.dbcp.BasicDataSource" id="wikiDataSource"
      destroy-method="close">
      <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
      <property name="url" value="jdbc:oracle:thin:@dbserver:portNO:dbName" />
      <property name="username" value="scott" />
      <property name="password" value="elephant" />
      <property name="defaultAutoCommit" value="true" />
      <property name="maxActive" value="30" />
   </bean>
   <!-- jdbctemplate -->
   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
      <property name="dataSource" ref="wikiDataSource" />
   </bean>
   <!-- dao -->
   <bean id="wikiUserDao" class="com.jpratice.dao.WikiUserDao">
      <property name="jdbcTemplate" ref="jdbcTemplate" />
   </bean>
</beans>

spring-serivice.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
   <bean id="wikiUserService" class="com.jpratice.service.WikiUserService">
      <property name="wikiUserDao" ref="wikiUserDao" />
   </bean>
</beans>

spring-presentation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
   <bean name="/searchWikiUser" class="com.jpratice.action.SearchWikiUsers">
      <property name="wikiUserService" ref="wikiUserService" />
   </bean>
</beans>

spring-jndi.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
   <bean class="com.jpratice.bean.EnvBean" id="EnvBean">
      <property name="deployedEnv">
         <bean class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="java:comp/env/deployedEnv" />
         </bean>
      </property>
      <property name="deployedAppVersion">
         <bean class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName" value="java:comp/env/deployedAppVersion" />
         </bean>
      </property>
   </bean>
</beans>
</property>

spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
   <import resource="/spring_persistence.xml" />
   <import resource="/spring_service.xml" />
   <import resource="/spring_presentation.xml" />
   <import resource="/spring_jndi.xml" />
</beans>
4

Importing the different Spring files for clean separation based on different layers

5

Importing the properties file present in jar which is kept @ classpath

6

Time taken by each method execution

7

configuring the Struts ,Spring with tomcat context file

8

References

Spring Framework
Spring Framework Autowire
Junit
What is AbstractTransactionalSpringContextTests
JdbcTemplate
JndiObjectFactoryBean
DelegatingActionProxy

Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page


Name (required):

Website:

Comment:

Facts about Best Pratices and Tweaks in Spring FrameworkRDF feed
Category Spring  +
Keyword AbstractTransactionalSpringContextTests  +, JdbcTemplate  +, junit  +, DelegatingActionProxy  +, JndiObjectFactoryBean  +, and BasicDataSource  +
Resource Java SDK 1.4 (or higher)  +, Spring Framework  +, and Junit  +
Learn Java today! Java sample code jsp tutorials All about Java basics