Understanding Spring Autowired: Preventing NullPointerException Compared to Manual Instantiation

The article explains how Spring's automatic dependency injection initializes beans to avoid NullPointerExceptions, contrasting it with manual object creation using new, and provides example code demonstrating proper @Autowired usage versus manual instantiation.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Understanding Spring Autowired: Preventing NullPointerException Compared to Manual Instantiation

The root cause is that when Spring manages beans it automatically initializes the properties that will be used later, whereas creating objects with new requires manual initialization of dependent instances; otherwise a NullPointerException occurs.

Example 1 shows a typical Spring setup where TestService is injected with @Autowired, so Spring automatically injects TestDao into TestService:

@RestController
@RequestMapping(value = "/test")
public class TestController {
    @Autowired
    private TestService testService;

    @RequestMapping(value = "/print", method = RequestMethod.GET)
    public void test() {
        testService.test();
    }
}

@Service
public class TestService {
    @Autowired
    private TestDao testDao;

    public void test() {
        testDao.test();
    }
}

If TestService is instantiated with new, Spring will not inject TestDao, leaving it null and causing a NullPointerException. In this case you must manually create a TestDao instance, as shown in Example 2.

Example 2 demonstrates manual instantiation:

@RestController
@RequestMapping(value = "/test")
public class TestController {
    private TestService testService = new TestService();

    @RequestMapping(value = "/print", method = RequestMethod.GET)
    public void test() {
        testService.test();
    }
}

@Service
public class TestService {
    @Autowired
    private TestDao testDao;

    public void test() {
        TestDao testDao = new TestDao();
        testDao.test();
    }
}

Summary: At application startup, Spring loads and initializes components according to a defined loading chain. For instance, if component A injects B and B injects C, calling B from A will work only when Spring automatically injects the dependencies; using new bypasses this mechanism and leads to NullPointerExceptions because the nested dependencies are not initialized.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendJavaspringdependency-injectionAutowirednullpointerexception
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.