Practical 39 45
Practical 39 45
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">
}
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">
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
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
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">
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
Response :
2201921520198 YASH SRIVASTAVA B.Tech CSE – AI