2008年7月23日 星期三

Build Spring & Hibernate based application from scratch

Introduce
I was used to develop my system based on some well designed and integrated framework such as Appfuse. The integrated framework can shorten a lot of development time. So, I never think about build my own application from scratch. Until I starting to code the system for my PHD dissertation.
The major goal of dissertation is to develop a manufacture information platform for distributed mass customization by using inteligence agent. In order to let my agent can inline with my previous development effort, Spring and Hibernate framework integration become my first step to conquer.

Project Layout
Figure 1. shows the project layout. The most important things in Figure 1 is the lib folder.


Figure 1. Project layout
In order to use Spring and Hibernate in my project, I have to import these related jar files into the build path of my project.
The most easy way to test the project layout is correct or not is write a small program to load the Spring configuration.
For Example, create a Java class with a static main function and in the declare area put the following code.


protected final static ApplicationContext ctx;
static{
String[] paths = {"applicationContext.xml",};
ctx = new FileSystemXmlApplicationContext(paths);
}

public static void main(String[] args)
throws IOException
{
}

Well, you will need the Spring configuration file. I named this configuration as ApplicationContext.xml and list as following:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/agentpjm?useUnicode=true&amp;characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="h300"/>
<property name="maxActive" value="100"/>
<property name="maxIdle" value="30"/>
<property name="maxWait" value="1000"/>
<property name="defaultAutoCommit" value="true"/>
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="60"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
<property name="hibernateProperties">
<value>
hibernate.dialect=${hibernate.dialect}
hibernate.query.substitutions=true 'Y', false 'N'
hibernate.cache.use_second_level_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
</value>
<!-- Turn batching off for better error messages under PostgreSQL -->
<!-- hibernate.jdbc.batch_size=0 -->
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>

Put this file in the root of the project folder and run the program, there should be no exception appear in the window, then we can start to add Hibernate configuration and prepare the data access object(DAO).

Reference.
  1. http://www.findjar.com
  2. http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/
  3. http://www.hibernate.org/hib_docs/reference/en/html/session-configuration.html

沒有留言: