Home » Posts filed under tutorials
The best way to map @ManyToMany
June 29, 2021 Ravi Yasas
@ManyToMany
Student entity
Task entity
@manytomany / basics / beginners / hibernate / java / jpa / manytomany / mapping / tutorials
How to avoid NullPointerException
June 29, 2021 Ravi Yasas
java.lang.NullPointerException
When can it be thrown?
- Calling the instance method of a null object.
- Accessing or modifying the field of a null object.
- Taking the length of null as if it were an array.
- Accessing or modifying the slots of null as if it were an array.
- Throwing null as if it were a Throwable value.
How to avoid it?
- Use Optional in Java 8
- Call equals() and equalsIgnoreCase() methods on known objects.
- Use valueOf() over toString()
- Use null-safe methods and libraries. Ex: Apache Commons StringUtils.
- Use @NotNull and @Nullable annotations
- Always check for nulls.
basics / beginners / exceptions / java / java8 / nullpointer / optional / toString() / tutorials / value)f()
States of an object in Hibernate
June 26, 2021 Ravi Yasas
Hibernate object states
There are three states- Transient
- If the object just created, no primary key and not associate with a session, then the object is in the transient state
- Persistent
- If a session is open, the object is in the persistent state
- Detached
- If a session is closed, the object is in the detached state
Streams vs Collections in Java
June 26, 2021 Ravi Yasas
Streams vs Collections
Collections | Streams |
The collections are used to store & group data in data structures like List, Set, Map...etc. | Streams are used to perform complex operations like filtering, matching, mapping, etc... on stored data |
Data modification can be done. You can add or remove data from a Collection | Data cannot be modified |
External iterations need to be done | Internal iterations are being used |
Collections can be traversed | Streams can be traversed only once. If you need another traverse, you need to create a new stream |
Collections are eagerly constructed | Streams are lazily constructed |
beginners / collection framework / collections / java 8 / learning / stream / streams / tutorials
Bean scopes in Spring Framework
June 26, 2021 Ravi Yasas
Spring Bean scopes
Set up the scope
- The @Scope annotation can be used
- There are 6 scopes are available in the application context
singleton
- This is the default scope.
- The same object is returned every time.
- Better to use for all stateless beans.
prototype
- A new object is created every time.
- Better to use for all stateful beans.
request
- Here it will create a single instance and it will available in the complete lifecycle of an HTTP request
- Only valid in ApplicationContext
session
- Here it will create a single instance and it will available in the complete lifecycle of an HTTP session
- Only valid in ApplicationContext
application
- Here it will create a single instance and it will available in the complete lifecycle of the servlet context
- Only valid in ApplicationContext
websocket
- Here it will create a single instance and it will available in the complete lifecycle of the web socket
- Only valid in ApplicationContext
bean scope / beans / beginners / learning / prototype / request / scope / singleton / spring / spring boot / spring framework / tutorials
Dependency injection in Spring Boot
June 26, 2021 Ravi Yasas
Dependency injection
What is Dependency injection?
- Dependency injection is a form of the IoC container
- It is just passing dependencies to other objects or frameworks.
- Dependency injection allows you to remove hardcoded dependencies and make applications loosely coupled, extendable and maintainable.
- In Spring it uses two types of dependency injection methods, constructor and setter injection. In addition, we can use field injection as well.
Constructor injection
- If there is a single construction, @Autowired annotation is optional after Spring 4.3. But if there is more than one constructor, we need to specify the constructor by mentioning it using @Autowired annotation.
- Autowired field should be final
- This method is immutable and reliable
Setter injection
- More flexible
- Objects are mutable
- Null checks are required
- Less secure than constructor injection, because dependencies can be overridden
Field injection
- Very easy to use, but not recommended.
- If you use this, the unit tests can be failed.
When to use Setter or Constructor injections
- Constructor injection is better than Setter injection.
- Use Setter injection when a number of dependencies are more or you need readability.
- Use Constructor Injection when Object must be created with all of its dependencies.
- We can change a value easily when using the setter injection. It is more flexible than constructor injection.
- Setter injection will override the constructor injection. If we use both setter and constructor injection, the IOC container will use setter injection.
Why does constructor injection better than others?
- All required dependencies are available at the initialization time.
- This is very important in writing unit tests. Because it forces to load all objects for all dependencies.
Advantages of dependency injection
- Easy unit testing
- Configurable components
- Separations of concerns
- More control
@Autowired / beginners / constructor / dependency injection / di / injection / ioc / ioc container / learning / setter / spring / spring boot / spring framework / tutorials
jar vs war in Spring Boot
June 25, 2021 Ravi Yasas
Use jar not war
- If you run mvn clean install in Spring Boot, you will get a fat jar file.
- It contains everything like dependencies, embedded web server... etc.
- You can just run java -jar jarFile in a JVM, the default is Tomcat embedded server or you can use Jetty.
- You don't need any web server or application server to run spring boot jars.
- Because spring boot will provide us a production-ready jar file with the embedded tomcat.
- If you need to run spring boot applications in existing web servers, you may have to create war instead of a jar.
- Spring Boot supports only Tomcat, Jetty, and Undertow as the embedded server
beginners / embedded server / jar / java / learning / spring boot / springboot / tomcat / tutorials / war