We'll now add the Spring framework to our JDBC project. We'll add dependency injection to our Main and DAO class. We'll also learn how to configure DataSource as a Spring bean and supply connection parameters to it in the XML file.
************************************************************
org.springframework.stereotype.Component
Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.
Other class-level annotations may be considered as identifying a component as well, typically a special kind of component: e.g. the
package com.venkat;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.venkat.JdbcDao.JdbcDaoImpl;
import com.venkat.model.Login;
public class JDBCDemo {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcDaoImpl dao=context.getBean("jdbcDaoImpl",JdbcDaoImpl.class);
Login login=dao.getLogin("venkat");
System.out.println(login.getPassword());
}
}
************************************************************
org.springframework.stereotype.Component
Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.
Other class-level annotations may be considered as identifying a component as well, typically a special kind of component: e.g. the
@Repository
annotation or AspectJ's @Aspect
annotation.
org.springframework.beans.factory.annotation.Autowired
Marks a constructor, field, setter method or config method as to be autowired by Spring's dependency injection facilities.
Only one constructor (at max) of any given bean class may carry this annotation, indicating the constructor to autowire when used as a Spring bean. Such a constructor does not have to be public.
Fields are injected right after construction of a bean, before any config methods are invoked. Such a config field does not have to be public.
Config methods may have an arbitrary name and any number of arguments; each of those arguments will be autowired with a matching bean in the Spring container. Bean property setter methods are effectively just a special case of such a general config method. Such config methods do not have to be public.
In the case of multiple argument methods, the 'required' parameter is applicable for all arguments.
In case of a
Collection
or Map
dependency type, the container will autowire all beans matching the declared
value type. In case of a Map, the keys must be declared as type String and will
be resolved to the corresponding bean names.
Note that actual injection is performed through a
BeanPostProcessor
which in turn means that you cannot use @Autowired
to
inject references into BeanPostProcessor
or BeanFactoryPostProcessor
types. Please consult the javadoc for
the AutowiredAnnotationBeanPostProcessor
class (which, by default, checks for the presence of this annotation). package com.venkat;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.venkat.JdbcDao.JdbcDaoImpl;
import com.venkat.model.Login;
public class JDBCDemo {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcDaoImpl dao=context.getBean("jdbcDaoImpl",JdbcDaoImpl.class);
Login login=dao.getLogin("venkat");
System.out.println(login.getPassword());
}
}
**************************************************************************
package com.venkat.JdbcDao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.venkat.model.Login;
@Component
public class JdbcDaoImpl {
@Autowired
private DataSource datasource;
public DataSource getDatasource() {
return datasource;
}
public void setDatasource(DataSource datasource) {
this.datasource = datasource;
}
public Login getLogin(String userid) throws Exception
{
Connection con=datasource.getConnection();
PreparedStatement ps=con.prepareStatement("Select * from LOGIN where userid=?");
ps.setString(1, userid);
Login l=null;
ResultSet rs=ps.executeQuery();
if(rs.next())
{
l=new Login(userid, rs.getString("password"));
}
rs.close();
ps.close();
return l;
}
}
********************************************************************
package com.venkat.model;
public class Login {
private String userid;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String password;
public Login(String userid, String password) {
super();
this.userid = userid;
this.password = password;
}
}
*****************************************************************************
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.venkat"></context:component-scan>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver">
</property>
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:xe">
</property>
<property name="username" value="venkat"></property>
<property name="password" value="venkat"></property>
</bean></beans>
No comments:
Post a Comment