Anda di halaman 1dari 8

In this section we will wire up the components by defining the Application context

definition file (applicationContext-hibernate.xml). This file is used by context loader


servlet to initialize the Spring application context.

About Application ApplicationContext


An ApplicationContext is the central interface to provide configuration for an application.

An ApplicationContext provides the following functionalities:

• Bean factory methods, inherited from ListableBeanFactory. This avoids the need
for applications to use singletons.
• The ability to resolve messages, supporting internationalization. Inherited from
the MessageSource interface.
• The ability to load file resources in a generic fashion. Inherited from the
ResourceLoader interface.
• The ability to publish events. Implementations must provide a means of
registering event listeners.
• Inheritance from a parent context. Definitions in a descendant context will always
take priority. This means, for example, that a single parent context can be used by
an entire web application, while each servlet has its own child context that is
independent of that of any other servlet.

Here is the code of our applicationContext-hibernate.xml file:

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<!--
- Application context definition for RoseIndia Login Application on Hibernate.
-->
<beans>

<!-- ========================= RESOURCE DEFINITIONS


========================= -->

<!-- Configurer that replaces ${...} placeholders with values from a properties file -->
<!-- (in this case, JDBC-related settings for the dataSource definition below) -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>/WEB-INF/jdbc.properties</value></property>
</bean>

<!-- Local DataSource that works in any environment -->


<!-- Note that DriverManagerDataSource does not pool; it is not intended for production --
>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property
name="driverClassName"><value>${jdbc.driverClassName}</value></property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>

</bean>

<!-- JNDI DataSource for J2EE environments -->

<!-- Hibernate SessionFactory -->


<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="mappingResources">
<list>
<value>roseindia/dao/hibernate/Login.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>

<!-- ========================= BUSINESS OBJECT DEFINITIONS


========================= -->

<!--
Data access object: Hibernate implementation.
-->
<bean id="HibernateSpringDaoTarget" class="roseindia.dao.SpringHibernateDAOImpl">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>

<!--
- Transactional proxy for Login Application central data access object.
-
- Defines specific transaction attributes with "readOnly" markers,
- which is an optimization that is particularly valuable with Hibernate
- (to suppress unnecessary flush attempts for read-only operations).
-
- Note that in a real-life app with multiple transaction proxies,
- you will probably want to use parent and child bean definitions
- as described in the manual, to reduce duplication.
-->

<bean id="SpringHibernateDao"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
>
<property name="transactionManager"><ref local="transactionManager"/></property>
<property name="target"><ref local="HibernateSpringDaoTarget"/></property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="store*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

<!-- Email Service-->


<bean id="mailbean" class="roseindia.web.common.SendMail">
<property name="strSmtp"><value>192.168.10.14</value></property>
</bean>

</beans>

Understanding the applicationContext-hibernate.xml configuration file

Data source configuration:

The following line of the configuration file configures the data source for the application.

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property
name="driverClassName"><value>${jdbc.driverClassName}</value></property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>

The values for ${jdbc.driverClassName}, ${jdbc.url}, ${jdbc.username} and


${jdbc.password} are defined in the jdbc.properties file which is present in the /WEB-
INF/ directory of the web application.

Hibernate Session Factory:

Following line of the configuration file configures the Hibernate Session Factory.

<!-- Hibernate SessionFactory -->


<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="mappingResources">
<list>
<value>roseindia/dao/hibernate/Login.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

Configuring the Transaction Proxy for the DAO

Following line of the configures the Transaction Proxy for the DAO bean.

<bean id="SpringHibernateDao"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean
">
<property name="transactionManager"><ref local="transactionManager"/></property>
<property name="target"><ref local="HibernateSpringDaoTarget"/></property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="store*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

Mailer Bean

In our application we are sending the emails when new account is created or user asks for
the forgotten password. Following code configures the mailer bean.

<bean id="mailbean" class="roseindia.web.common.SendMail">


<property name="strSmtp"><value>192.168.10.14</value></property>
</bean>

In this section we have developed the configuration file that is used for configuring the
Spring Context. In the next section we will learn how to add the required configuration in
the web.xml file to load the Spring context though Spring's context loader servlet.

In this section we will learn about Spring's Context loader servlet and then see how to
add the required configuration in the web.xml file to load the Spring Context.

Spring ContextLoaderServlet
(org.springframework.web.context.ContextLoaderServlet)

Context Loader Servlet start up Spring's root WebApplicationContext when web


application is loaded.

This class has been deprecated for containers implementing Servlet API 2.4 or higher, in
favor of ContextLoaderListener. According to Servlet 2.4, listeners must be initialized
before load-on-startup servlets. Many Servlet 2.3 containers already enforce this
behavior. If you use such a container, this servlet can be replaced with
ContextLoaderListener. Else or if working with a Servlet 2.2 container, stick with this
servlet.

Servlet 2.3 containers known to work with bootstrap listeners are:

• Apache Tomcat 4.x+


• Jetty 4.x+
• Resin 2.1.8+
• Orion 2.0.2+
• BEA WebLogic 8.1 SP3

For working with any of them, ContextLoaderListener is recommended.

Servlet 2.3 containers known not to work with bootstrap listeners are:
• BEA WebLogic up to 8.1 SP2
• IBM WebSphere 5.x
• Oracle OC4J 9.0.3

Modification in the web.xml file

Following code needs to be added to the web.xml file to register the


ContextLoaderServlet.

<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

Following code specifies the the path of applicationContext-hibernate.xml file, where the
contextConfigLocation parameter has been defined..
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-
hibernate.xml</param-value>
</context-param>

Here is the full content of our web.xml file:


<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>
<display-name>Struts Blank Application</display-name>

<!-- Spring context Configuration Begins-->


<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-hibernate.xml</param-value>
</context-param>

<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Spring context loading ends-->

<!-- Standard Action Servlet Configuration (with debugging) -->


<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

<!-- Standard Action Servlet Mapping -->


<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<!-- The Usual Welcome File List -->


<welcome-file-list>
<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<!-- Struts Tag Library Descriptors -->


<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-logic</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-nested</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/tags/struts-tiles</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>

</web-app>

If you don't add the above code the Spring context won't initialized and you will get the
following error:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling
this request.

exception

java.lang.IllegalStateException: No WebApplicationContext found: no


ContextLoaderListener registered?
org.springframework.web.context.support.WebApplicationContext
Utils.getRequiredWebApplicationContext(WebApplicationContextUti
ls.java:84)
roseindia.tutorial.services.ServiceFinder.getContext(Unknown
Source)
org.apache.jsp.test_jsp._jspService(org.apache.jsp.test_jsp:51)

org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.j
ava:322)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache
Tomcat/5.5.12 logs.

Anda mungkin juga menyukai