Sunday, 27 July 2014

Spring Security Using MySql and JDBC

                                        Spring Security Using MySql and JDBC

Finally we need to configure Data Source in spring-context.xml file.

We need to configure Data Source using property driverClass, url, username and password.

We need to use spring's BasicDataSource for connecting to MySql Database.

Spring-Context.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
               http://www.springframework.org/schema/beans/spring-beans.xsd">

 <bean id="props" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" value="classpath:prop/jdbc.properties"/>
</bean>

<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="${jdbc.driverClassName}"/>
  <property name="url" value="${jdbc.url}"/>
  <property name="username" value="${jdbc.username}"/>
  <property name="password" value="${jdbc.password}"/>
</bean>     

</beans>

driverClass : Driver class is used to communicate to MySql database.
url :  Location of the database in MySql server.
username : username credentials to communicate with MySql  database.
password :  password credentials to communicate with MySql  database.

jdbc.properties

In  Properties file we need to configure the key value for driverClass, url, username and password.

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/login
jdbc.username=root
jdbc.password=root123

In spring config file the string ${jdbc.username} is replaced at runtime with the value "root". All the properties for placeholders  are replaced at the runtime based on the matched key in the properties file.




No comments:

Post a Comment

Spring 3 Rest Web Service Batch Update with JQuery & Ajax Integration

                      Spring 3  Rest Web Service Batch Update with JQuery & Ajax Integration Hi     In this blog I will describe the...