Java 如何在springboot中使用@autowired实现简单的“可故障”主控?

Java 如何在springboot中使用@autowired实现简单的“可故障”主控?,java,spring-boot,main,Java,Spring Boot,Main,我想用SpringBoot做一个简单的main,并自动连接一些字段 我希望应用程序失败错误代码!=如果进程中引发了某些异常,则返回0 例如: @SpringBootApplication public class SqlInserterMain { @Autowired private static JdbcTemplate jdbcTemplate; public static void main(String[] args) { Configurab

我想用SpringBoot做一个简单的main,并自动连接一些字段

我希望应用程序失败错误代码!=如果进程中引发了某些异常,则返回0

例如:

@SpringBootApplication
public class SqlInserterMain
{
    @Autowired
    private static JdbcTemplate jdbcTemplate;

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SqlInserterMain.class, args);

        insertData();

        context.close();
    }

    private static void insertData()
    {
        // Do something with jdbcTemplate.
        // If jdbcTemplate fails on exception, the app should fail and return some error code.
        System.out.println("YOYO" + jdbcTemplate);
    }
}
但是,jdbcTemplate为null

如果我添加一个@component或ApplicationRunner,@autowire可以工作,但我不会让main失败

你知道如何用一些自动连接的字段来实现一个快速简单的main,这些字段在异常情况下会失败吗?谢谢。

我不认为SqlInserterMain是Spring管理的bean,它不是Spring实例化的,是吗?。只要像yAzou提到的那样创建一个额外的bean,或者从Spring上下文中手动提取JdbcTemplate。

我不认为SqlInserterMain是Spring管理的bean,它不是Spring实例化的,是吗?。只需像yAzou提到的那样创建一个额外的bean,或者从Spring上下文中手动提取JdbcTemplate。

我想您需要这样做

您可以使用ApplicationListener和自动连接JdbcTemplate或执行任务所需的bean

@Component
@Order(0)
class MyApplicationListener 
    implements ApplicationListener<ApplicationReadyEvent> {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        System.out.println("YOYO" + jdbcTemplate);
    }
}
我想你想

您可以使用ApplicationListener和自动连接JdbcTemplate或执行任务所需的bean

@Component
@Order(0)
class MyApplicationListener 
    implements ApplicationListener<ApplicationReadyEvent> {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        System.out.println("YOYO" + jdbcTemplate);
    }
}

最后,我手动连接bean,捕获异常,打印异常,然后退出close或System.exit1:

我希望SpringBoot能够像普通的java主机一样工作

@SpringBootApplication
public class SqlInserterMain
{
    private static JdbcTemplate jdbcTemplate;

    public static void main(String[] args)
    {
        ConfigurableApplicationContext context = SpringApplication.run(SqlInserterMain.class, args);

        try
        {
            jdbcTemplate = context.getBean(JdbcTemplate.class);

            insertData();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.exit(1);
        }

        context.close();
    }

    private static void insertData()
    {
        // Do something with jdbcTemplate.
        // If jdbcTemplate fails on exception, the app should fail and return some error code.
        System.out.println("YOYO" + jdbcTemplate);
    }
}

最后,我手动连接bean,捕获异常,打印异常,然后退出close或System.exit1:

我希望SpringBoot能够像普通的java主机一样工作

@SpringBootApplication
public class SqlInserterMain
{
    private static JdbcTemplate jdbcTemplate;

    public static void main(String[] args)
    {
        ConfigurableApplicationContext context = SpringApplication.run(SqlInserterMain.class, args);

        try
        {
            jdbcTemplate = context.getBean(JdbcTemplate.class);

            insertData();
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.exit(1);
        }

        context.close();
    }

    private static void insertData()
    {
        // Do something with jdbcTemplate.
        // If jdbcTemplate fails on exception, the app should fail and return some error code.
        System.out.println("YOYO" + jdbcTemplate);
    }
}


我认为最好的方法是创建一个具有所需依赖项的简单Bean…为什么要将静态添加到JdbcTemplate?我添加静态是因为我想从main访问它,main也是静态的。请参见类似的问题@user7294900-当@PostConstruct方法中引发异常时,它也会使main失败吗?我认为最好的方法是创建一个具有所需依赖项的简单Bean…为什么要将静态添加到JdbcTemplate?我添加静态是因为我想从main访问它,它也是静态的。请参阅类似的问题@user7294900-当@PostConstruct方法中引发异常时,它也会失败吗?我最终完成了jdbcTemplate=context.getBeanJdbcTemplate.class;但是我想知道是否有一种方法可以使用@Autowired来实现这一点。有一种方法可以使用@Autowired来实现这一点:将它放在另一个类中并使其非静态。@Taschi-如何实例化另一个类?您没有。用@Component注释它,Spring会为您实例化它。@Taschi-无论@Autowired是什么,代码逻辑中都会抛出异常。如果代码抛出异常,我希望整个spring应用程序失败;但是我想知道是否有一种方法可以使用@Autowired来实现这一点。有一种方法可以使用@Autowired来实现这一点:将它放在另一个类中并使其非静态。@Taschi-如何实例化另一个类?您没有。用@Component注释它,Spring会为您实例化它。@Taschi-无论@Autowired是什么,代码逻辑中都会抛出异常。如果我的代码抛出异常,我希望整个spring应用程序失败。如果ApplicationListener中抛出异常,整个应用程序会停止并返回错误代码吗?@AlikElzin kilaka如果你抛出异常,我测试了这个,它部分工作。当抛出异常时,应用程序确实停止了,但错误代码为0:您指的是什么错误代码?进程退出代码?这个解决方案肯定是最干净的!是的,应用程序进程返回退出代码0,即使发生了异常且未被捕获为“我的代码”。我希望spring启动失败,退出代码为1,就像普通java main一样。如果ApplicationListener中抛出异常,整个应用程序是否会停止并返回错误代码?@AlikElzin kilaka如果抛出异常,我测试了这个,它部分工作。当抛出异常时,应用程序确实停止了,但错误代码为0:您指的是什么错误代码?进程退出代码?这个解决方案肯定是最干净的!是的,应用程序进程返回退出代码0,即使发生了异常且未被捕获为“我的代码”。我希望spring启动失败,退出代码为1,就像普通的java main一样。