Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Creating Spring Application in Eclipse IDE

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Creating Spring Application in Eclipse IDE

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Creating spring application in Eclipse IDE

1. Creating spring application in Eclipse


2. Steps to create spring application in Eclipse

Here, we are going to create a simple application of spring framework


using eclipse IDE. Let's see the simple steps to create the spring
application in Eclipse IDE.

o create the java project


o add spring jar files
o create the class
o create the xml file to provide the values
o create the test class

Steps to create spring application in Eclipse IDE

Let's see the 5 steps to create the first spring application using eclipse
IDE.

1) Create the Java Project

Go to File menu - New - project - Java Project. Write the project


name e.g. firstspring - Finish. Now the java project is created.

2) Add spring jar files

There are mainly three jar files required to run this application.

o org.springframework.core-3.0.1.RELEASE-A
o com.springsource.org.apache.commons.logging-1.1.1
o org.springframework.beans-3.0.1.RELEASE-A

To load the jar files in eclipse IDE, Right click on your


project - Build Path - Add external archives - select all the
required jar files - finish.
3) Create Java class
In such case, we are simply creating the Student class have name
property. The name of the student will be provided by the xml file. It
is just a simple example not the actual use of spring. We will see the
actual use in Dependency Injection chapter. To create the java
class, Right click on src - New - class - Write the class name e.g.
Student - finish. Write the following code:

package com.pu;

public class Student {


private String name;

public String getName() {


return name;
}
public void setName(String name) {
this.name = name;
}
public void displayInfo(){
System.out.println("Hello: "+name);
}
}
4) Create the xml file

To create the xml file click on src - new - file - give the file name
such as applicationContext.xml - finish. Open the
applicationContext.xml file, and write the following code

<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/
schema/beans
http://www.springframework.org/schema/beans/spring-
beans-3.0.xsd">
<bean id="studentbean" class="com.javatpoint.Student">
<property name="name" value="Vimal Jaiswal"></property>
</bean>
</beans>

The bean element is used to define the bean for the given class.
The property subelement of bean specifies the property of the
Student class named name. The value specified in the property
element will be set in the Student class object by the IOC container.

5) Create the test class

Create the java class e.g. Test. Here we are getting the object of
Student class from the IOC container using the getBean() method of
BeanFactory. Let's see the code of test class.

package com.pu;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Test {


public static void main(String[] args) {
Resource resource=new ClassPathResource("applicationContext
.xml");
BeanFactory factory=new XmlBeanFactory(resource);

Student student=(Student)factory.getBean("studentbean");
student.displayInfo();
}
}
Spring JDBC Example
CREATE TABLE Student(ID INT,NAME VARCHAR(20),AGE
INT);

St
ep Description
s
Create a project with a name SpringExample and
1 create a package com.pu under the src folder in the
created project.
Add required Spring libraries using Add External
2 JARs option as explained in the Spring Hello
World Example chapter.
Add Spring JDBC specific latest libraries mysql-
connector-java.jar, org.springframework.jdbc.j
3 ar and org.springframework.transaction.jar in
the project. You can download required libraries if
you do not have them already.
Create DAO interface StudentDAO and list down
all the required methods. Though it is not required
4 and you can directly
write StudentJDBCTemplate class, but as a good
practice, let's do it.
Create other required Java
5 classes Student, StudentMapper, StudentJDBCTem
plate and MainApp under the com.pu package.
Make sure you already created Student table in
TEST database. Also make sure your MySQL
6 server is working fine and you have read/write
access on the database using the give username and
password.
Create Beans configuration file Beans.xml under
7
the src folder.
The final step is to create the content of all the Java
8 files and Bean Configuration file and run the
application

StudentDAO.java

package com.pu;

import java.util.List;
import javax.sql.DataSource;

public interface StudentDAO {


/**
* This is the method to be used to initialize
* database resources ie. connection.
*/
public void setDataSource(DataSource ds);
/**
* This is the method to be used to create
* a record in the Student table.
*/
public void create(String name, Integer age);
/**
* This is the method to be used to list down
* a record from the Student table corresponding
* to a passed student id.
*/
public Student getStudent(Integer id);
/**
* This is the method to be used to list down
* all the records from the Student table.
*/
public List<Student> listStudents();
/**
* This is the method to be used to delete
* a record from the Student table corresponding
* to a passed student id.
*/
public void delete(Integer id);
/**
* This is the method to be used to update
* a record into the Student table.
*/
public void update(Integer id, Integer age);
}

Student.java file

package com.pu;

public class Student {


private Integer age;
private String name;
private Integer id;

public void setAge(Integer age) {


this.age = age;
}
public Integer getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
}

StudentMapper.java file

package com.pu;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;

public class StudentMapper implements RowMapper<Student> {


public Student mapRow(ResultSet rs, int rowNum) throws
SQLException {
Student student = new Student();
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
return student;
}
}

StudentJDBCTemplate.java

package com.pu;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;

public class StudentJDBCTemplate implements StudentDAO {


private DataSource dataSource;
private JdbcTemplate jdbcTemplateObject;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.jdbcTemplateObject = new JdbcTemplate(dataSource);
}
public void create(String name, Integer age) {
String SQL = "insert into Student (name, age) values (?, ?)";
jdbcTemplateObject.update( SQL, name, age);
System.out.println("Created Record Name = " + name + " Age = "
+ age);
return;
}
public Student getStudent(Integer id) {
String SQL = "select * from Student where id = ?";
Student student = jdbcTemplateObject.queryForObject(SQL,
new Object[]{id}, new StudentMapper());
return student;
}
public List<Student> listStudents() {
String SQL = "select * from Student";
List <Student> students = jdbcTemplateObject.query(SQL, new
StudentMapper());
return students;
}
public void delete(Integer id) {
String SQL = "delete from Student where id = ?";
jdbcTemplateObject.update(SQL, id);
System.out.println("Deleted Record with ID = " + id );
return;
}
public void update(Integer id, Integer age){
String SQL = "update Student set age = ? where id = ?";
jdbcTemplateObject.update(SQL, age, id);
System.out.println("Updated Record with ID = " + id );
return;
}
}

MainApp.java file

package com.pu;

import java.util.List;

import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationConte
xt;
import com.tutorialspoint.StudentJDBCTemplate;

public class MainApp {


public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("Beans.xml");

StudentJDBCTemplate studentJDBCTemplate =
(StudentJDBCTemplate)context.getBean("studentJDBCTemplate");
System.out.println("------Records Creation--------");
studentJDBCTemplate.create("Zara", 11);
studentJDBCTemplate.create("Nuha", 2);
studentJDBCTemplate.create("Ayan", 15);
System.out.println("--Listing Multiple Records---");
List<Student> students = studentJDBCTemplate.listStudents();
for (Student record : students) {
System.out.print("ID : " + record.getId() );
System.out.print(", Name : " + record.getName() );
System.out.println(", Age : " + record.getAge());
}
System.out.println("--Updating Record with ID = 2 -" );
studentJDBCTemplate.update(2, 20);
System.out.println("--Listing Record with ID = 2 --" );
Student student = studentJDBCTemplate.getStudent(2);
System.out.print("ID : " + student.getId() );
System.out.print(", Name : " + student.getName() );
System.out.println(", Age : " + student.getAge());
}
}

configuration file Beans.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-
3.0.xsd ">

<!-- Initialization for data source -->


<bean id="dataSource"
class =
"org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name = "driverClassName" value =
"com.mysql.jdbc.Driver"/>
<property name = "url" value =
"jdbc:mysql://localhost:3306/TEST"/>
<property name = "username" value = "root"/>
<property name = "password" value = "password"/>
</bean>

<!-- Definition for studentJDBCTemplate bean -->


<bean id = "studentJDBCTemplate"
class = "com.pu.StudentJDBCTemplate">
<property name = "dataSource" ref = "dataSource" />
</bean>
</beans>

You might also like