0% found this document useful (0 votes)
1 views18 pages

Practical 39 45

The document outlines various practical exercises in Spring framework, including creating a basic Spring application, injecting values using constructor and setter injection, and implementing Spring annotations. It also covers creating a REST service with Spring Boot, demonstrating how to define beans in XML and use them in Java classes. Each practical includes code examples and expected outputs.

Uploaded by

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

Practical 39 45

The document outlines various practical exercises in Spring framework, including creating a basic Spring application, injecting values using constructor and setter injection, and implementing Spring annotations. It also covers creating a REST service with Spring Boot, demonstrating how to define beans in XML and use them in Java classes. Each practical includes code examples and expected outputs.

Uploaded by

samii000786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

2201921520198 YASH SRIVASTAVA B.

Tech CSE – AI

Practical No. – 39
Create the First Spring Application using eclipse/other framework and print the
value from XML.
Solution :
student.java
package com.yash;
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);
}
}

applicationContext.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: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.yash.Student">
<property name="name" value="Yash Srivastava"></property>
</bean>
</beans>
2201921520198 YASH SRIVASTAVA B.Tech CSE – AI

Test.java
package com.yash;
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();
}
}
Output
2201921520198 YASH SRIVASTAVA B.Tech CSE – AI

Practical No. – 40
Write the program in spring to inject primitive and string-based values using
Constructor Injection.
Solution :
1) Injecting Primitive Based Injection
Employee.java
package com.yash;
public class Employee {
private int id;
private String name;
public Employee() {
System.out.println("def cons");
}
public Employee(int id) {
this.id = id;
}
public Employee(String name) {
this.name = name;
}
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
void show(){
System.out.println(id+" "+name);
}
}
applicationContext.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"
2201921520198 YASH SRIVASTAVA B.Tech CSE – AI

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="e" class="com.yash.Employee">


<constructor-arg value="10" type="int"></constructor-arg>
</bean>
</beans>
Test.java
package com.javatpoint;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.*;
public class Test {
public static void main(String[] args) {
Resource r=new ClassPathResource("applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(r);
Employee s=(Employee)factory.getBean("e");
s.show();
}
}
Output

2) Injecting string-based values


Employee.java
package com.yash;
public class Employee {
private int id;
private String name;
public Employee() {
System.out.println("def cons");
2201921520198 YASH SRIVASTAVA B.Tech CSE – AI

}
public Employee(int id) {
this.id = id;
}
public Employee(String name) {
this.name = name;
}
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
void show(){
System.out.println(id+" "+name);
}
}
applicationContext.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: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="e" class="com.yash.Employee">


<constructor-arg value="10" type="int" ></constructor-arg>
<constructor-arg value="Yash Srivastava"></constructor-arg>
</bean>
</beans>
Test.java
package com.javatpoint;
import org.springframework.beans.factory.BeanFactory;
2201921520198 YASH SRIVASTAVA B.Tech CSE – AI

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.*;
public class Test {
public static void main(String[] args) {
Resource r=new ClassPathResource("applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(r);
Employee s=(Employee)factory.getBean("e");
s.show();
}
}
Output
2201921520198 YASH SRIVASTAVA B.Tech CSE – AI

Practical No. – 41
Write the program in spring to inject primitive and string-based values using
Setter Injection.
Solution :
Employee.java
package com.yash;
public class Employee {
private int id;
private String name;
private String city;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
void display(){
System.out.println(id+" "+name+" "+city);
}
2201921520198 YASH SRIVASTAVA B.Tech CSE – AI

applicationContext.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: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="obj" class="com.yash.Employee">
<property name="id">
<value>20</value>
</property>
<property name="name">
<value>Yash Srivastava</value>
</property>
<property name="city">
<value>Varanasi</value>
</property>
</bean>
</beans>
Test.java
package com.yash;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.*;
public class Test {
public static void main(String[] args) {
Resource r=new ClassPathResource("applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(r);
Employee e=(Employee)factory.getBean("obj");
s.display();
2201921520198 YASH SRIVASTAVA B.Tech CSE – AI

}
}
Output
2201921520198 YASH SRIVASTAVA B.Tech CSE – AI

Practical No. – 42
Write the program in spring to inject primitive and string-based values using
Setter Injection.
Solution :
HelloWorld.java
package com.yash;
public class HelloWorld {
public void sayHello() {
System.out.println("Spring World");
}
}
applicationContext.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.xsd">

<!-- Define the HelloWorld bean -->


<bean id="helloWorld" class="com.yash.HelloWorld"/>
</beans>

MainApplication.java
package com.yash;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApplication {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// Retrieve the HelloWorld bean from the context
2201921520198 YASH SRIVASTAVA B.Tech CSE – AI

HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");

// Call the sayHello method on the bean


helloWorld.sayHello();
}
}
Output
2201921520198 YASH SRIVASTAVA B.Tech CSE – AI

Practical No. – 43
Write a program to display Spring annotations.
Solution :
HelloWorld.java
package com.yash;
import org.springframework.stereotype.Component;
@Component
public class HelloWorld {
public void sayHello() {
System.out.println("Implementing : Spring Annotations Example");
}
}
MessagePrinter.java
package com.yash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MessagePrinter {
private final HelloWorld helloWorld;
@Autowired
public MessagePrinter(HelloWorld helloWorld) {
this.helloWorld = helloWorld;
}
public void printMessage() {
helloWorld.sayHello();
}
}
applicationContext.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"
2201921520198 YASH SRIVASTAVA B.Tech CSE – AI

xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<!-- Enable component scanning -->


<context:component-scan base-package="com.yash"/>
</beans>
MainApplication.java
package com.yash;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApplication {
public static void main(String[] args) {
// Create the application context using the applicationContext.xml file
ApplicationContext context = new
ClassPathXmlApplicationContext("applicationContext.xml");
// Retrieve the MessagePrinter bean from the context
MessagePrinter messagePrinter = context.getBean(MessagePrinter.class);
// Call the printMessage method on the bean
messagePrinter.printMessage();
}
}
Output
2201921520198 YASH SRIVASTAVA B.Tech CSE – AI

Practical No. – 44
Write the program for Spring Boot Example.
Solution :
student.java
package com.yash;
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("Sprint boot example by : "+name);
}
}

applicationContext.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: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.yash.Student">
<property name="name" value="Yash Srivastava"></property>
</bean>
</beans>
2201921520198 YASH SRIVASTAVA B.Tech CSE – AI

Test.java
package com.yash;
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();
}
}
Output
2201921520198 YASH SRIVASTAVA B.Tech CSE – AI

Practical No. – 45
Creating REST Service with Spring Boot.
Solution :
Book.java
public class Book {
private Long id;
private String title;
private String author;
// Getters and setters (omitted for brevity)
}
BookController.java
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping("/{id}")
public Book getBookById(@PathVariable Long id) {
// Logic to fetch book by id from a database or service
Book book = new Book();
book.setId(id);
book.setTitle("Sample Book Title");
book.setAuthor("Sample Author");
return book;
}
@PostMapping("/")
public Book addBook(@RequestBody Book book) {
// Logic to save the book to the database or service
// Typically you would save the book and return the saved instance
return book;
}
2201921520198 YASH SRIVASTAVA B.Tech CSE – AI

GET Request : http://localhost:8080/books/1


Response :

POST Request : http://localhost:8080/books/


Request Body :

Response :
2201921520198 YASH SRIVASTAVA B.Tech CSE – AI

You might also like