Spring Framework Configuration

Steps to Configure Spring Framework

Initially, we configure all spring related setting in web.xml because every request will come to web.xml

[xml]

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Spring3</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.abc</url-pattern>
</servlet-mapping>

</web-app>

[/xml]

Name : dispatcher

Server will find the name start with “dispatcher” and ends with “servlet” for

dispacher-servlet.xml contain all sprint related configureation [packages required for application like beans, hibernatetemplate]

[xml]

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:security="http://www.springframework.org/schema/security" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd">

<context:component-scan base-package="com.fareed"></context:component-scan>

<bean id="basicdatasource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" ></property>
<property name="url" value="jdbc:mysql://localhost:3306/springs" ></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>

<mvc:annotation-driven></mvc:annotation-driven>

<bean id="sf" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

<property name="dataSource" ref="basicdatasource"></property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

</props>

</property>

<property name="packagesToScan" value="com.fareed.pojo">

</property>

</bean>

<bean id="hibernatetempalte" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sf"></property>

</bean>

<bean id="transactionManager" class=" org.springframework.orm.hibernate4.HibernateTransactionManager">

<property name="sessionFactory" ref="sf"></property>

</bean>

<tx:annotation-driven></tx:annotation-driven>

<bean id="" class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />

</bean>

</beans>

[/xml]

[java]

package com.fareed.controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.DispatcherServlet;

import com.fareed.pojo.DBConnection;
import com.fareed.service.TestService;

@Controller

public class TestController {

@Autowired
TestService ts;

@Autowired
HibernateTemplate ht;

@RequestMapping("/testfareed")
public void test(HttpServletResponse res) throws IOException
{

//System.out.println("my First TestController");

/*PrintWriter pw =res.getWriter();
pw.write("Namaste");*/

/*TestService ts = new TestService();*/
ts.service(res);
}

@RequestMapping("/dbtest")
public void dbtest() throws IOException
{
ht.get(DBConnection.class, 1);

}

}

[/java]

Add a Comment

Your email address will not be published. Required fields are marked *