Should i never use 'new' keyword on a spring boot project?

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 ?


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.

Why you should never use the word should, He stopped me. I don't like should he said. It implies judgment. “Why would I insist that people know how to do certain things by certain ages? Each essay should have exactly five paragraphs. Don’t begin a sentence with “and” or “because.” Never include personal opinion. Never use “I” in essays. We get these ideas primarily from teachers and other students. Often these ideas are derived from good advice but have been turned into unnecessarily strict rules in our minds.


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

One Word You Should Never Use In a Conversation, One Word You Should Never Use In a Conversation. Ellen Gutoskey. 1/3/2020. a group of people sitting at a table using a laptop computer © fizkes/iStock via  Credit cards don’t age like fine wine. Sometimes your oldest ones have the fewest perks and benefits, so you stop using them. Is it a problem to have a credit card you never use?


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

27 Words You Should Never Use to Describe Yourself, Do you write things about yourself you would never actually have the nerve to say? Here are some words that are great when other people use  The Three Words You Should Never Use in a Work Email—And What to Say Instead By Laura Belgray October 18, 2018


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.

This Is the 1 Phrase You Should Never Use With a Customer (or , When things get heated, don't make it personal, and never apologize for the way someone else feels. 10 Times You Should Never Use Antibacterial Wipes Kimberly Holland Updated: Mar. 31, 2020 Antibacterial wipes are a time-crunched cleaner's best friend—but here are ten things you should never


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

27 Words You Should Never Use to Describe Yourself, Do you write things about yourself you would never actually have the nerve to say? Here are some words that are great when other people use  2 Reasons Why You Should Never Use A Rototiller In Your Garden As crazy as it sounds, if you want a garden with less maintenance, less weeds and better yields – stop using a rototiller! Tilling, especially over-tilling can all but destroy important soil structure!


12 words and phrases you should never use, words and phrases you should never use. Some of these are verbal fillers, and others can be downright rude. Cut them from your vocabulary. While every person has to decide how to best use their credit card based on individual needs, not using your credit card at all can have some consequences. And being aware of exactly what happens when you never use your credit card can help you decide what to do with a card that gets infrequent usage.


"DATE!" Is a Four-Letter Word: and Why You Should Never Use it , DATE! Is a Four-Letter Word: and Why You Should Never Use it [Steven Provenzano] on Amazon.com. *FREE* shipping on qualifying offers. It's time to stop  6 Places You Should Never Use a Debit Card All of that said, you should never even think about using a debit card in any of these situations: When buying airline tickets – Lots of things can go wrong when flying the “friendly” skies — up to and including your airline going bust.


"DATE!" Is a Four-Letter Word: and Why You Should , "DATE!" Is a Four-Letter Word: and Why You Should Never Use it - Kindle edition by Steven Provenzano. Download it once and read it on your Kindle device, PC  Using a home equity line to pay for a vacation or to fund leisure and entertainment activities is an indicator that you're spending beyond your means. Although it's cheaper than paying with a