0

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?

Elydasian
  • 2,016
  • 5
  • 23
  • 41
mohneesh
  • 1
  • 4
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Jens May 14 '18 at 07:53

3 Answers3

1

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.

Sauhard Sharma
  • 405
  • 1
  • 3
  • 13
0

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

PrasadW
  • 44
  • 7
0

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

Batman
  • 282
  • 3
  • 12