首页 > 解决方案 > 为什么没有使用@autowired 和@component 类创建对象?

问题描述

//主类

@SpringBootApplication
public class SpringBootTest{
    

    public static void main(String[] args){
        SpringApplication.run(SpringBootTest.class,args);
        A a=new A();
        a.method();

        B b=new B();
        b.method1();

    }

}

AmqpTemplate 的包是 import org.springframework.amqp.core.AmqpTemplate;

@Component
public class A{
    
    @Autowired
    AmqpTemplate obj;

    public void method(){
        System.out.println(obj);
    }

} 

//B类

@Component
public class B{
    
    @Autowired
    A obj1;

    public void method1(){
        System.out.println(obj1);
    }

} 

为什么B类中的Obj1和B类中的obj在执行代码后为空?任何帮助将不胜感激。

标签: javaspring-bootautowired

解决方案


推荐阅读