Null Pointer exception while initializing Object using "new" keyword in springboot based microservcies - spring

What happens when I build an object using new keyword in service using springboot framework?
Does the Object created using "new" keyword is built out of the container?

No, the object created out of a "new" keyword doesn't come out of the container. There are two disadvantages to this approach. The first being that it renders the Spring framework a bit useless. Secondly, if there are #Autowired or Spring managed beans inside your "new" bean, they won't get injected. Once you do a "new", spring leaves all the subsequent hierarchical dependency injection to you.

in Spring, objects that are created using new keyword, created outside of the container. So you wouldn't get benefits like life cycle management, Security etc

You should always use Spring dependency injection to create Beans (using the #Autowired annotation is the recommended way to do this) otherwise you're losing the benefits of using the Spring framework. I would suggest reading this documentation on Beans and Dependency Injection if you haven't already

Related

Is Spring Boot Autowired for object creation better than manual object creation?

Which below case will give me better performance
Using Spring Boot Autowired annotation for creating an object
Or
Using new key for creating an object.
I understand with spring annotations will have a choice of using lot more inbuilt features.
Kindly help me to understand
You need to learn an Inversion of Control pattern first of all: https://en.wikipedia.org/wiki/Inversion_of_control
Then you need to make yourself familiar with Spring Framework as an implementation of it: https://spring.io/projects/spring-framework
In two words answering your question: the Spring way with its dependency injection is much better by performance at runtime, then manual new managing. Just because Spring instantiates objects only once, at start up (mostly) and then provide you those ready objects. With new you have to manage then yourself and in most cases you lose with GC and memory (de)allocation.

Where spring container will create all run time objects

As per Spring documentation it says that spring IOC container manages entire life cycle for beans.
My question is where the Spring container will create a new object? in JVM or where? Also how references of objects will be maintained.
Also below questions,
For standalone application where those bean will be created?
For WebApplication where those bean will be created?
I have gone through major of the Spring doc's but haven't found any clear idea on how object references are maintained.
Since a Spring Boot application runs in a JVM, the objects are created there as well.
Spring keeps references to the beans in the application context. There are several concrete implementations of the ApplicationContext interface (depending on the type of the application), but in the end it comes down to a bunch of HashMaps, which hold the bean instances or information about how to create them, bean types etc. The most relevant class, if you're interested in implementation details, is IMO org.springframework.beans.factory.support.DefaultListableBeanFactory. This is used by all application context implementations to register beans, resolve them etc.

ClassBridge called by MassIndexer and access to spring service

I wonder whether it is possible to use any spring services from within hibernate search ClassBridge.
Abstract:
My entity does not have all information i'd like to put into indexed document. I can not get it by #IndexedEmbeded too. This data comes from external data source, and I have service to provide this. This is only needed when reindexing. When indexing single object save service provide this information in transient entity field.
For reindex I use MassIndexer.
My application is working in Spring MVC environment. I use annotation driven configuration. So to access my service I need only use #Autowired annotation and service is ready to be used.
Solution?
When using Hibernate Search life is not so easy (or I have no idea how it could be).
To get additional information I decided to use ClassBridge. I also implement simple MassIndexer procedure called from within my spring service.
In my ClassBridge spring does not autowire service. When I try do quick workaround and use static field in ClassBridge and pass service reference from MassIndexer caller the other problem occurred. Service reports exception "no session" (sessionFactory.getCurrentSession() throws exception)... I have no idea where to go further.
What is recommended way to access Spring service from within ClassBridge code?
How can I get active hibernate Session (or SessionFactory) from within ClassBridge?
Thanx for you time & hope your help.
The recommended way is through compile time weaving and #Configurable
A comment on this page (http://guylabs.ch/2014/02/22/autowiring-pring-beans-in-hibernate-jpa-entity-listeners/) provides a much simpler way of handling this that might be useful. You can have Spring autowire an object for you, more explicitly by calling:
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
Doc: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/context/support/SpringBeanAutowiringSupport.html

in Spring, what is the best way to dynamically create different objects of a certain class, each object being able to access a Spring bean?

Say I have a file with each line depicting a different command (but of the same kind), which I want to read out and check and run and maybe do other operations such as merging, comparing and most importantly, store the commands into database.
To do that, I create the Command Class, and new a new Command object while reading each line of the file. Now the problem is, a Command object need to make use of, say a Spring bean which provides database access. As a result, I have to pass in that bean as a constructor argument of the Command class, which is very ugly, which doesn't seem to be the "Spring way"...
and I don't want to use ApplicationContextAware to make my class coupled to the Spring context.
Is there a best practice for this situation?
I very new to Spring and I know it might be a dumb question ...
I would create a CommandFactory that is coupled with spring and use that in your consumer instead. If the factory implements an interface you are not coupling yourself in the consumer and you don't close your possibilities of using a different -non spring coupled- one at a later point (e.g. testing).
In this case I think it is the best way to make the classes created by new Spring Beans.
Therefor annotate them with #Configurable, enable AspectJ, and read the Spring Reference Chapter 7.8.1 Using AspectJ to dependency inject domain objects with Spring

Tomcat Spring WebApplicationContext

In the Spring framework reference I found this:
The ApplicationContext interface has a few other methods for retrieving beans, but ideally your application code should never use them. Indeed, your application code should have no calls to the getBean method at all, and thus no dependency on Spring APIs at all.
but I have no idea to realize this.
After a lot of search I found this
avoid to use getBean in your application code, try to write a single class with one or more getBean() calls, this bootstrap class must be executed at the startup of your webapplication
is this a more recommended way?
Spring encourages you to use Inversion of Control, where the framework (e.g. Spring) injects dependencies into your objects, rather than your objects fetching things from the framework.

Resources