0% found this document useful (0 votes)
77 views

Spring Core Notes

The document discusses the Spring framework and its core modules. Spring is a dependency injection framework that makes Java applications loosely coupled. The important core modules to learn are Spring Core, Spring Beans, Spring Context, Spring JDBC, Spring ORM, and Spring Web. It provides sample code of a basic Maven configuration file, a POJO class, an XML configuration file, and a main file to demonstrate dependency injection using Spring.

Uploaded by

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

Spring Core Notes

The document discusses the Spring framework and its core modules. Spring is a dependency injection framework that makes Java applications loosely coupled. The important core modules to learn are Spring Core, Spring Beans, Spring Context, Spring JDBC, Spring ORM, and Spring Web. It provides sample code of a basic Maven configuration file, a POJO class, an XML configuration file, and a main file to demonstrate dependency injection using Spring.

Uploaded by

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

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

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.springcore</groupId>
<artifactId>springcore</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>springcore</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-
core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-
context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
POJO class
Student.java
package com.springcore;

public class Student {

private int studentId;


private String studentName;
private String studentAddress;

public int getStudentId() {


return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentAddress() {
return studentAddress;
}
public void setStudentAddress(String studentAddress) {
this.studentAddress = studentAddress;
}

public Student(int studentId, String studentName, String


studentAddress) {
super();
this.studentId = studentId;
this.studentName = studentName;
this.studentAddress = studentAddress;
}

public Student() {
super();
// TODO Auto-generated constructor stub
}

@Override
public String toString() {
return "Student [studentId=" + studentId + ", studentName=" +
studentName + ", studentAddress=" + studentAddress
+ "]";
}

}
XML Configuration file

Config.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<bean class="com.springcore.Student" 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

App.java

package com.springcore;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello Spring World!" );

ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");

Student student1 = (Student)context.getBean("student1");


System.out.println(student1);

}
}

You might also like