0% found this document useful (0 votes)
4 views15 pages

Spring Dependency Injection Examples

The document provides a detailed overview of a Spring application with various configurations, including XML, Java, and annotation-based setups. It includes code examples for defining beans, dependency injection methods, and running the application with outputs. Key components include the 'student' class, configuration files, and main application classes demonstrating different approaches to Spring configuration.

Uploaded by

sithir0604
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views15 pages

Spring Dependency Injection Examples

The document provides a detailed overview of a Spring application with various configurations, including XML, Java, and annotation-based setups. It includes code examples for defining beans, dependency injection methods, and running the application with outputs. Key components include the 'student' class, configuration files, and main application classes demonstrating different approaches to Spring configuration.

Uploaded by

sithir0604
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Pom.

xml :-
<project xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
<modelVersion>4.0.0</modelVersion>

<groupId>[Link]</groupId>
<artifactId>spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring</name>
<url>[Link]

<properties>
<[Link]>UTF-8</[Link]>
</properties>

<dependencies>
<!-- [Link] -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-core</artifactId>
<version>6.0.6</version>
</dependency>
<!-- [Link] -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.6</version>
</dependency>
<!-- [Link] -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-jdbc</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
1. Configuration using XML File
Package - [Link]

[Link] :-
package [Link];

import [Link];
import [Link];

public class App


{
public static void main( String[] args )
{
ApplicationContext context = new
ClassPathXmlApplicationContext("com/shrutika/spring/[Link]");

student student1= (student) [Link]("student1");


[Link](student1);
student student2= (student) [Link]("student2");
[Link](student2);

}
}
[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;
}
@Override
public String toString() {
return "student [studentId=" + studentId + ", studentName=" + studentName + ",
studentAddress=" + studentAddress
+ "]";
}
public student(int studentId, String studentName, String studentAddress) {
super();
[Link] = studentId;
[Link] = studentName;
[Link] = studentAddress;
}
public student() {
super();
// TODO Auto-generated constructor stub
}

[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" p:studentId="124"


p:studentName="abc" p:studentAddress="Gurugram" />

<bean class ="[Link]" name="student2">

<property name ="studentId" value="1234"/>

<property name ="studentName" value="kanchan"/>

<property name ="studentAddress" value="sirsa" />

</bean>

</beans>

Run [Link]
Output –

2. Configuration using Java File


Package – [Link].Java_config

[Link] :-
package [Link].Java_config;

public class Student {


private String name;
private int rollno;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public int getRollno() {
return rollno;
}
public void setRollno(int rollno) {
[Link] = rollno;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
[Link] = email;
}
public void display()
{
[Link]("Name : "+name);
[Link]("Rollno: "+rollno);
[Link]("Email: "+email);
}

[Link] :-
package [Link].Java_config;

import [Link];
import [Link];

@Configuration
public class SpringConfigFile {
@Bean
public Student stdId1()
{

Student std= new Student();


[Link]("kamal");
[Link](3);
[Link]("kamal@[Link]");
return std;
}

[Link] :-
package [Link].Java_config;

import [Link];
import [Link];

public class Main {

public static void main(String[] args)


{
ApplicationContext context = new
AnnotationConfigApplicationContext([Link]);
Student std=(Student) [Link]("stdId1");
[Link]();
}

Run [Link]
Output –
3. Configuration using Annotation
Package – [Link]

[Link] :-
package [Link];

import [Link];
import [Link];

@Component

public class Student {


@Value("Kkkk")
private String name;
@Value("105")
private int rollno;

@Value("94.6f")
private float marks;
public String getName() {
return name;
}

public void setName(String name) {


[Link] = name;
}
public int getRollno() {
return rollno;
}
public void setRollno(int rollno) {
[Link] = rollno;

}
public float getMarks() {
return marks;
}
public void setMarks(float marks) {

[Link] = marks;
}
public void display()
{
[Link]("Name:"+name);

[Link]("RollNo:"+rollno);
[Link]("Marks:"+marks);

Spring_config_file.java :-
package [Link];

import [Link];
import [Link];

@Configuration
@ComponentScan("[Link]")

public class Spring_config_file {

[Link] :-
package [Link];

import [Link];
import [Link];
import [Link];

public class Main {

public static void main(String[] args) {


// TODO Auto-generated method stub

ApplicationContext context = new


AnnotationConfigApplicationContext(Spring_config_file.class);
Student std=(Student) [Link]("student");
[Link]();
}
}

Run [Link]
Output –
Methods of Dependency Injection –
1. By using setter method
Package – [Link]

[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;
[Link]("Setting 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
+ "]";
}

[Link] :-
package [Link];

import [Link];
import [Link];

public class setter {

public static void main(String[] args) {


// TODO Auto-generated method stub

ApplicationContext context8 = new


ClassPathXmlApplicationContext("com/spring/setter/[Link]");

Student student1= (Student) [Link]("student1");


[Link](student1);

[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">

<property name="studentId">
<value> 12345</value>
</property>
<property name="studentName">
<value> Pinky</value></property>
<property name="studentAddress">
<value> Hansi</value></property>
</bean>
</beans>

Run [Link]
Output –

2. By using constructor method


Package – [Link]

[Link] :-
package [Link];

public class Person {


private String name;
private int personId;
public Person(String name, int personId) {
super();
[Link] = name;
[Link] = personId;
}
@Override
public String toString() {
return [Link]+":"+[Link];

}
}

[Link] :-
package [Link];

import [Link];
import [Link];

public class test {

public static void main(String[] args) {


// TODO Auto-generated method stub
ApplicationContext context1 = new
ClassPathXmlApplicationContext("com/spring/constructor/[Link]");
Person p= (Person) [Link]("person");
[Link](p);

}
}

[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 ="person">


<constructor-arg value="ram"/>
<constructor-arg value="12"/>

</bean>
</beans>

Run [Link]
Output –

You might also like