2

I'm working on Spring Boot Rest API, and I did end up using the new keyword here and there.

I'm wondering, did I do something wrong when I used the new keyword for my program. And if it is absolutely forbidden to use new keyword on a real project.

If the answer is yes should i annotate each class i wrote with @component annotation so i can instantiate an object using @autowired.

If the answer is no when can we break that rule ?

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
Ayoub k
  • 7,788
  • 9
  • 34
  • 57

5 Answers5

9

You can create objects using the new keyword in a spring application.

But these objects would be outside the scope of the Spring Application Context and hence are not spring managed.

Since these are not spring managed, any nested levels of dependency (such as your Service class having a reference to your Repository class etc) will not be resolved.

So if you try to invoke a method in your service class, you might end up getting a NullPointer for the repository.

@Service
public class GreetingService {
    @Autowired
    private GreetingRepository greetingRepository;
    public String greet(String userid) {
       return greetingRepository.greet(userid); 
    }
}

@RestController
public class GreetingController {
    @Autowired
    private GreetingService greetingService;
    @RequestMapping("/greeting")
    public String greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        return String.format("Hello %s", greetingService.greet(name));
    }
    @RequestMapping("/greeting2")
    public String greeting2(@RequestParam(value = "name", defaultValue = "World") String name) {
      GreetingService newGreetingService = new GreetingService();
      return String.format("Hello %s", newGreetingService.greet(name));
    }
}

In the above example /greeting will work but /greeting2 will fail because the nested dependencies are not resolved.

So if you want your object to be spring managed, then you have to Autowire them. Generally speaking, for view layer pojos and custom bean configurations, you will use the new keyword.

Saikat
  • 548
  • 1
  • 4
  • 12
5

There is no rule for using or not using new. It's up to you if you want Spring to manage your objects or want to take care of them on your own. Spring eases object creation, dependency management, and auto wiring; however, you can instantiate it using new if you don't want that.

Amit Phaltankar
  • 3,341
  • 2
  • 20
  • 37
3

I think its fine to use new keyword, but you should learn the difference between different stereotype (Controller, Service, Repository)

You can follow this question to get some clarity:

What's the difference between @Component, @Repository & @Service annotations in Spring?

Using appropriate annotation will allow you to correctly use DI (dependency injection), that will help in writing sliced tests for your spring boot application. Also the Service,Controller and Repository components are created as Singleton, so lesser GC overhead. Moreover components that you create using new keyword are not managed by Spring, and by default Spring will never inject dependencies in a object created using new.

Spring official documentation: https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-spring-beans-and-dependency-injection.html

Munish Chandel
  • 3,572
  • 3
  • 24
  • 35
1

You will need new on Spring mock tests when you will have to create an object as service and inject mock object as dao.

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
1

Look at the following code; here as you see, based on a condition it's necessary to dynamically load advertisements on demand. so here you can not @autowire this group of items because all the information are loaded from DB or an external system, so you just need to fill you model accordingly.

if (customer.getType() == CustomerType.INTERNET) {
    List < Advertisement > adList = new ArrayList < Advertisement > ();
    for (Product product: internetProductList) {
        Advertisement advertisement = new Advertisement();
        advertisement.setProduct(product);
        adList.add(advertisement);
    }
}

Note it's appropriate to use Spring for managing external dependencies like plugging a JDBC connection into a DAO or configurations like specifying which database type to use.