[go: up one dir, main page]

0% found this document useful (0 votes)
12 views9 pages

Unit 5 Oops With Java

The document outlines the Spring framework, emphasizing its lightweight nature and support for building Java applications through features like Inversion of Control (IoC) and Dependency Injection (DI). It details various types of DI, the concept of beans, and the management of their lifecycle and scopes within the application context. Additionally, it discusses Aspect-Oriented Programming (AOP) and its benefits in enhancing modularity and maintainability in Spring applications.

Uploaded by

mail.aashiagr
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)
12 views9 pages

Unit 5 Oops With Java

The document outlines the Spring framework, emphasizing its lightweight nature and support for building Java applications through features like Inversion of Control (IoC) and Dependency Injection (DI). It details various types of DI, the concept of beans, and the management of their lifecycle and scopes within the application context. Additionally, it discusses Aspect-Oriented Programming (AOP) and its benefits in enhancing modularity and maintainability in Spring applications.

Uploaded by

mail.aashiagr
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
You are on page 1/ 9

‭5.1 Spring is a lightweight framework‬ ‭5.

7 Dependency Injection (DI)‬


‭ ‬ I‭t is a comprehensive framework for building Java applications.‬
● ‭●‬ I‭t is a design pattern used to implement Inversion of Control (IoC) in‬
‭●‬ ‭It provides infrastructure support, reducing the need for boilerplate‬ ‭software development‬
‭code.‬ ‭●‬ ‭making it easier to manage dependencies between different‬
‭●‬ ‭It uses Inversion of Control (IoC) to manage object creation and‬ ‭components.‬
‭dependency injection.‬ ‭●‬ ‭This pattern enhances modularity, maintainability, and testability of‬
‭●‬ ‭It supports Aspect-Oriented Programming (AOP) for separating‬ ‭code by decoupling the creation of an object from its usage.‬
‭cross-cutting concerns.‬ ‭●‬ ‭Dependency‬‭: An object that another object relies on.‬
‭●‬ ‭It provides a powerful Model-View-Controller (MVC) framework for web‬ ‭●‬ ‭Injection‬‭: The process of passing the dependent object to a dependent‬
‭applications.‬ ‭object.‬
‭●‬ ‭It offers Spring Boot for easier setup and configuration of new Spring‬
‭applications.‬ ‭5.8 Types of Dependency Injection‬
‭5.2 Inversion of Control (IoC):‬ ‭●‬ ‭Constructor Injection:‬
‭●‬ ‭Dependencies are provided through a class constructor.‬
‭●‬ A ‭ design principle where the control of creating and managing objects‬
‭●‬ ‭Most common and recommended type.‬
‭is transferred from the application itself to a container or framework.‬
‭●‬ ‭Ensures that dependencies are not mutable and that the object is‬
‭●‬ ‭Example‬‭: Instead of a class instantiating its dependencies,‬‭the container‬
‭always in a consistent state.‬
‭provides them.‬
‭public‬‭class‬‭Car‬‭{‬
‭5.3 Container:‬ ‭private‬‭Engine‬‭engine‬‭;‬

‭●‬ T‭ he component in Spring that implements IoC. It creates, configures,‬ ‭public‬‭Car‬‭(‬‭Engine‬‭engine‬‭)‬‭{‬


‭and manages beans.‬ ‭this‬‭.‭e‬ ngine‬‭=‬‭engine‬‭;‬
‭}‬
‭●‬ ‭Example‬‭: Spring's ApplicationContext and BeanFactory are examples of‬
‭}‬
‭containers.‬ ‭●‬ ‭Setter Injection:‬
‭●‬ ‭Dependencies are provided through setter methods.‬
‭5.4 Dependency Injection (DI):‬ ‭●‬ ‭Allows the dependencies to be replaced or changed after the‬
‭●‬ A ‭ specific form of IoC where the container injects dependencies into an‬ ‭object is created.‬
‭object, rather than the object creating them itself.‬
‭public‬‭class‬‭Car‬‭{‬
‭●‬ ‭Types:‬ ‭private‬‭Engine‬‭engine‬‭;‬
‭●‬ ‭Constructor Injection:‬‭Dependencies are provided through‬‭a class‬
‭constructor.‬ ‭public‬‭void‬‭setEngine‬‭(‭E‬ ngine‬‭engine‬‭)‬‭{‬
‭this‬‭.‭e‬ ngine‬‭=‬‭engine‬‭;‬
‭●‬ ‭Setter Injection‬‭: Dependencies are provided through‬‭setter‬ ‭}‬
‭methods.‬ ‭}‬
‭●‬ ‭Field Injection:‬‭Dependencies are directly injected into fields.‬ ‭●‬ ‭Field Injection:‬
‭●‬ ‭Dependencies are provided directly into fields.‬
‭5.5 Beans:‬ ‭●‬ ‭This approach can be less flexible and harder to test.‬
‭●‬ ‭Only used for reference variable‬
‭●‬ T‭ he objects that the Spring IoC container manages. They form the‬
‭backbone of a Spring application.‬ ‭public‬‭class‬‭Car‬‭{‬
‭●‬ ‭Example‬‭: A service class, a repository class, or a‬‭configuration class can‬ ‭@‬‭Autowired‬
‭be beans.‬ ‭private‬‭Engine‬‭engine‬‭;‬
‭}‬

‭5.6 Correlation Between Spring ioc components‬


‭5.9 Benefits of Dependency Injection‬
T‭ he‬‭Spring Framework‬‭provides the‬‭IoC Container‬‭, which‬‭manages‬‭Beans‬‭.‬
‭●‬ ‭ ecoupling‬‭: Objects are decoupled from their dependencies,‬
D
‭The‬‭IoC Container‬‭uses‬‭Configuration Metadata‬‭to know‬‭how to create and‬
‭making the code more modular.‬
‭configure beans.‬
‭●‬ ‭Easier Testing:‬‭Dependencies can be mocked or stubbed out‬
‭Dependency Injection (DI)‬‭is used by the container‬‭to inject dependencies‬
‭easily, improving testability.‬
‭into beans.‬
‭●‬ ‭Flexibility‬‭: Dependencies can be changed without modifying the‬
‭Aspect-Oriented Programming (AOP)‬‭is applied to beans‬‭to handle‬
‭dependent class.‬
‭cross-cutting concerns, enhancing their functionality.‬
‭●‬ ‭Improved Code Quality‬‭: Promotes the use of interfaces and‬
‭abstractions, leading to cleaner and more maintainable code.‬

‭5.10 Aspect oriented programming (AOP)‬


‭●‬ D ‭ eclarative Transaction Management‬‭:‬‭Spring AOP allows developers to‬
‭manage transactions without tightly coupling them to business logic.‬
‭●‬ ‭Modularization of Crosscutting Concerns‬‭:‬‭AOP in Spring helps to‬
‭modularize concerns like logging, security, and caching, which impact‬
‭multiple parts of an application.‬
‭●‬ ‭Custom Aspect Implementation‬‭: Developers can create reusable‬
‭aspects in Spring AOP to encapsulate crosscutting concerns,‬
‭complementing traditional OOP.‬
‭●‬ ‭Enhancement of Spring IoC‬‭:‬‭While not mandatory, integrating AOP with‬
‭Spring IoC enhances its capabilities by combining dependency injection‬
‭with aspect-oriented programming.‬
‭●‬ I‭ mproved Modularity and Maintainability:‬‭AOP enriches the Spring‬ x‭ ml config‬‭:‬
‭framework by promoting a cleaner separation of concerns and‬ ‭<‭b
‬ ean id‬‭="‬‭singletonBean‬‭"‬‭class‬‭="‬‭com.example.SingletonBean‬‭"‬‭/>‬

‭facilitating easier maintenance of enterprise applications.‬ j‭ava config‬‭:‬


‭@‬‭Bean‬
‭5.11 Bean in Spring‬ ‭public‬‭SingletonBean‬‭singletonBean‬‭()‬‭{‬
‭return‬‭new‬‭SingletonBean‬‭();‬
‭●‬ I‭n the Spring Framework, a bean is an object that is instantiated,‬
‭}‬
‭assembled, and managed by the Spring IoC (Inversion of Control)‬
‭container.‬
‭ .‬ P
2 ‭ rototype Scope‬
‭●‬ ‭It represents a fundamental building block of a Spring application.‬
‭●‬ ‭Prototype scope creates a new instance of the bean whenever‬
‭requested. Each time you ask for a prototype bean, Spring creates‬
‭5.12 Key Characteristics of a Bean:‬ ‭a new instance.‬
‭●‬ B ‭ eans are defined in Spring configuration metadata, either XML-based‬ ‭●‬ ‭Useful for stateful beans where each client needs its own instance.‬
‭(applicationContext.xml) or Java-based (annotations or Java‬ ‭●‬ ‭Configuration‬‭:‬
x‭ ml config‬‭:‬
‭configuration classes).‬
‭<‭b
‬ ean id‬‭="‬‭prototypeBean‬‭"‬‭class‬‭="‬‭com.example.SingletonBean‬‭"‬
‭●‬ ‭Example in XML‬ ‭scope‬‭="‬‭prototype‬‭"‬‭/>‬

‭<‬‭bean id‬‭="‬‭userService‬‭"‬‭class‬‭="‬‭com.example.UserService‬‭"/>‬ j‭ava config‬‭:‬


‭@‬‭Bean‬
‭@‬‭Scope‬‭("‬‭prototype‬‭")‬
‭●‬ ‭Instantiation:‬ ‭public‬‭PrototypeBean‬‭prototypeBean‬‭()‬‭{‬
‭●‬ ‭The Spring IoC container creates beans based on their‬ ‭return‬‭new‬‭PrototypeBean‬‭();‬
‭}‬
‭configuration.‬
‭●‬ ‭Beans can be instantiated using default constructors or specific‬
‭ .‬ R
3 ‭ equest Scope‬
‭factory methods.‬
‭●‬ ‭Request scope creates a new instance of the bean for each HTTP‬
‭●‬ ‭Configuration:‬ ‭request. This scope is available only for web-aware Spring‬
‭●‬ ‭Beans can be configured with dependencies (other beans),‬ ‭ApplicationContext.‬
‭properties (simple values), initialization methods, destruction‬ ‭●‬ ‭Used for beans that need to be recreated for every HTTP request,‬
‭methods, etc.‬ ‭such as request-specific data.‬
‭●‬ ‭Example with dependencies in XML:‬ ‭●‬ ‭Configuration‬‭:‬
x‭ ml config‬‭:‬
‭<‬‭bean id‬‭="‬‭userService‬‭"‬‭class‬‭="‬‭com.example.UserService‬‭">‬ ‭<‭b
‬ ean id‬‭="‬‭requestBean‬‭"‬‭class‬‭="‬‭com.example.RequestBean‬‭"‬
‭<‭p
‬ roperty name‬‭="‬‭userDao‬‭"‬‭ref‬‭="‬‭userDao‬‭"/>‬ ‭scope‬‭="‬‭request‬‭"‬‭/>‬
‭</‬‭bean‬‭>‬
j‭ava config‬‭:‬
‭●‬ ‭Dependency Injection:‬ ‭@‬‭Bean‬
‭@‬‭Scope‬‭(‭v‬ alue‬‭=‬‭"‭r‬ equest‬‭",‬‭proxyMode‬‭=‬
‭●‬ ‭Spring injects dependencies into a bean's properties or‬ ‭ScopedProxyMode‬‭.‬‭TARGET_CLASS‬‭)‬
‭constructor at runtime.‬ ‭public‬‭RequestBean‬‭requestBean‬‭()‬‭{‬
‭●‬ ‭Example using constructor injection:‬ ‭return‬‭new‬‭RequestBean‬‭();‬
‭}‬

‭public‬‭class‬‭UserService‬‭{‬ ‭ .‬ S‭ ession Scope‬


4
‭private‬‭final‬‭UserDao‬‭userDao‬‭;‬
‭●‬ ‭Session scope creates a new bean instance for each HTTP session.‬
‭public‬‭UserService‬‭(‬‭UserDao‬‭userDao‬‭)‬‭{‬ ‭This scope is also specific to web-aware Spring ApplicationContext.‬
‭this‬‭.‭u
‬ serDao‬‭=‬‭userDao‬‭;‬ ‭●‬ ‭Suitable for beans that store session-specific information, like user‬
‭}‬
‭}‬
‭preferences or shopping cart details.‬
‭●‬ ‭Configuration‬‭:‬
‭●‬ ‭Lifecycle Management:‬ x‭ ml config‬‭:‬
‭●‬ ‭The Spring IoC container manages the lifecycle of beans, including‬ ‭<‭b
‬ ean id‬‭="‬‭sessionBean‬‭"‬‭class‬‭="‬‭com.example.SessionBean‬‭"‬
‭scope‬‭="‬‭session‬‭"‬‭/>‬
‭instantiation, initialization, usage, and optional destruction (for‬
‭singleton beans).‬ j‭ava config‬‭:‬
‭●‬ ‭Use‬‭@PostConstruct‬‭and‬‭@PreDestroy‬‭annotations or‬ ‭@‬‭Bean‬
‭@‬‭Scope‬‭(‭v‬ alue‬‭=‬‭"‭s‬ ession‬‭",‬‭proxyMode‬‭=‬
‭init-method and destroy-method attributes in XML for‬ ‭ScopedProxyMode‬‭.‬‭TARGET_CLASS‬‭)‬
‭initialization and destruction methods.‬ ‭public‬‭SessionBean‬‭sessionBean‬‭()‬‭{‬
‭return‬‭new‬‭SessionBean‬‭();‬
‭}‬
‭5.13 Scopes of Bean‬
‭ eans can have different scopes (singleton, prototype, request, session, etc.)‬
B ‭ .‬ A
5 ‭ pplication Scope‬
‭that define their lifecycle and visibility within the application context.‬ ‭●‬ ‭Application scope creates a single bean instance per‬
‭ServletContext (global to the web application).‬
‭ .‬
1 S‭ ingleton Scope‬ ‭●‬ ‭Useful for beans that need to be shared across the entire‬
‭●‬ ‭Singleton scope is the default scope in Spring. It means that Spring‬ ‭application, like a global cache or configuration settings.‬
‭container creates exactly one instance of the bean definition and‬ ‭●‬ ‭Configuration‬‭:‬
‭shares it throughout the application.‬ x‭ ml config‬‭:‬
‭●‬ ‭Suitable for stateless beans where a single instance can be shared‬ ‭<‭b
‬ ean id‬‭="‬‭applicationBean‬‭"‬‭class‬‭="‬‭com.example.ApplicationBean‬‭"‬
‭by multiple clients.‬ ‭scope‬‭="‬‭application‬‭"‬‭/>‬

‭●‬ ‭Configuration‬‭:‬
j‭ava config‬‭:‬ ‭ .‬ C
4 ‭ onstructor‬
‭@‬‭Bean‬ ‭●‬ ‭Autowires by matching the constructor parameters to beans in the‬
‭@‬‭Scope‬‭("‬‭application‬‭")‬
‭public‬‭ApplicationBean‬‭applicationBean‬‭()‬‭{‬ ‭container. Constructor injection is generally recommended for‬
‭return‬‭new‬‭ApplicationBean‬‭();‬ ‭mandatory dependencies.‬
‭}‬ ‭●‬ ‭Example‬‭:‬

‭ .‬
6 ‭ eb Socket Scope‬
W ‭ ML‬‭Configuration‬‭:‬
X
‭<‭b‬ ean id‬‭="‬‭a‭"‬ ‬‭class‬‭="‬‭com.example.A‬‭"‬‭autowire‬‭="‬‭constructor‬‭"/>‬
‭●‬ ‭WebSocket scope is used for beans that are associated with a‬
‭<‭b
‬ ean id‬‭="‬‭b‬‭"‬‭class‬‭="‬‭com.example.B‬‭"/>‬
‭WebSocket session. It's available only in a WebSocket-enabled‬
‭application.‬ J‭ ava‬‭Configuration‬‭:‬
‭●‬ ‭Suitable for WebSocket-specific beans that maintain state across‬ ‭@‬‭Autowired‬
‭public‬‭A‭(‬ ‬‭B‬‭b‬‭)‬‭{‬
‭WebSocket sessions.‬ ‭this‬‭.‭b
‬ ‬‭=‬‭b‭;‬ ‬
‭●‬ ‭Configuration‬‭:‬ ‭}‬
j‭ava config‬‭:‬
‭@‬‭Bean‬ ‭ .‬ A
5 ‭ utodetect (Deprecated)‬
‭@‬‭Scope‬‭("‬‭websocket‬‭")‬
‭public‬‭WebSocketBean‬‭webSocketBean‬‭()‬‭{‬ ‭●‬ ‭Attempts to autowire using constructor first, and if no constructor‬
‭return‬‭new‬‭WebSocketBean‬‭();‬ ‭is found, uses byType.‬
‭}‬ ‭●‬ ‭Example‬‭: This mode is deprecated and should be avoided‬‭in favor‬
‭of explicit autowiring modes.‬
f‭ or more details:‬ ‭5.15 Annotations in Spring‬
‭https://docs.spring.io/spring-framework/reference/core/beans/factory-scop‬ ‭●‬ ‭ nnotations in Spring provide metadata about the beans and their‬
A
‭es.html‬ ‭dependencies.‬
‭‬
● ‭They simplify configuration and reduce XML-based configuration.‬
‭5.14 Autowiring in Bean‬ ‭●‬ ‭Usage‬‭: Used with components like‬‭ @Component‬ @Autowired‬
‭,‬‭ ‭,‬
‭●‬ A ‭ utowiring is Spring's feature that allows automatic dependency‬ @Scope‬
‭ @RequestMapping‬
‭,‬‭ ‭, etc.‬
‭injection.‬ ‭●‬ ‭Example‬‭:‬
‭●‬ ‭It simplifies bean wiring by letting Spring resolve dependencies based‬
‭ ‬‭RestController‬
@
‭on type or name.‬
‭@‬‭RequestMapping‬‭("‬‭/api‬‭")‬
‭●‬ ‭We used the‬‭@Autowired‬‭annotations for the autowiring‬‭the bean‬ ‭public‬‭class‬‭MyController‬‭{‬
‭●‬ ‭Reduces boilerplate code and manual bean wiring.‬
‭ ‬‭Autowired‬
@
‭ ‬‭Autowired‬
@ ‭private‬‭MyService‬‭myService‬‭;‬
‭private‬‭Dependency‬‭dependency‬‭;‬ ‭}‬
‭●‬ ‭Types‬‭: ByType, ByName, Constructor, Autodetect, etc.‬

‭ .‬
1 ‭ o Autowiring (default)‬
N ‭5.16 Lifecycle callbacks‬
‭●‬ ‭No automatic wiring is performed. Dependencies must be‬ ‭●‬ l‭ifecycle callbacks are methods that allow you to hook into specific‬
‭manually injected.‬ ‭stages of a bean's lifecycle.‬
‭●‬ ‭Example‬‭: Default setting, no need for annotation or‬‭XML‬ ‭●‬ ‭These stages include bean creation, initialization, and destruction.‬
‭configuration.‬ ‭●‬ ‭The primary purpose of lifecycle callbacks is to provide a way to execute‬
‭custom logic during these stage :‬
‭ .‬
2 ‭ByType‬ ‭●‬ ‭Application Started‬‭:‬‭Triggered right after the application starts.‬
‭●‬ ‭ utowires by matching the type of the property to the type of a‬
A ‭●‬ ‭Spring Container Started:‬‭Triggered when the Spring IoC container‬
‭bean in the container.‬ ‭is initialized.‬
‭●‬ ‭Example‬‭::‬ ‭●‬ ‭Bean Constructed & Dependency Injection (DI‬‭)‭:‬ Beans are‬
‭ ML‬‭Configuration‬‭:‬
X ‭instantiated and dependencies are injected.‬
‭<‬‭bean id‬‭="‬‭b‭"‬ ‬‭class‬‭="‬‭com.example.B‬‭"/>‬ ‭●‬ ‭Custom Init Call:‬‭Custom initialization methods are called after the‬
‭<‬‭bean id‬‭="‬‭a‭"‬ ‬‭class‬‭="‬‭com.example.A‬‭"‬‭autowire‬‭="‬‭byType‬‭"/>‬ ‭bean is constructed.‬
J‭ ava‬‭Configuration‬‭:‬
‭●‬ ‭Other Methods Call:‬‭Regular business methods are invoked during‬
‭@‬‭Autowired‬ ‭the application's operation.‬
‭private‬‭B‬‭b‭;‬ ‬ ‭●‬ ‭Custom Destroy Call:‬‭Custom destruction methods are called‬
‭before the application shuts down.‬

‭ .‬
3 ‭ yName‬
B
‭●‬ ‭Autowires by matching the name of the property to the name of a‬
‭bean in the container.‬
‭●‬ ‭Example‬‭:‬

‭ ML‬‭Configuration‬‭:‬
X
‭<‬‭bean id‬‭="‬‭a‭"‬ ‬‭class‬‭="‬‭com.example.A‬‭"‬‭autowire‬‭="‬‭byName‬‭"/>‬
‭<‬‭bean id‬‭="‬‭b‭"‬ ‬‭class‬‭="‬‭com.example.B‬‭"/>‬

J‭ ava‬‭Configuration‬‭:‬
‭@‬‭Autowired‬
‭private‬‭B‬‭b‭;‬ ‬
‭5.17 Bean configuration‬ ‭MainApplication.java‬‭:‬
‭●‬ B‭ ean configuration in Spring refers to defining and managing beans‬ ‭ ackage‬‭com‬‭.‭e
p ‬ xample‬‭;‬
‭import‬‭org‬‭.‬‭springframework‬‭.‬‭context‬‭.‬‭ApplicationContext‬‭;‬
‭within the Spring IoC (Inversion of Control) container.‬ ‭import‬‭org‬‭.‭s‬ pringframework‬‭.‬‭context‬‭.‬‭annotation‬‭.‬‭AnnotationConfigApplicationContext‬‭;‬
‭●‬ ‭There are several ways to configure beans in Spring:‬ ‭import‬‭org‬‭.‬‭springframework‬‭.‬‭stereotype‬‭.‬‭Component‬‭;‬
‭●‬ ‭XML Configuration‬‭: Defining beans in XML files.‬
‭ ‬‭Component‬
@
‭●‬ ‭Annotation-Based Configuration‬‭: Using annotations‬‭to define and‬ ‭public‬‭class‬‭DemoApplication‬‭{‬
‭configure beans.‬ ‭public‬‭static‬‭void‬‭main‬‭(‭S‬ tring‬‭[]‬‭args‬‭)‬‭{‬
‭ApplicationContext‬‭context‬‭=‬‭new‬‭AnnotationConfigApplicationContext‬‭("‬‭com.example‬‭");‬
‭●‬ ‭Java-Based Configuration‬‭: Using Java classes to configure‬‭beans.‬
E‭ xampleBean‬‭exampleBean‬‭=‬‭context‬‭.‬‭getBean‬‭(‬‭ExampleBean‬‭.‬‭class‬‭);‬
‭exampleBean‬‭.‬‭display‬‭();‬
‭ .‬
1 ‭ ML Configuration‬
X ‭}‬
‭●‬ ‭Beans are defined in an XML file, typically named‬ ‭}‬
‭applicationContext.xml.‬

‭applicationContext.xml‬‭:‬ ‭ .‬ J‭ ava-Based Configuration‬


3
‭<‭b
‬ eans xmlns‬‭="‬‭http://www.springframework.org/schema/beans‬‭"‬ ‭●‬ ‭Using @Configuration and @Bean annotations to define and‬
‭xmlns‬‭:‭x‬ si‬‭="‬‭http://www.w3.org/2001/XMLSchema-instance‬‭"‬ ‭configure beans.‬
‭xsi‬‭:‭s‬ chemaLocation‬‭="‬‭http://www.springframework.org/schema/beans‬
‭http://www.springframework.org/schema/beans/spring-beans.xsd‬‭">‬
‭AppConfig.java‬‭:‬
‭<‬‭bean id‬‭="‬‭exampleBean‬‭"‬‭class‬‭="‬‭com.example.ExampleBean‬‭">‬ ‭package‬‭com‬‭.‬‭example‬‭;‬
‭<‭p
‬ roperty name‬‭="‬‭propertyName‬‭"‬‭value‬‭="‬‭propertyValue‬‭"/>‬
‭</‬‭bean‬‭>‬ i‭mport‬‭org‬‭.‬‭springframework‬‭.‬‭context‬‭.‬‭annotation‬‭.‬‭Bean‬‭;‬
‭</‬‭beans‬‭>‬ ‭import‬‭org‬‭.‬‭springframework‬‭.‬‭context‬‭.‬‭annotation‬‭.‬‭Configuration‬‭;‬

‭ ‬‭Configuration‬
@
‭ExampleBean.java‬‭:‬ ‭public‬‭class‬‭AppConfig‬‭{‬
‭ ackage‬‭com‬‭.‬‭example‬‭;‬
p
‭ ‬‭Bean‬
@
‭public‬‭class‬‭ExampleBean‬‭{‬
‭public‬‭ExampleBean‬‭exampleBean‬‭()‬‭{‬
‭private‬‭String‬‭propertyName‬‭;‬
‭ExampleBean‬‭bean‬‭=‬‭new‬‭ExampleBean‬‭();‬
‭bean‬‭.‬‭setPropertyName‬‭("‬‭propertyValue‬‭");‬
‭public‬‭void‬‭setPropertyName‬‭(‬‭String‬‭propertyName‬‭)‬‭{‬
‭return‬‭bean‬‭;‬
‭this‬‭.‬‭propertyName‬‭=‬‭propertyName‬‭;‬
‭}‬
‭}‬
‭}‬
‭public‬‭void‬‭display‬‭()‬‭{‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭Property Value:‬‭"‬‭+‬‭propertyName‬‭);‬ E‭ xampleBean.java‬‭: Same as xml configuration file‬
‭}‬ ‭MainApplication.java‬‭:‬
‭}‬ ‭ ackage‬‭com‬‭.‭e
p ‬ xample‬‭;‬
‭import‬‭org‬‭.‬‭springframework‬‭.‬‭context‬‭.‬‭ApplicationContext‬‭;‬
‭import‬‭org‬‭.‭s‬ pringframework‬‭.‬‭context‬‭.‬‭annotation‬‭.‬‭AnnotationConfigApplicationContext‬‭;‬
‭MainApplication.java‬‭:‬
‭ ackage‬‭com‬‭.‬‭example‬‭;‬
p ‭public‬‭class‬‭DemoApplication‬‭{‬
‭import‬‭org‬‭.‬‭springframework‬‭.‭c‬ ontext‬‭.‭A ‬ pplicationContext‬‭;‬ ‭public‬‭static‬‭void‬‭main‬‭(‭S‬ tring‬‭[]‬‭args‬‭)‬‭{‬
‭ApplicationContext‬‭context‬‭=‬‭new‬‭AnnotationConfigApplicationContext‬‭(‭A
‬ ppConfig‬‭.‬‭class‬‭);‬
‭import‬‭org‬‭.‬‭springframework‬‭.‭c‬ ontext‬‭.‭s‬ upport‬‭.‬‭ClassPathXmlApplicationContext‬‭;‬

‭public‬‭class‬‭DemoApplication‬‭{‬ E‭ xampleBean‬‭exampleBean‬‭=‬‭context‬‭.‬‭getBean‬‭(‬‭ExampleBean‬‭.‬‭class‬‭);‬
‭public‬‭static‬‭void‬‭main‬‭(‬‭String‬‭[]‬‭args‬‭)‬‭{‬ ‭exampleBean‬‭.‬‭display‬‭();‬
‭ApplicationContext‬‭context‬‭=‬‭new‬ ‭}‬
‭ClassPathXmlApplicationContext‬‭("‬‭applicationContext.xml‬‭");‬ ‭}‬

E‭ xampleBean‬‭exampleBean‬‭=‬‭(‬‭ExampleBean‬‭)‬‭context‬‭.‬‭getBean‬‭("‬‭exampleBean‬‭");‬ ‭5.18 Spring Boot supports‬


‭exampleBean‬‭.‬‭display‬‭();‬ ‭●‬ S‭ pring Boot supports various build systems that help manage‬
‭}‬
‭}‬ ‭dependencies, package applications, and automate build tasks.‬
‭●‬ ‭The two primary build systems used with Spring Boot are Maven and‬
‭ .‬
2 ‭ nnotation-Based Configuration‬
A ‭Gradle:‬
‭●‬ ‭Spring provides several annotations to define and configure beans.‬
‭1.Maven‬
‭●‬ ‭Maven is a widely-used build automation tool primarily for Java‬
E‭ xampleBean.java‬‭:‬
‭projects.‬
‭package‬‭com‬‭.‬‭example‬‭;‬
‭●‬ ‭It manages project dependencies, builds artifacts, and executes‬
‭import‬‭org‬‭.‬‭springframework‬‭.‬‭stereotype‬‭.‭C
‬ omponent‬‭;‬
‭various lifecycle phases like compile, test, package, and install.‬
‭ ‭C
@ ‬ omponent‬
‭public‬‭class‬‭ExampleBean‬‭{‬ ‭2.Gradle‬
‭private‬‭String‬‭propertyName‬‭=‬‭"‬‭propertyValue‬‭";‬
‭●‬ ‭ radle is a modern build automation tool that combines the‬
G
‭public‬‭void‬‭display‬‭()‬‭{‬
‭flexibility of Ant with the dependency management and‬
‭System‬‭.‬‭out‬‭.‬‭println‬‭("‬‭Property Value:‬‭"‬‭+‬‭propertyName‬‭);‬
‭}‬ ‭conventions of Maven.‬
‭}‬ ‭●‬ ‭It uses Groovy-based DSL (Domain Specific Language) or Kotlin for‬
‭configuring builds.‬
‭5.19 Choosing Between Maven and Gradle with Spring Boot‬ ‭●‬ p
‭ om.xml:‬‭Maven build configuration file defining‬‭dependencies,‬
‭plugins, and project settings‬
‭1.Maven‬‭:‬
‭●‬ ‭Widely adopted in the Java ecosystem.‬ ‭5.21 Logger‬
‭●‬ ‭Strong support for dependency management and project lifecycle‬
‭management.‬ ‭●‬ l‭ogging is an essential aspect of application development and‬
‭●‬ ‭XML-based configuration (pom.xml) might be familiar to many‬ ‭maintenance.‬
‭Java developers.‬ ‭●‬ ‭Spring Boot supports logging through various logging frameworks, with‬
‭2.Gradle:‬ ‭SLF4J (Simple Logging Facade for Java) and Logback being the default‬
‭●‬ ‭Offers flexibility and concise syntax with Groovy or Kotlin DSL.‬ ‭choices.‬
‭●‬ ‭Supports incremental builds and parallel execution of tasks.‬ ‭●‬ ‭SLF4J is an abstraction for various logging frameworks (e.g.,‬
‭●‬ ‭Preferred for its readability and customization options, especially‬ ‭java.util.logging, logback, log4j)‬‭allowing the end user to plug in the‬
‭for complex projects or where Kotlin is preferred.‬ ‭desired logging framework at deployment time.‬
‭●‬ ‭Use logging statements in your classes to output messages at different‬
‭5.20 Spring Boot Code Structure‬ ‭levels. Here’s how you can use them:‬

i‭mport‬‭org‬‭.‬‭slf4j‬‭.‬‭Logger‬‭;‬
‭import‬‭org‬‭.‬‭slf4j‬‭.‬‭LoggerFactory‬‭;‬
‭import‬‭org‬‭.‬‭springframework‬‭.‬‭stereotype‬‭.‬‭Service‬‭;‬

‭ ‬‭Service‬
@
‭public‬‭class‬‭MyService‬‭{‬
‭private‬‭static‬‭final‬‭Logger‬‭logger‬‭=‬‭LoggerFactory‬‭.‬‭getLogger‬‭(‬‭MyService‬‭.‬‭class‬‭);‬
‭public‬‭void‬‭doSomething‬‭()‬‭{‬
‭logger‬‭.‬‭trace‬‭("‬‭This is a TRACE message‬‭");‬
‭logger‬‭.‬‭debug‬‭("‬‭This is a DEBUG message‬‭");‬
‭logger‬‭.‬‭info‬‭("‬‭This is an INFO message‬‭");‬
‭logger‬‭.‬‭warn‬‭("‬‭This is a WARN message‬‭");‬
‭logger‬‭.‬‭error‬‭("‬‭This is an ERROR message‬‭");‬
‭}‬
‭}‬

‭5.22 Building RESTful web services‬


‭‬ B
● ‭ uilding RESTful web services with Spring Boot involves several steps‬
‭●‬ ‭including setting up your project, creating controllers, defining‬
‭endpoints, and handling requests and responses.‬
‭●‬ ‭Here's a step-by-step guide to building RESTful web services in Spring‬
‭Boot:‬
‭Step 1: Set Up Your Project‬
‭●‬ ‭First, set up your Spring Boot project using your preferred method (e.g.,‬
‭Spring Initializr, Maven, or Gradle).‬
‭Step 2: Create the Main Application Class‬
‭●‬ ‭This class will bootstrap your Spring Boot application.‬
‭DemoApplication.java‬‭:‬
‭ ackage‬‭com‬‭.‬‭example‬‭.‭d
p ‬ emo‬‭;‬
‭import‬‭org‬‭.‬‭springframework‬‭.‬‭boot‬‭.‭S‬ pringApplication‬‭;‬
‭import‬‭org‬‭.‬‭springframework‬‭.‬‭boot‬‭.‭a‬ utoconfigure‬‭.‭S‬ pringBootApplication‬‭;‬

‭ ‬‭SpringBootApplication‬
@
‭Explanation of C‬‭omponents‬ ‭public‬‭class‬‭DemoApplication‬‭{‬
‭public‬‭static‬‭void‬‭main‬‭(‬‭String‬‭[]‬‭args‬‭)‬‭{‬
‭ ‬ s‭ rc/main/java:‬‭Contains the Java source code for your application.‬
● ‭SpringApplication‬‭.‭r‬ un‬‭(‬‭DemoApplication‬‭.‭c‬ lass‬‭,‬‭args‬‭);‬
‭●‬ ‭com.example.myapplication:‬‭Root package structure. Replace‬ ‭}‬
‭com.example.myapplication‬‭with your actual package name.‬ ‭}‬
‭●‬ ‭MyApplication.java‬‭: Main class annotated with‬
S‭ tep 3: Create a REST Controller‬
‭@SpringBootApplication‬‭, which serves as the entry point of the‬
‭●‬ ‭Create a controller to handle HTTP requests.‬
‭application.‬
‭UserController.java‬‭:‬
‭●‬ ‭controller‬‭: Package for Spring MVC controllers handling HTTP requests.‬
‭ ackage‬‭com‬‭.‬‭example‬‭.‭d
p ‬ emo‬‭.‬‭controller‬‭;‬
‭●‬ ‭service‬‭: Package for service classes containing business logic.‬ ‭import‬‭org‬‭.‬‭springframework‬‭.‬‭web‬‭.‬‭bind‬‭.‬‭annotation‬‭.‬‭*‭;‬ ‬
‭●‬ ‭model‬‭: Package for‬‭POJO‬‭classes representing data entities.‬ ‭import‬‭java‬‭.‬‭util‬‭.‭A ‬ rrayList‬‭;‬
‭●‬ ‭src/main/resources‬‭: Contains non-Java resources.‬ ‭import‬‭java‬‭.‬‭util‬‭.‭L‬ ist‬‭;‬

‭●‬ ‭application.properties‬‭: Configuration file for application-specific‬ ‭ ‬‭RestController‬


@
‭properties. Can be replaced with‬‭application.yml‬‭for YAML-based‬ ‭@‬‭RequestMapping‬‭("‬‭/users‬‭")‬
‭configuration.‬ ‭public‬‭class‬‭UserController‬‭{‬
‭private‬‭List‬‭<‬‭User‬‭>‬‭users‬‭=‬‭new‬‭ArrayList‬‭<>();‬
‭●‬ ‭static‬‭: Directory for static assets like‬‭CSS, JavaScript, images,‬‭etc.‬ ‭@‬‭GetMapping‬
‭●‬ ‭templates‬‭: Directory for HTML templates used by Spring‬‭MVC‬ ‭public‬‭List‬‭<‬‭User‬‭>‬‭getAllUsers‬‭()‬‭{‬
‭(Thymeleaf, FreeMarker,‬‭etc.).‬ ‭return‬‭users‬‭;‬
‭}‬
‭●‬ ‭src/test/java‬‭: Contains test code.‬
‭●‬ ‭com.example.myapplication‬‭: Mirror package structure for testing.‬
‭●‬ ‭controller:‬‭Package for controller unit tests.‬
‭●‬ ‭service:‬‭Package for service unit tests.‬
‭ ‭G
@ ‬ etMapping‬‭("‬‭/{id}‬‭")‬ ‭"name"‬‭:‬‭"John Doe"‬
‭public‬‭User‬‭getUserById‬‭(@‬‭PathVariable‬‭int‬‭id‬‭)‬‭{‬ ‭}‬
‭return‬‭users‬‭.‭s‬ tream‬‭()‬
‭.‭fi‬ lter‬‭(‬‭user‬‭->‬‭user‬‭.‬‭getId‬‭()‬‭==‬‭id‬‭)‬ ‭ UT‬‭http‬‭:‬‭//localhost:8080/users/1‬ ‭//update the record at id=1‬
P
‭.‭fi
‬ ndFirst‬‭()‬ ‭Body‬‭:‬‭{‬
‭.‭o
‬ rElse‬‭(‭n‬ ull‬‭);‬ ‭"name"‬‭:‬‭"Jane Doe"‬
‭}‬ ‭}‬

‭ ‭P
@ ‬ ostMapping‬ ‭DELETE‬‭http‬‭:‬‭//localhost:8080/users/1‬ ‭//delete the record id=1‬
‭public‬‭User‬‭createUser‬‭(@‬‭RequestBody‬‭User‬‭user‬‭)‬‭{‬
‭users‬‭.‬‭add‬‭(‬‭user‬‭);‬
‭return‬‭user‬‭;‬ ‭5.23 @PathVariable‬
‭}‬
‭‬ P
● ‭ urpose‬‭: Extract values from the URI path.‬
‭ ‭P
@ ‬ utMapping‬‭("‬‭/{id}‬‭")‬ ‭●‬ ‭Usage‬‭: When values are part of the URL.‬
‭public‬‭User‬‭updateUser‬‭(@‬‭PathVariable‬‭int‬‭id‬‭,‬‭@‬‭RequestBody‬‭User‬‭updatedUser‬‭)‬‭{‬
‭●‬ ‭Example‬:‭ For URL /users/1, extract 1.‬
‭for‬‭(‭U ‬ ser‬‭user‬‭:‬‭users‬‭)‬‭{‬
‭if‬‭(‬‭user‬‭.‬‭getId‬‭()‬‭==‬‭id‬‭)‬‭{‬
‭ ‬‭GetMapping‬‭("‬‭/{id}‬‭")‬
@
‭user‬‭.‬‭setName‬‭(‬‭updatedUser‬‭.‭g‬ etName‬‭());‬
‭public‬‭String‬‭getUserById‬‭(@‬‭PathVariable‬‭("‬‭id‬‭")‬‭int‬‭userId‬‭)‬‭{‬
‭return‬‭user‬‭;‬
‭return‬‭"‬‭User ID:‬‭"‬‭+‬‭userId‬‭;‬
‭}‬
‭}‬
‭}‬
‭return‬‭null‬‭;‬
‭}‬ ‭5.24 @RequestParam‬
‭‬ P
● ‭ urpose‬‭: Extract query parameters from the URL.‬
‭ ‭D
@ ‬ eleteMapping‬‭("‬‭/{id}‬‭")‬
‭public‬‭String‬‭deleteUser‬‭(@‬‭PathVariable‬‭int‬‭id‬‭)‬‭{‬
‭●‬ ‭Usage‬‭: When values are passed as query parameters.‬
‭users‬‭.‬‭removeIf‬‭(‬‭user‬‭->‬‭user‬‭.‭g‬ etId‬‭()‬‭==‬‭id‬‭);‬ ‭●‬ ‭Example‬:‭ For URL /users?page=2&size=10, extract page=2 and size=10‬
‭return‬‭"‬‭User with ID‬‭"‬‭+‬‭id‬‭+‬‭"‬‭deleted.‬‭";‬
‭}‬ ‭ ‬‭GetMapping‬
@
‭}‬ ‭public‬‭String‬‭getUsers‬‭(@‬‭RequestParam‬‭("‬‭page‬‭")‬‭int‬‭page‬‭,‬‭@‭R
‬ equestParam‬‭("‬‭size‬‭")‬‭int‬
‭size‬‭)‬‭{‬
‭return‬‭"‬‭Page:‬‭"‬‭+‬‭page‬‭+‬‭"‬‭, Size:‬‭"‬‭+‬‭size‬‭;‬
S‭ tep 4: Create a User Model‬ ‭}‬
‭●‬ ‭Define a User model class to represent user data.‬
‭User.java‬‭:‬ ‭5.25 Using Both Together‬
‭package‬‭com‬‭.‬‭example‬‭.‬‭demo‬‭.‬‭controller‬‭;‬
‭‬ P
● ‭ urpose‬‭: Capture values from both the path and query parameters.‬
‭public‬‭class‬‭User‬‭{‬ ‭●‬ ‭Example‬‭: For URL /users/1/orders?page=1&size=5, extract userId=1,‬
‭private‬‭int‬‭id‬‭;‬ ‭page=1, and size=5‬
‭private‬‭String‬‭name‬‭;‬
‭ ‬‭GetMapping‬‭("‬‭/{id}/orders‬‭")‬
@
‭ ublic‬‭User‬‭()‬‭{‬
p ‭public‬‭String‬‭getUserOrders‬‭(@‬‭PathVariable‬‭("‬‭id‬‭")‬‭int‬‭userId‬‭,‬
‭}‬ ‭@‬‭RequestParam‬‭("‬‭page‬‭")‬‭int‬‭page‬‭,‬
‭@‬‭RequestParam‬‭("‬‭size‬‭")‬‭int‬‭size‬‭)‬‭{‬
‭public‬‭User‬‭(‭i‬nt‬‭id‬‭,‬‭String‬‭name‬‭)‬‭{‬ ‭return‬‭"‬‭User ID:‬‭"‬‭+‬‭userId‬‭+‬‭"‬‭, Page:‬‭"‬‭+‬‭page‬‭+‬‭"‭,‬ Size:‬‭"‬‭+‬‭size‬‭;‬
‭this‬‭.‬‭id‬‭=‬‭id‬‭;‬ ‭}‬
‭this‬‭.‬‭name‬‭=‬‭name‬‭;‬
‭}‬
‭5.26 GET API‬
‭public‬‭int‬‭getId‬‭()‬‭{‬ ‭‬ P
● ‭ urpose‬‭: Retrieve resources.‬
‭return‬‭id‬‭;‬ ‭●‬ ‭Example‬‭: Fetch user details.‬
‭}‬ ‭ ‬‭GetMapping‬‭("‬‭/{id}‬‭")‬
@
‭public‬‭User‬‭getUserById‬‭(@‬‭PathVariable‬‭("‬‭id‬‭")‬‭int‬‭userId‬‭)‬‭{‬
‭public‬‭void‬‭setId‬‭(‬‭int‬‭id‬‭)‬‭{‬ ‭return‬‭userService‬‭.‬‭findUserById‬‭(‭u
‬ serId‬‭);‬
‭this‬‭.‬‭id‬‭=‬‭id‬‭;‬ ‭}‬
‭}‬
‭5.27 POST API‬
‭public‬‭String‬‭getName‬‭()‬‭{‬ ‭‬ P
● ‭ urpose‬‭: Create new resources.‬
‭return‬‭name‬‭;‬ ‭●‬ ‭Example‬‭: Add a new user.‬
‭}‬
‭ ‬‭PostMapping‬
@
‭public‬‭User‬‭createUser‬‭(@‬‭RequestBody‬‭User‬‭user‬‭)‬‭{‬
‭public‬‭void‬‭setName‬‭(‬‭String‬‭name‬‭)‬‭{‬
‭return‬‭userService‬‭.‬‭saveUser‬‭(‬‭user‬‭);‬
‭this‬‭.‬‭name‬‭=‬‭name‬‭;‬
‭}‬
‭}‬
‭}‬ ‭5.28 PUT API‬
‭‬ P
● ‭ urpose‬‭: Update existing resources.‬
S‭ tep 5: Running the Application‬ ‭●‬ ‭Example‬‭: Update user details.‬
‭●‬ ‭Run the DemoApplication class. Your Spring Boot application will start,‬ ‭ ‬‭PutMapping‬‭("‬‭/{id}‬‭")‬
@
‭and the embedded Tomcat server will be launched.‬ ‭public‬‭User‬‭updateUser‬‭(@‬‭PathVariable‬‭("‬‭id‬‭")‬‭int‬‭userId‬‭,‬‭@‬‭RequestBody‬‭User‬
‭userDetails‬‭)‬‭{‬
S‭ tep 6: Testing Your RESTful Web Service‬ ‭return‬‭userService‬‭.‬‭updateUser‬‭(‬‭userId‬‭,‬‭userDetails‬‭);‬
‭}‬
‭●‬ ‭You can use tools like Postman or curl to test your RESTful web service.‬
‭5.29 DELETE API‬
‭●‬ ‭Examples:‬
‭‬ P
● ‭ urpose‬‭: Delete resources.‬
‭GET‬‭http‬‭:‬‭//localhost:8080/users‬ ‭// get all users‬ ‭●‬ ‭Example‬‭: Remove a user.‬
‭ ‬‭DeleteMapping‬‭("‬‭/{id}‬‭")‬
@
‭GET‬‭http‬‭:‬‭//localhost:8080/users/1‬ ‭// get user id =1‬ ‭public‬‭ResponseEntity‬‭<‬‭Void‬‭>‬‭deleteUser‬‭(@‬‭PathVariable‬‭("‬‭id‬‭")‬‭int‬‭userId‬‭)‬‭{‬
‭userService‬‭.‭d
‬ eleteUser‬‭(‬‭userId‬‭);‬
‭ OST‬‭http‬‭:‭/‬ /localhost:8080/users‬ ‭// store the record‬
P ‭return‬‭ResponseEntity‬‭.‬‭noContent‬‭().‬‭build‬‭();‬
‭Body‬‭:‬‭{‬ ‭}‬
‭"id"‬‭:‬‭1‭,‬‬
‭5.30 REST Controller‬ ‭5. Controller Class‬
‭●‬ ‭Handle web requests.‬
‭●‬ ‭Defines RESTful web services‬ ‭ ‬‭Controller‬
@
‭ ‬‭RestController‬
@ ‭@‬‭RequestMapping‬‭("‬‭/users‬‭")‬
‭@‬‭RequestMapping‬‭("‬‭/api/users‬‭")‬ ‭public‬‭class‬‭UserController‬‭{‬
‭public‬‭class‬‭UserController‬‭{‬ ‭@‬‭Autowired‬
‭@‭A‬ utowired‬ ‭private‬‭UserService‬‭userService‬‭;‬
‭private‬‭UserService‬‭userService‬‭;‬
‭}‬ ‭ ‬‭GetMapping‬
@
‭public‬‭String‬‭getAllUsers‬‭(‬‭Model‬‭model‬‭)‬‭{‬
‭model‬‭.‬‭addAttribute‬‭("‬‭users‬‭",‬‭userService‬‭.‭g‬ etAllUsers‬‭());‬
‭5.31 Request Mapping‬ ‭return‬‭"‬‭user-list‬‭";‬
‭}‬
‭‬ M
● ‭ aps HTTP requests to handler methods.‬
‭●‬ ‭Maps base URL for all endpoints.‬ ‭ ‬‭GetMapping‬‭("‬‭/{id}‬‭")‬
@
‭ ‬‭GetMapping‬
@ ‭public‬‭String‬‭getUserById‬‭(@‬‭PathVariable‬‭("‬‭id‬‭")‬‭Long‬‭id‬‭,‬‭Model‬‭model‬‭)‬‭{‬
‭public‬‭List‬‭<‭U
‬ ser‬‭>‬‭getAllUsers‬‭()‬‭{‬ ‭model‬‭.‬‭addAttribute‬‭("‬‭user‬‭",‬‭userService‬‭.‬‭getUserById‬‭(‬‭id‬‭));‬
‭return‬‭userService‬‭.‬‭getAllUsers‬‭();‬ ‭return‬‭"‬‭user-detail‬‭";‬
‭}‬ ‭}‬

‭ ‬‭GetMapping‬‭("‬‭/{id}‬‭")‬
@ ‭ ‬‭GetMapping‬‭("‬‭/new‬‭")‬
@
‭public‬‭User‬‭getUserById‬‭(@‬‭PathVariable‬‭Long‬‭id‬‭)‬‭{‬ ‭public‬‭String‬‭createUserForm‬‭(‬‭Model‬‭model‬‭)‬‭{‬
‭return‬‭userService‬‭.‬‭getUserById‬‭(‬‭id‬‭);‬ ‭model‬‭.‬‭addAttribute‬‭("‬‭user‬‭",‬‭new‬‭User‬‭());‬
‭}‬ ‭return‬‭"‬‭user-form‬‭";‬
‭}‬
‭ ‬‭PostMapping‬
@
‭public‬‭User‬‭createUser‬‭(@‬‭RequestBody‬‭User‬‭user‬‭)‬‭{‬ ‭ ‬‭PostMapping‬
@
‭return‬‭userService‬‭.‬‭saveUser‬‭(‬‭user‬‭);‬ ‭public‬‭String‬‭createUser‬‭(@‬‭ModelAttribute‬‭("‬‭user‬‭")‬‭User‬‭user‬‭)‬‭{‬
‭}‬ ‭userService‬‭.‭s‬ aveUser‬‭(‬‭user‬‭);‬
‭return‬‭"‬‭redirect:/users‬‭";‬
‭ ‬‭PutMapping‬‭("‬‭/{id}‬‭")‬
@ ‭}‬
‭public‬‭User‬‭updateUser‬‭(@‬‭PathVariable‬‭Long‬‭id‬‭,‬‭@‬‭RequestBody‬‭User‬‭user‬‭)‬‭{‬
‭user‬‭.‬‭setId‬‭(‭i‬d‬‭);‬ ‭ ‬‭GetMapping‬‭("‬‭/edit/{id}‬‭")‬
@
‭return‬‭userService‬‭.‬‭saveUser‬‭(‬‭user‬‭);‬ ‭public‬‭String‬‭editUserForm‬‭(@‬‭PathVariable‬‭("‬‭id‬‭")‬‭Long‬‭id‬‭,‬‭Model‬‭model‬‭)‬‭{‬
‭}‬ ‭model‬‭.‬‭addAttribute‬‭("‬‭user‬‭",‬‭userService‬‭.‬‭getUserById‬‭(‬‭id‬‭));‬
‭return‬‭"‬‭user-form‬‭";‬
‭ ‬‭DeleteMapping‬‭("‬‭/{id}‬‭")‬
@ ‭}‬
‭public‬‭void‬‭deleteUser‬‭(@‬‭PathVariable‬‭Long‬‭id‬‭)‬‭{‬
‭userService‬‭.‬‭deleteUser‬‭(‭i‬d‬‭);‬ ‭ ‬‭PostMapping‬‭("‬‭/{id}‬‭")‬
@
‭}‬ ‭public‬‭String‬‭updateUser‬‭(@‬‭PathVariable‬‭("‬‭id‬‭")‬‭Long‬‭id‬‭,‬‭@‬‭ModelAttribute‬‭("‬‭user‬‭")‬
‭User‬‭user‬‭)‬‭{‬
‭user‬‭.‭s‬ etId‬‭(‬‭id‬‭);‬
‭5.32 Request Body‬ ‭userService‬‭.‭s‬ aveUser‬‭(‬‭user‬‭);‬
‭●‬ ‭Binds HTTP request body to a method parameter.‬ ‭return‬‭"‬‭redirect:/users‬‭";‬
‭ ‬‭PostMapping‬
@ ‭}‬
‭public‬‭User‬‭createUser‬‭(@‬‭RequestBody‬‭User‬‭user‬‭)‬‭{‬
‭return‬‭userService‬‭.‬‭saveUser‬‭(‬‭user‬‭);‬ ‭ ‬‭GetMapping‬‭("‬‭/delete/{id}‬‭")‬
@
‭}‬ ‭public‬‭String‬‭deleteUser‬‭(@‬‭PathVariable‬‭("‬‭id‬‭")‬‭Long‬‭id‬‭)‬‭{‬
‭userService‬‭.‭d‬ eleteUser‬‭(‬‭id‬‭);‬
‭return‬‭"‬‭redirect:/users‬‭";‬
‭5.33 Building Web Applications with Spring Boot‬ ‭}‬
‭1. Set Up Project‬ ‭}‬
‭●‬ ‭ reate a Spring Boot project with dependencies:‬‭Spring Web‬‭,‬‭Thymeleaf‬‭,‬
C
‭Spring Data JPA‬‭.‬
‭2. Model Class‬ ‭ . Thymeleaf Templates‬
6
‭●‬ ‭Define the entity.‬ ‭src/main/resources/templates/user-list.html:‬

‭ ‬‭Entity‬
@ <‭ !‬‭DOCTYPE‬‭html‬‭>‬
‭public‬‭class‬‭User‬‭{‬ ‭<‭h ‬ tml‬‭xmlns:th‬‭="‬‭http://www.thymeleaf.org‬‭"‬‭>‬
‭@‭I‬d‬ ‭<‭h ‬ ead‬‭><‬‭title‬‭>‬‭User List‬‭</‬‭title‬‭></‬‭head‬‭>‬
‭@‭G ‬ eneratedValue‬‭(‭s‬ trategy‬‭=‬‭GenerationType‬‭.‬‭IDENTITY‬‭)‬ ‭<‭b ‬ ody‬‭>‬
‭private‬‭Long‬‭id‬‭;‬ ‭<‭h ‬ 1‬‭>‬‭User List‬‭</‬‭h1‬‭>‬
‭private‬‭String‬‭name‬‭;‬ ‭<‭a‬ ‬‭href‬‭="‬‭/users/new‬‭"‬‭>‬‭Add New User‬‭</‬‭a‬‭>‬
‭private‬‭String‬‭email‬‭;‬ ‭<‭t‬ able‬‭>‬
‭// Getters and setters‬ ‭<‬‭tr‬‭><‬‭th‬‭>‭I‬D‬‭</‬‭th‬‭><‬‭th‬‭>‬‭Name‬‭</‬‭th‬‭><‬‭th‬‭>‬‭Email‬‭</‬‭th‬‭><‬‭th‬‭>‬‭Actions‬‭</‬‭th‬‭></‬‭tr‬‭>‬
‭}‬ ‭<‬‭tr‬‭th:each‬‭="‬‭user : ${users}‬‭"‬‭>‬
‭<‬‭td‬‭th:text‬‭="‬‭${user.id}‬‭"‬‭></‬‭td‬‭>‬
‭<‬‭td‬‭th:text‬‭="‬‭${user.name}‬‭"‭>‬ </‬‭td‬‭>‬
‭3. Repository Interface‬
‭<‬‭td‬‭th:text‬‭="‬‭${user.email}‬‭"‬‭></‬‭td‬‭>‬
‭●‬ ‭Interface for data access.‬
‭ ublic‬‭interface‬‭UserRepository‬‭extends‬‭JpaRepository‬‭<‬‭User‬‭,‬‭Long‬‭>‬‭{‬
p
‭}‬
‭<‬‭td‬‭>‬
‭<‬‭a‬‭th:href‬‭="‬‭@{/users/{id}(id=${user.id})}‬‭"‬‭>‬‭View‬‭</‬‭a‬‭>‬
‭5. Service Class‬ ‭<‬‭a‬‭th:href‬‭="‬‭@{/users/edit/{id}(id=${user.id})}‬‭"‬‭>‬‭Edit‬‭</‬‭a‭>‬ ‬
‭●‬ ‭Handle business logic.‬ ‭<‬‭a‬‭th:href‬‭="‬‭@{/users/delete/{id}(id=${user.id})}‬‭"‬‭>‭D ‬ elete‬‭</‬‭a‭>‬ ‬
‭ ‬‭Service‬
@ ‭</‬‭td‬‭>‬
‭public‬‭class‬‭UserService‬‭{‬ ‭</‬‭tr‬‭>‬
‭@‭A ‬ utowired‬ ‭</‬‭table‬‭>‬
‭private‬‭UserRepository‬‭userRepository‬‭;‬ ‭</‬‭body‬‭>‬
‭public‬‭List‬‭<‬‭User‬‭>‬‭getAllUsers‬‭()‬‭{‬‭return‬‭userRepository‬‭.‬‭findAll‬‭();‬‭}‬ ‭</‬‭html‬‭>‬
‭public‬‭User‬‭getUserById‬‭(‭L‬ ong‬‭id‬‭)‬‭{‬‭return‬‭userRepository‬‭.‭fi‬ ndById‬‭(‭i‬d‬‭).‬‭orElse‬‭(‭n
‬ ull‬‭);‬‭}‬
‭public‬‭User‬‭saveUser‬‭(‬‭User‬‭user‬‭)‬‭{‬‭return‬‭userRepository‬‭.‭s‬ ave‬‭(‬‭user‬‭);‬‭}‬
‭public‬‭void‬‭deleteUser‬‭(‬‭Long‬‭id‬‭)‬‭{‬‭userRepository‬‭.‭d
‬ eleteById‬‭(‭i‬d‬‭);‬‭}‬
‭}‬
‭src/main/resources/templates/user-form.html:‬
<‭ !‬‭DOCTYPE‬‭html‬‭>‬
‭<‭h ‬ tml‬‭xmlns:th‬‭="‬‭http://www.thymeleaf.org‬‭"‭>‬ ‬
‭<‭h ‬ ead‬‭><‬‭title‬‭>‬‭User Form‬‭</‬‭title‬‭></‬‭head‬‭>‬
‭<‭b ‬ ody‬‭>‬
‭<‭h ‬ 1‬‭>‭U ‬ ser Form‬‭</‬‭h1‬‭>‬
‭<‭f‬ orm‬‭th:action‬‭="‬‭@{/users}‬‭"‬‭th:object‬‭="‬‭${user}‬‭"‬‭method‬‭="‬‭post‬‭"‭>‬ ‬
‭<‬‭input‬‭type‬‭="‬‭hidden‬‭"‬‭th:field‬‭="‬‭*{id}‬‭"‭>‬ ‬
‭<‬‭div‬‭><‬‭label‬‭for‬‭="‬‭name‬‭"‭>‬ ‭N ‬ ame:‬‭</‬‭label‬‭><‬‭input‬‭type‬‭="‬‭text‬‭"‬‭id‬‭="‬‭name‬‭"‬
‭th:field‬‭="‬‭*{name}‬‭"‬‭></‬‭div‬‭>‬
‭<‬‭div‬‭><‬‭label‬‭for‬‭="‬‭email‬‭"‭>‬ ‭E‬ mail:‬‭</‬‭label‬‭><‬‭input‬‭type‬‭="‬‭text‬‭"‬‭id‬‭="‬‭email‬‭"‬
‭th:field‬‭="‬‭*{email}‬‭"‬‭></‬‭div‬‭>‬
‭<‬‭div‬‭><‬‭button‬‭type‬‭="‬‭submit‬‭"‬‭>‭S‬ ave‬‭</‬‭button‬‭></‬‭div‬‭>‬
‭</‬‭form‬‭>‬
‭<‭a‬ ‬‭href‬‭="‬‭/users‬‭"‭>‬ ‬‭Back to List‬‭</‬‭a‭>‬ ‬
‭</‬‭body‬‭>‬
‭</‬‭html‬‭>‬

‭src/main/resources/templates/user-detail.html:‬

<‭ !‬‭DOCTYPE‬‭html‬‭>‬
‭<‭h ‬ tml‬‭xmlns:th‬‭="‬‭http://www.thymeleaf.org‬‭"‭>‬ ‬
‭<‭h ‬ ead‬‭><‬‭title‬‭>‬‭User Detail‬‭</‬‭title‬‭></‬‭head‬‭>‬
‭<‭b ‬ ody‬‭>‬
‭<‭h ‬ 1‬‭>‭U ‬ ser Detail‬‭</‬‭h1‬‭>‬
‭<‭p ‬ ‭>‬ ‬‭ID:‬‭<‬‭span‬‭th:text‬‭="‬‭${user.id}‬‭"‭>‬ </‬‭span‬‭></‬‭p‭>‬ ‬
‭<‭p ‬ ‭>‬ ‬‭Name:‬‭<‬‭span‬‭th:text‬‭="‬‭${user.name}‬‭"‭>‬ </‬‭span‬‭></‬‭p‭>‬ ‬
‭<‭p ‬ ‭>‬ ‬‭Email:‬‭<‭s‬ pan‬‭th:text‬‭="‬‭${user.email}‬‭"‬‭></‬‭span‬‭></‬‭p‭>‬ ‬
‭<‭a‬ ‬‭href‬‭="‬‭/users‬‭"‭>‬ ‬‭Back to List‬‭</‬‭a‭>‬ ‬
‭</‬‭body‬‭>‬
‭</‬‭html‬‭>‬

‭ . Running the Application‬


7
‭●‬ ‭Start the Spring Boot application:‬‭Run the DemoApplication class.‬
‭●‬ ‭Access the Application‬‭: Open a web browser and navigate to‬
‭http://localhost:8080/users‬‭.‬

You might also like