Spring Core
What is Spring framework?
Spring is a Dependency Injection framework to make java application loosely coupled.
Spring Modules:
Important Modules to learn: Core, beans, Context, JDBC, ORM, web
Sample basic maven configuration file
[Link]
<project xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>springcore</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springcore</name>
<url>[Link]
<properties>
<[Link]>UTF-8</[Link]>
</properties>
<dependencies>
<!-- [Link]
core -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-core</artifactId>
<version>[Link]</version>
</dependency>
<!-- [Link]
context -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-context</artifactId>
<version>[Link]</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
POJO class
[Link]
package [Link];
public class Student {
private int studentId;
private String studentName;
private String studentAddress;
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
[Link] = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
[Link] = studentName;
}
public String getStudentAddress() {
return studentAddress;
}
public void setStudentAddress(String studentAddress) {
[Link] = studentAddress;
}
public Student(int studentId, String studentName, String
studentAddress) {
super();
[Link] = studentId;
[Link] = studentName;
[Link] = studentAddress;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Student [studentId=" + studentId + ", studentName=" +
studentName + ", studentAddress=" + studentAddress
+ "]";
}
}
XML Configuration file
[Link]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="[Link]
xmlns:xsi="[Link]
xmlns:context="[Link]
xmlns:p="[Link]
xsi:schemaLocation="[Link]
[Link]
[Link]
[Link]
<bean class="[Link]" name="student1">
<!-- collaborators and configuration for this bean go here -->
<property name="studentId">
<value>1001</value>
</property>
<property name="studentName">
<value>Sudhanshu</value>
</property>
<property name="studentAddress">
<value>Gurgaon</value>
</property>
</bean>
<!-- more bean definitions go here -->
</beans>
Main file
[Link]
package [Link];
import [Link];
import [Link];
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
[Link]( "Hello Spring World!" );
ApplicationContext context = new ClassPathXmlApplicationContext("[Link]");
Student student1 = (Student)[Link]("student1");
[Link](student1);
}
}