Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
30 views14 pages

Week3 Spring Core and Maven

Uploaded by

lalithilango51
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)
30 views14 pages

Week3 Spring Core and Maven

Uploaded by

lalithilango51
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/ 14

Digital Nurture 4.

0 – Week 2

Spring Core and Maven

Exercise 1: Configuring a Basic Spring Application


Scenario:
Your company is developing a web application for managing a library. You need
to use the Spring Framework to handle the backend operations.

Objective:
To configure a basic Spring Core application using Maven in IntelliJ IDEA for a
Library Management system, which separates concerns using BookService and
BookRepository components, and manages them via XML-based Dependency
Injection.

Steps:
o Open IntelliJ IDEA → File → New → Project.
o Select Maven (not Spring Initializr).
o Project name: LibraryManagement.
o Set groupId: com.library, artifactId: LibraryManagement.
o Finish. IntelliJ will generate the basic Maven structure

Add Spring Core Dependency:


pom.xml.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.30</version>
</dependency>
Create Folder Structure:
src/main/java, create:
 com.library.service → for BookService.java
 com.library.repository → for BookRepository.java
src/main/resources, create:
 applicationContext.xml

Create BookRepository Class:


package com.library.repository;

public class BookRepository {


public void printBookInfo() {
System.out.println("BookRepository: Fetching book data...");
}
}

Create BookService Class:


package com.library.service;

import com.library.repository.BookRepository;

public class BookService {


private BookRepository bookRepository;

public void setBookRepository(BookRepository bookRepository) {


this.bookRepository = bookRepository;
}
public void displayBook() {
System.out.println("BookService: Displaying book");
bookRepository.printBookInfo();
}
}
Configure Spring Beans in XML:
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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bookRepository"
class="com.library.repository.BookRepository" />
<bean id="bookService" class="com.library.service.BookService">
<property name="bookRepository" ref="bookRepository" />
</bean>
</beans>

MainApp.Java
package com.library;
import com.library.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {


public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("applicationContext.xml");
BookService bookService = context.getBean("bookService",
BookService.class);
bookService.displayBook();
}
}

Output:

Exercise 2: Implementing Dependency Injection


Objective:
To implement Dependency Injection (DI) in a Library Management System
using Spring Framework, where the BookService class depends on the
BookRepository class.Spring's Inversion of Control (IoC) container will
manage the creation and wiring of these objects through XML-based
configuration.

Steps:
Step 1: Create BookRepository Class:
package com.library.repository;

public class BookRepository {


public void updateBookStatus(String bookId, String status) {
System.out.println("Book " + bookId + " status updated to: " + status);
}
}

Step 2: Create BookService Class:


package com.library.service;

import com.library.repository.BookRepository;
public class BookService {
private BookRepository bookRepository;
public void setBookRepository(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
public void issueBook(String bookId) {
System.out.println("Issuing Book: " + bookId);
bookRepository.updateBookStatus(bookId, "issued");
}
}

Step 3: Create applicationContext.xml:


<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">
<bean id="bookRepository"
class="com.library.repository.BookRepository" />
<bean id="bookService" class="com.library.service.BookService">
<property name="bookRepository" ref="bookRepository" />
</bean>
</beans>

Step 4: Create LibraryManagementApplication.java:


package com.library;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.library.service.BookService;
public class LibraryManagementApplication {
public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("applicationContext.xml");
BookService bookService = context.getBean("bookService",
BookService.class);
bookService.issueBook("B101");
}
}

Output:
Exercise 4: Creating and Configuring a Maven Project:
Objective
To create a Maven-based Java project named LibraryManagement, add Spring
framework dependencies, and configure Maven to compile with Java 1.8 using
the Maven Compiler Plugin.

Step 1: Create a New Maven Project:


o Open IntelliJ IDEA → File → New → Project
o Choose Maven (from the left panel).
o Uncheck “Create from archetype” (keep it simple).
o Click Next.
o Enter:
o GroupId: com.library
o ArtifactId: LibraryManagement
o Version: Leave default or use 1.0-SNAPSHOT
o Click Finish.

Step 2: Add Spring Dependencies in pom.xml:


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.30</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.30</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.30</version>
</dependency>

Step4: MainApp.java:
package com.library;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("Spring context loaded successfully!This is Nice!");
context.close();
}
}
Output:
Additional Hands-on

Exercise 5: Configuring the Spring IoC Container


Objective:
To configure and test the Spring IoC (Inversion of Control) Container using
XML. This helps automatically manage object creation and dependency
injection in your application.

Steps:
Step 1: Create Spring Configuration File
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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bookRepository"
class="com.library.repository.BookRepository"/>
<bean id="bookService" class="com.library.service.BookService">
<property name="bookRepository" ref="bookRepository"/>
</bean>
</beans>

Step2: BookRepository.java
package com.library.repository;
public class BookRepository {
public void fetchBookData() {
System.out.println(“!!!BookRepository: Fetching book data from the
database...");
}}
Step 3: BookService.java
package com.library.service;

import com.library.repository.BookRepository;
public class BookService {
private BookRepository bookRepository;
public void setBookRepository(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
public void displayBook() {
System.out.println("!!!BookService: Displaying book information...");
bookRepository.fetchBookData();
}
}

Step 4: MainApp.java
package com.library;

import com.library.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {


public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("applicationContext.xml");
BookService bookService = context.getBean("bookService",
BookService.class);
bookService.displayBook();
((ClassPathXmlApplicationContext) context).close();
}
}

Output:

Exercise 7: Implementing Constructor and Setter Injection


Objective
To configure constructor and setter injection for Spring beans using
applicationContext.xml, and test the setup through a main class.
Steps:
Step1: BookRepository.java
package com.library.repository;
public class BookRepository {
public void fetchBookData() {
System.out.println("!!!BookRepository: Fetching book data from the
database...");
}
}
Step2: BookService.java
package com.library.service;

import com.library.repository.BookRepository;
public class BookService {
private String message;
private BookRepository bookRepository;
public BookService(String message) {
this.message = message;
System.out.println("!!! Constructor called: " + message);
}
public void setBookRepository(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}
public void displayBook() {
System.out.println("!!! BookService: " + message);
bookRepository.fetchBookData();
}
}
Step3: 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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bookRepository"
class="com.library.repository.BookRepository" />
<bean id="bookService" class="com.library.service.BookService">
<constructor-arg value="Library Service Initialized via Constructor
Injection" />
<property name="bookRepository" ref="bookRepository" />
</bean>
</beans>
Step4: LibraryManagementApplication.java
package com.library;

import com.library.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class LibraryManagementApplication {
public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("applicationContext.xml");
BookService bookService = context.getBean("bookService",
BookService.class);
bookService.displayBook();
((ClassPathXmlApplicationContext) context).close();
}
}
Output:

You might also like