Springboot的对象常见问题


package com.example.demo2;

import com.example.demo2.user.TestUser;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Demo2ApplicationTests {

    @Autowired
    private TestUser testUser;

    @Test
    void contextLoads() {
    }

    @Test
    void test(){
//        TestUser testUser = new TestUser();
        testUser.testUser();
    }
}

这里使用AutoWired方法注入TestUser对象,就没问题。
但是如果使用new创建的话就空指针异常了,为啥?

TestUser里是不是注入有其他bean? 如果注入有其他bean,就不能用new的方式创建TestUser,new的方式不能完成依赖注入..