Spring architecture and Spring boot

Spring is well-organized architecture consisting  of seven modules. Modules in the Spring framework are:

Spring AOP
One of the key components of Spring is the AOP framework. AOP is used in Spring:

To provide declarative enterprise services, especially as a replacement for EJB declarative services. The most important such service is declarative transaction management, which builds on Spring’s transaction abstraction.
To allow users to implement custom aspects, complementing their use of OOP with AOP
Spring ORM
The ORM package is related to the database access. It provides integration layers for popular object-relational mapping APIs, including JDO, Hibernate and iBatis.

Spring Web
The Spring Web module is part of Spring?s web application development stack, which includes Spring MVC.

Spring DAO
The DAO (Data Access Object) support in Spring is primarily for standardizing the data access work using the technologies like JDBC, Hibernate or JDO.

Spring Context
This package builds on the beans package to add support for message sources and for the Observer design pattern, and the ability for application objects to obtain resources using a consistent API.

Spring Web MVC
This is the Module which provides the MVC implementations for the web applications.

Spring Core
The Core package is the most import component of the Spring Framework.
This component provides the Dependency Injection features. The BeanFactory  provides a factory pattern which separates the dependencies like initialization, creation and access of the objects from your actual program logic.

Read more


Static keyword in java

Static keyword in java

The static keyword in java is used for memory management in JAVA. Static keywords belong to the class rather than the instance/object of a class.
We can use static keywords with:
  1. Variable
  2. Method
  3. Block
  4. Class

1. Static variable in Java

You can declare a static variable by simply use of static keyword with a variable. 
  • Static variables are also known as a class variable because they are associated with the class and common for all the objects of a class.
  • Static variables are declared using the static keyword within a class outside any method/constructor/block.
  • When a class created then one copy of a static variable created in memory. Static variables would be common for all objects of class because a static variable is associated with a class. 
  • Static variables are created at the start of program execution and destroyed automatically when execution ends.
  • Initialization of Static Variable is not Mandatory. Its default value is depending on the data type of variable.
  • If we access the static variable through an object the compiler will show the warning message. The compiler will replace the object name to the class name automatically.
  • We can create static variables at class-level only.

2. Static method in Java

You can declare a static method by using of static keyword.
1. Static methods are belonging to a class.
2. You can access a static method by using the class name. You do not have the need to create an object.
3. A static method can access only static variables. If you write any non-static variable in a static method, then it will throw an exception at compilation time.
Why a static method is important: Static method can access without any object. They can access directly by class name. The most common example of a static method is main( ) method. If it were a non-static method, JVM creates an object first then call main() method that will lead the problem of extra memory allocation.

3. Static block in Java

If you want to do some calculation and initialize the static variables you can use static block. The static block is get executed once when the class is first loaded.

4. Static class in Java

You can read the static class from my previous post
--
Cheers
Krishna babu Ghanta
Mobile : 303-886-1490 
Time zone : CST

Read more


Java Springboot Microservices FAQs

1. Is Spingboot thread-safe? 

It depends. The main factor which determines the thread-safety of a component is its scope.

In a standard servlet-based Spring web application, every new HTTP request generates a new thread. If the container creates a new bean instance just for that particular request, we can say this bean is thread-safe.

Is Spring singleton thread-safe?
The short answer is: no, it isn’t.

Reference: http://dolszewski.com/spring/spring-bean-thread-safety-guide/

2. How do we implement spring security?
Dependency :

    org.springframework.boot
    spring-boot-starter-security

start by creating a Spring Security configuration class that extends WebSecurityConfigurerAdapter. By adding @EnableWebSecurity, we get Spring Security and MVC integration support.

Reference: https://developer.okta.com/blog/2018/10/05/build-a-spring-boot-app-with-user-authentication

3. How to cancel an order for user-specific settings at the spring level? 

3b. Multi calls between ms to ms? 


4. Tomcat Max Threads settings in Springboot application? 


server.tomcat.max-connections
8192
Maximum number of connections that the server accepts and processes at any given time. Once the limit has been reached, the operating system may still accept connections based on the "acceptCount" property.
server.tomcat.max-http-form-post-size
2MB
Maximum size of the form content in any HTTP post request.
server.tomcat.max-swallow-size
2MB
Maximum amount of request body to swallow.
server.tomcat.max-threads
200
Maximum amount of worker threads.
server.tomcat.mbeanregistry.enabled
false
Whether Tomcat's MBean Registry should be enabled.
server.tomcat.min-spare-threads
10
Minimum amount of worker threads.
server.tomcat.port-header
X-Forwarded-Port
Name of the HTTP header used to override the original port value.
server.tomcat.processor-cache
200
Maximum number of idle processors that will be retained in the cache and reused with a subsequent request. When set to -1 the cache will be unlimited with a theoretical maximum size equal to the maximum number of connections.
server.tomcat.protocol-header
Header that holds the incoming protocol, usually named "X-Forwarded-Proto".
Reference :
https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html

5. Sort Employee salary using Java Streams and display salary which is greater than X amount? Java 8 - streams, lambda expression


// find employees whose salaries are above 10000
        empList.stream().filter(emp->emp.getSalary() > 10000).forEach(System.out::println);

Reference :
https://www.java2novice.com/java-8/streams/filter-method-example/

6. When do we use java inner classes?
It is a way of logically grouping classes that are only used in one place: If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.


Use a non-static nested class (or inner class) if you require access to an enclosing instance's non-public fields and methods. Use a static nested class if you don't require this access.



Inner classes are used to get functionality that can get an object better than method.
They can be used in the case when a set of multiple operations are required and chances of reusability are good inside the class and they will not be accessed but methods outside the outer class.
Inner classes are made to achieve multiple inheritances also.
Inner classes are used when they are useful in the class context.
They are used to separate logic inside classes.
inner class means one class which is a member of another class. There are basically four types of inner classes in java.

1) Nested Inner class

2) Method Local inner classes

3) Anonymous inner classes

4) Static nested classes

7. Spring boot - Dependency injection types? 

There are basically three types of dependency injection:
constructor injection: the dependencies are provided through a class constructor.
setter injection: the client exposes a setter method that the injector uses to inject the dependency.
interface injection: the dependency provides an injector method that will inject the dependency into any client passed to it. Clients must implement an interface that exposes a setter method that accepts the dependency.
So now its the dependency injection’s responsibility to:

  • Create the objects
  • Know which classes require those objects
  • And provide them all those objects
Reference : https://java2blog.com/introduction-to-spring-framework/#Spring_Boot
8 . Mutable(Changable) Vs Immutable(Not Changable) ? 

Mutable object – You can change the states and fields after the object is created

Sting is im-mutable , Stringbuffer is mutable and Date is mutable  .


How to create a Mutable class?
To create a mutable class in Java you have to make sure the following requirements are satisfied:

  • Provide a method to modify the field values
  • Getter and Setter method

How to create an Immutable class?
A class should be declared as final so that it can’t be extended.
All the fields should be made private so that direct access is not allowed
No setter methods
Make all mutable fields final, so that they can be assigned only once.

9.


Read more


JBOSS start and stop shell scripts - domain mode - host controller - domain controller

 JBOSS start and stop shell scripts - domain mode - host controller - domain controller
===========START DOMAIN CONTROLLER MACHINE JBOSS=================
echo "Setting JBOSS Env"
export JAVA_HOME=/opt/app/java/jdk/jdk160
export PATH=$PATH:/opt/app/jboss/jboss-eap-6.3/bin:$JAVA_HOME/bin
echo "PATH is : $PATH"

echo "JBOSS Environment is ready and starting jboss in domain mode"
echo "MY:JAVA_OPTS: $JAVA_OPTS"
export JAVA_OPTS="$JAVA_OPTS -DASOCPropsFile=/abc/etc/abc.props -DLNSPropsFile=/pns/etc/pns.props -DAVSPropsFile=/bvs/etc/bvs.props -DEOTPropsFile=/toe/etc/toe.props -DLPSPropsFile=/psl/etc/psl.props -DADLPropsFile=/hfl/etc/hfl.props -Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump=true"
echo "MY AFTER:JAVA_OPTS: $JAVA_OPTS"

/export/opt/jboss/jboss-eap-6.3/bin/domain.sh -b cldv0015.abc.cpf.com -bmanagement cldv0015.abc.cpf.com &
=============================================
============START HOST CONTROLLER==========
=============================================

==========STOP JBOSS Process harshly==============
echo "Stopping JBOSS JAVA processes"

for i in `ps -eaf | grep jboss | grep java | cut -d " " -f6`
{
        kill -9 $i
    echo "Killing: $i"
}
ps -eaf | grep jboss | grep java
echo "Stop completed"
============================================

Read more


GIT Eclipse usage basic

GIT- Basics & Eclipse Usage
This post aim at very basic usage of using GIT based tools and eclipse configurations for the same. 

  • create a new repository : create a new directory, open it and perform a
    • git init
to create a new git repository.

  • checkout a repository : Create a working copy of a local repository by running the command 
    • git clone /path/to/repository 
when using a remote server, your command will be
git clone username@host:/path/to/repository

  • Goto project dir 
1. git init
2. git status
3.git add .
4. git status
5. git commit -m "first commit"
6. Create an empty repo in GITHUB
7.copy clone url
8.git remote add origin << URL >>
9.git push -u origin master
10. Check your files are there on GIT remote

To add more / update files

  • git add . 
  • git status 
  • git commit -m "second commit"
  • git push -u origin master

Read more


Unix command to find class in jar | windows command to search class in jars

Below are the sample commands to find out required class is there in the jar files of current directory and its sub-dirs.

Unix : 

find . -type f -name '*.jar' -print0 |  xargs -0 -I '{}' sh -c 'jar tf {} | grep com/sun/net/ssl/HostnameVerifier.class &&  echo {}'
find . -type f -name '*.jar' -print0 |  xargs -0 -I '{}' sh -c 'jar tf {} | grep HostnameVerifier.class &&  echo {}'
Ex :
[kg6378@bldv0014 jdk1.6.0_20]$ find . -type f -name '*.jar' -print0 |  xargs -0 -I '{}' sh -c 'jar tf {} | grep com/sun/net/ssl/HostnameVerifier.class &&  echo {}'
com/sun/net/ssl/HostnameVerifier.class
./jre/lib/jsse.jar

[kg6378@bldv0014 jdk1.6.0_20]$ find . -type f -name '*.jar' -print0 |  xargs -0 -I '{}' sh -c 'jar tf {} | grep HostnameVerifier.class &&  echo {}'
com/sun/deploy/security/CertificateHostnameVerifier.class
./jre/lib/deploy.jar
javax/net/ssl/HostnameVerifier.class
javax/net/ssl/HttpsURLConnection$DefaultHostnameVerifier.class
sun/net/www/protocol/https/DefaultHostnameVerifier.class
com/sun/net/ssl/HostnameVerifier.class
./jre/lib/jsse.jar

Windows : 


forfiles /S /M *.jar /C "cmd /c jar -tvf @file | findstr /C:"HostnameVerifier.class" && echo @path"


C:\Users\kg6378\Desktop\jdk-7u79-windows-x64\tools>cd C:\Krish\MyDocs\SWS\Jdk1.6
.0_14

C:\Krish\MyDocs\SWS\Jdk1.6.0_14>forfiles /S /M *.jar /C "cmd /c jar -tvf @file |
 findstr /C:"HostnameVerifier.class" && echo @path"

  3517 Thu May 21 09:19:20 EDT 2009 com/sun/deploy/security/CertificateHostnameV
erifier.class
"C:\Krish\MyDocs\SWS\Jdk1.6.0_14\jre\lib\deploy.jar"
   194 Thu Feb 05 13:54:20 EST 2009 javax/net/ssl/HostnameVerifier.class
   618 Thu Feb 05 13:54:22 EST 2009 javax/net/ssl/HttpsURLConnection$DefaultHost
nameVerifier.class
   384 Thu Feb 05 13:54:22 EST 2009 sun/net/www/protocol/https/DefaultHostnameVe
rifier.class
   272 Thu Feb 05 13:54:32 EST 2009 com/sun/net/ssl/HostnameVerifier.class
"C:\Krish\MyDocs\SWS\Jdk1.6.0_14\jre\lib\jsse.jar"

C:\Krish\MyDocs\SWS\Jdk1.6.0_14>





Read more

Popular Posts

Enter your email address:

Buffs ...

Tags


Powered by WidgetsForFree