Should i never use 'new' keyword on a spri

2020-08-03 07:08发布

问题:

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 ?

回答1:

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.



回答2:

There is no rule of using or not using new

It's upto you if you want your object to be managed by Spring or you want want to take care of it on your own.

Spring eases object creation, dependency management and autowiring however if you want none in your class you can instantiate it using new



回答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



回答4:

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



回答5:

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.