Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用@Autowired时,何时必须创建新实例_Java_Spring_Intellij Idea_Autowired - Fatal编程技术网

Java 使用@Autowired时,何时必须创建新实例

Java 使用@Autowired时,何时必须创建新实例,java,spring,intellij-idea,autowired,Java,Spring,Intellij Idea,Autowired,直到今天,我才100%确定在将类定义为bean时不必创建新的类实例。 今天我有点困惑 我会尝试用文字解释其中的一部分,因为我认为上传所有代码会让人难以理解 我使用intellij和Spring创建了新的REST项目。 我创建了一个新类来映射它,并向它添加了@RestController。 在这个类中,我添加了我自己创建的另一个类的属性,并向其中添加了@Autowired。 我从未创建过这个类的新实例,但我添加了一个bean配置。 到目前为止,一切都很顺利 我想添加ThreadPoolTaskSc

直到今天,我才100%确定在将类定义为bean时不必创建新的类实例。 今天我有点困惑

我会尝试用文字解释其中的一部分,因为我认为上传所有代码会让人难以理解

我使用intellij和Spring创建了新的REST项目。 我创建了一个新类来映射它,并向它添加了
@RestController
。 在这个类中,我添加了我自己创建的另一个类的属性,并向其中添加了
@Autowired
。 我从未创建过这个类的新实例,但我添加了一个bean配置。 到目前为止,一切都很顺利

我想添加
ThreadPoolTaskScheduler
逻辑,所以我打开了新类,添加了新属性
ThreadPoolTaskScheduler
,并用
@Autowired
标记它

我为它添加了一个Bean:

 @Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler(){
    ThreadPoolTaskScheduler threadPoolTaskScheduler
            = new ThreadPoolTaskScheduler();
    threadPoolTaskScheduler.setPoolSize(5);
    threadPoolTaskScheduler.setThreadNamePrefix(
            "ThreadPoolTaskScheduler");
    return threadPoolTaskScheduler;
}
现在在主类中,如果我不发送新的类实例,if将抛出null异常

所以这个代码是有效的:

public static void main(String[] args) {
    SpringApplication.run(RestApiApplication.class, args);
    TaskScheduler taskScheduler = new TaskScheduler(new ThreadPoolTaskScheduler());
    taskScheduler.setTaskScheduler();
}
该代码不是:

public static void main(String[] args) {
    SpringApplication.run(RestApiApplication.class, args);
    TaskScheduler taskScheduler = new TaskScheduler();
    taskScheduler.setTaskScheduler();
}
这是TaskScheduler类:

    @Controller
public class TaskScheduler {
    @Autowired
    ThreadPoolTaskScheduler threadPoolTaskScheduler;
    TaskScheduler(){}
    TaskScheduler(ThreadPoolTaskScheduler threadPoolTaskScheduler){
        this.threadPoolTaskScheduler = threadPoolTaskScheduler;
    }

    public void setTaskScheduler(){
        threadPoolTaskScheduler.schedule(
                new ScheduledTask(),
                new Date());
    }
}
  • 我想不出setTaskScheduler上threadPoolTaskScheduler为NULL的原因,知道吗
  • 如果我也将TaskScheduler定义为一个bean,那么它可以正常工作,为什么我必须这么做?春天能处理一切还是什么都不能

  • 如果您想让我添加更多代码,请告诉我。

    您可以获得如下bean对象:

    public class RestApiApplication{
    
        public static void main(String[] args) {
            ConfigurableApplicationContext context = SpringApplication.run(RestApiApplication.class, args);
            TaskScheduler ts = context.getBean(TaskScheduler.class);
            ts.setTaskScheduler()
        }
    }
    
    但在实际项目中使用
    ApplicationContext
    就像这个例子一样是个坏主意

    如果需要REST,应该使用@RestController注释。
    选中此项

    当您实例化一个bean本身时,它不再对其进行管理,因此所有的
    @Autowired
    依赖项都不会被处理。为了确保正确注入依赖项,需要通过Spring上下文实例化bean。Spring需要实例化整个bean跟踪,以便正确地执行依赖项注入。请在此链接中查看国际奥委会的详细信息:

    查看您的代码,有两种方法可以做到这一点

    使用弹簧靴 具有main方法的类将如下所示:

    @SpringBootApplication
    public class RestApiApplication implements CommandLineRunner {
    
        @Autowired
        private TaskScheduler ts;
    
        public static void main(String[] args) {
            SpringApplication.run(RestApiApplication.class, args);
        }
    
        @Override
        public void run(String... args) throws Exception {
            ts.<your-code>
        }
    
    @springboot应用程序
    公共类RestApiApplication实现CommandLineRunner{
    @自动连线
    专用任务调度器;
    公共静态void main(字符串[]args){
    run(RestApiApplication.class,args);
    }
    @凌驾
    公共无效运行(字符串…参数)引发异常{
    ts。
    }
    
    }


    另一种方法是使用ApplicationContext直接获取bean实例,如果需要执行一些集成/单元测试,这种方法可以很好地工作

    在对象上使用
    new
    时,将其从Spring控件中排除<代码>TaskScheduler TaskScheduler=新建TaskScheduler()-此对象不受spring控制,由您实例化,因此spring在这种情况下不会解决任何依赖关系。但我不想将spring用于TaskScheduler我想将其用于ThreadPoolTaskScheduler从main方法调用它只是为了测试,问题是,当新实例被定义为bean@user1913615您不需要传递新实例,只需将该bean自动连接到控制器中。控制器bean也将由spring创建。所有bean都存储在Spring IoC容器中。但是@Autowired for ThreadPoolTaskScheduler位于TaskScheduler类上,但是您可以使用
    new
    TaskScheduler TaskScheduler=new TaskScheduler()创建TaskScheduler它不正确…这是我的问题,为什么它不正确,它的一部分不能自动连接,另一部分不能?我在创建新的TaskScheduler时没有问题,我在创建新的ThreadPoolTaskScheduler时遇到问题使用Spring Boot创建包含bean的任务调度器后,
    ThreadPoolTaskScheduler
    将自动创建并连接。为什么?spring是什么都有还是什么都没有?是的,所有的bean都应该得到管理,以便IoC注入能够正常工作。请查找下面的链接,了解此操作背后的技术细节。请在答案中添加最后一条评论