How to Implement Design Patterns in Spring Boots Application
Singleton PatternThe Singleton pattern ensures a class has only one instance and provides a global point of access to it. In Spring, beans are singleton by default.
For Example :
Factory Pattern
The Factory pattern is used to create objects without specifying the exact class of object that will be created.
Usage in a Controller
Prototype Pattern
The Prototype pattern is used to create a new instance of a class by copying or cloning an existing instance.
Adapter Pattern
The Adapter pattern allows incompatible interfaces to work together. In a Spring Boot project, it can be used to integrate third-party libraries.
Usage:
Decorator Pattern
The Decorator pattern is used to add behavior to individual objects without affecting the behavior of other objects from the same class.
Example:
Observer Pattern
The Observer pattern allows an object to notify other objects when its state changes.
Example:
Template Method Pattern
The Template Method pattern defines the skeleton of an al algorithm in a method, deferring some steps to subclasses.
Example:
Conclusion: Design patterns are critical in developing well-structured, maintainable, and flexible Spring Boot applications. They help in decoupling components, reusing code, and making the application easier to understand and extend. By leveraging these patterns in your Spring Boot project, you can achieve better code organization and more effective application design.
Inversion of Control (IoC) in Spring Frameworks
Key Concepts of Inversion of Control :
- Dependency Injection (DI)
- The most common implementation of IoC.
- Dependencies (objects that a class requires to function) are injected into the class rather than the class creating them itself.
- Can be implemented in three main ways: constructor injection, setter injection, and interface injection.
- Service Locator
- An alternative to dependency injection.
- A central registry (the service locator) provides dependencies upon request.
- Can lead to less transparency in dependency management compared to DI.
- Without IoC.
- With IoC (Dependency Injection).
Benefits of IoC:
Spring (Java): Provides comprehensive support for dependency injection and IoC.
Example in Spring Framework:
Spring Framework is a well-known example of an IoC container. Here’s how dependency injection is typically configured in a Spring application:
XML Configuration:
Multiples DATABASE Connection and Data shifting Using Spring Boots API
1. application.properties File for Add multiple DataBase Connection.
- Create Spring Boot Java (Microservices)
- Project Name: DataShiftingConfigSpringBoots
- Group: DataShifting-a143mk
- Artifactid: DataShifting-a143mk
- Description: DataBase Connection and Data shifting using Spring boot API.
- Package: DataShiftinga143mk
- spring-boot-configuration-processor
- ojdbc8
while creating the project pom.xml file and some maven dependency, main class is automatically created when web add any dependency like.
pom.xml
1. Configure application.properties
Create DataBase Configuration java Files :
DBConfigDATABASE1.java
DBConfigDATABASE2.java
DBConfigDATABASE3.java
Create Scheduler ClassCreate Scheduler for Auto run Program and import some Packages Scheduler.java as below:
Have you seen the previous post? You all know how to learn and create Entity Bean, Repository, Service Implementation in the project. If you don't know about it then go to the previous post and learn it.
How to PACKAGE Spring Boot application to JAR and WAR
The default packaging type of a Spring Boot project is jar if the
- CLEAN
- BUILD
- INSTALL
- PACKAGE.................etc.
LDAP Authentication with Spring Boots
The client (an LDAP-ready system or application) sends a request to access information stored within an LDAP database. The client provides their LDAP server user credentials (username and password). The LDAP server cross-checks the user’s submitted credentials against the core user identity data stored in its LDAP database. If the provided credentials match the stored core user identity, the client can access the requested information. Incorrect credentials will lead to denied access to the LDAP database.
ldap.enable=true |
Enable
/ disable LDAP support. |
ldap.ldapUrl=ldap://HostIP:389 |
The
URL/IP to your LDAP server, including port |
ldap.ldapSystemDN=cn=Administrator,dc=olattest,dc=org |
System
user: used for getting all users and connection testing |
ldap.ldapSystemPW=mySuperPassword |
The
LDAP password of the system user |
ldap.ldapBases=
ou=person,dc=olattest,dc=org |
List
of bases where to find users. To use multiple bases you must edit the config
file manually |
ldap.sslEnabled
= false |
Enable
SSL connection |
ldap.trustStoreLocation=/usr/lib/j2sdk1.5-sun/jre/lib/security/cacerts |
Location
of the Java trust store |
ldap.trustStorePwd=changeit |
The
Java trust store password |
ldap.trustStoreType=JKS |
The
Java trust store type |
ldap.cacheLDAPPwdAsOLATPwdOnLogin=true |
When
users log in via LDAP, the system can keep a copy of the password as
encrypted hash in the database. This makes OLAT more independent from an
offline LDAP server and users can use their LDAP password to use the WebDAV
functionality. When setting to true (recommended), make sure you
configured password.change.allowed=false |
ldap.convertExistingLocalUsersToLDAPUsers=true |
When
the system detects an LDAP user that does already exist in OLAT but is not
marked as LDAP user, the OLAT user can be converted to an LDAP managed user.
When enabling this feature you should make sure that you don't have a user
'administrator' in your ldapBases (not a problem but not recommended) |
ldap.deleteRemovedLDAPUsersOnSync=false |
Users
that have been created vial LDAP sync but now can't be found on the LDAP
anymore can be deleted automatically. If unsure, set to false and delete
those users manually in the LDAP administration panel. |
ldap.deleteRemovedLDAPUsersPercentage=50 |
Sanity
check when deleteRemovedLDAPUsersOnSync is set to 'true': if more than the
defined percentages of user accounts are not found on the LDAP server and
thus recognized as to be deleted, the LDAP sync will not happen and require a
manual triggering of the delete job from the admin interface. This should prevent
accidential deletion of OLAT user because of temporary LDAP problems or user
relocation on the LDAP side. Value= 0 (never delete) to 100 (always delete). |
ldap.ldapSyncOnStartup=true |
Should
users be created and synchronized automatically on startup? If you set this
and the cron configuration to false, the users will be generated on-the-fly
when they log in the first time. |
ldap.ldapSyncCronSync=true |
Should
users be created and synchronized periodically, not just during startup? If
you set this and the startup configuration to false, the users will be
generated on-the-fly when they log in the first time. |
ldap.ldapSyncCronSyncExpression=0
0 * * * ? |
Set
the rule how often the LDAP synchronization should take place. Default is
every hour. For the cron syntax see quartz cron syntax documentation |
ldap.ldapUserObjectClass=person |
Configuration
for syncing user attributes during login or cron and batch sync. The user
object class to use. |
ldap.ldapUserCreatedTimestampAttribute=createdTimestamp |
The
LDAP attribute that tells the creation date of the LDAP user. In active
Directory this is typically "whenCreated", in OpenLDAP
"createdTimestamp". If not available, use the same as for the last
modified date. |
ldap.ldapUserLastModifiedTimestampAttribute=modifyTimestamp |
The
LDAP attribute that tells the creation date of the LDAP user. In active
Directory this is typically "whenChanged", in OpenLDAP
"modifiedTimestamp" |
ldap.attributename.useridentifyer=uid |
Mapping
of the LDAP user identifyer to an OLAT user. In Active Directory this is
typically "sAMAccountName", in OpenLDAP "uid". |
ldap.attributename.email=mail |
Mapping
of the mandatory OLAT user properties. In this example the LDAP attribute
"mail" will be mapped to the OLAT user property "email" |
ldap.attributename.firstName=givenName |
Mapping
of the mandatory OLAT user properties. In this example the LDAP attribute
"givenName" will be mapped to the OLAT user property
"firstname" |
ldap.attributename.lastName=sn |
Mapping
of the mandatory OLAT user properties. In this example the LDAP attribute
"sn" will be mapped to the OLAT user property "lastName" |
olatprovider.enable=false |
Disable
the normal OLAT login screen. When you
set ldap.cacheLDAPPwdAsOLATPwdOnLogin=true, a fallback to the normal
OLAT login is made in case the LDAP server can not be reached. You can set
this variable also to true to have the OLAT and the LDAP login screen
available |
default.auth.provider=LDAP |
Use
the LDAP login screen as the default login screen. |
- Create Spring Boot Java (Microservices) Project Example
- Project Name: LdapAuthenticationConfigSpringBoots.
- Group:Ldap-Auth-a143mk.
- Artifactid:Ldap-Auth-a143mk.
- Description:Learn-Configure LDAP Authentication.
- Package:LDAPAutha143mk.
- Spring Web
- Spring ldap
- ldapsdk
- ldap security
while creating the project pom.xml file and some maven dependency, main class is automatically created when web add any dependency like.
pom.xml
Configure application.properties
Create ldap-data.ldif :
Directory Server uses LDAP Data Interchange Format is a standard text format for representing LDAP objects and LDAP updates (add, modify, delete, modify DN) in a textual form.
WebSecurityConfig ClassCreate new package, named LDAPAutha143mk.config. In this package, create Configuration class name is WebSecurityConfig.java as below:
Create Controllers
Create new package named LDAPAutha143mk.controller. In this package, create new java class named LoginWelcomeController.java
Learn I- Report configuration with Spring boot/Microservices using Java
iReport is a powerful graphical design tool for report designers and power users to define reports for execution using the JasperReports engine. Whether you are using the JasperReports library by itself as a critical component of your application or you are using it as a component of the JasperReports Server application, iReport can help you design reports to meet the most complex reporting demands. iReport is built on the NetBeans platform and is available as a standalone application or as a Netbeans plug-in.
Requirements For current Download JasperReports iReport Tools , Ckeck It is recommended that you run iReport with JDK v1.5,v1.6 or v1.7. Note that for Windows Vista users, it is recommended that you run JDK v1.7.
- Create Spring Boot Java (Microservices)
- Create/Design report.jrxml
- Project Name: I-ReportConfigurationApp
- Group:I-Report-a143mk
- Artifactid:I-Report-a143mk
- Description:Learn I- Report configuration with Microservices Application
- Package:I-Report-a143mk
- Spring Web
- Spring JPA
- Jasperreports
- javax.persistence
- Tomcat
- javax.servlet
- mysql
- jstl
while creating the project pom.xml file and some maven dependency, main class is automatically created when web add any dependency like.
pom.xml
2. Create Table: Table Name is product.
Configure application.properties
Create new package named IReporta143mk.repositories. In this package create the ProductRepository interface implements from CrudRepository interface of Spring Data JPA that provides CRUD Operations for an entity.
Create new package named IReporta143mk.services. In this package create the ProductService interface and ProductServiceImpl as below:
First one is ProductService.java
Create new package named IReporta143mk.controllers. In this package, create new java class named ProductController.java
Create new folder name is jsp. In thes folder /src/main/webapp/WEB-INF/jsp/index.jsp
Single Sign-On (SSO) Using SMTP Authentication With Spring Boot Microservices
- LoginMailAuthHost_PortA143mk();
- OutLookMailAuthA143mk();
- GmailMailAuthA143mk();
while creating the project pom.xml file and some maven dependency, main class is automatically created when web add any dependency like.
pom.xml