Spring console application configured using annotations

后端 未结 7 1866
谎友^
谎友^ 2021-01-30 13:33

I want to create spring console application (running from command line with maven for example: mvn exec:java -Dexec.mainClass=\"package.MainClass\").

Is this application

相关标签:
7条回答
  • 2021-01-30 14:13

    I'd to figure this out for a project recently. I was building a CLI for a utility that would be run from a scheduled job and reused part of the web application code for the project. I had a problem bootstrapping all of the @Autowired dependencies, and I didn't actually need them all, so I bootstrapped the specific dependencies in the main class using the AnnotationConfigApplicationContext register(java.lang.Class...) method as follows:

    @Component
    public class SpringAppCLI
    {
    
        /**
         * Service to be autowired!
         */
        @Autowired
        private SampleService sampleService;
    
        /**
         *
         */
        public static void main(String[] args) throws Exception {
    
            final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
    
            // setup configuration
            applicationContext.register(SampleServiceConfig.class);
            applicationContext.register(SampleServiceRepository.class);
            applicationContext.register(JpaConfig.class);
            applicationContext.register(CommandLineConfig.class);
            applicationContext.register(SampleService.class);
            applicationContext.register(SpringAppCLI.class);
            // add CLI property source
            applicationContext.getEnvironment().getPropertySources()
                    .addLast(new SimpleCommandLinePropertySource(args));
    
            // setup all the dependencies (refresh) and make them run (start)
            applicationContext.refresh();
            applicationContext.start();
    
            try {
                SpringAppCLI springAppCLI = applicationContext.getBean(SpringAppCLI.class);
                springAppCLI.doWhatever();
            } catch (Exception e) {
                //some handling
    
            } finally {
                applicationContext.close();
            }
        }
    }
    

    and here's the configuration class:

    @Configuration
    @ComponentScan(basePackageClasses = SolrLoadCLI.class, includeFilters = @Filter(Controller.class), useDefaultFilters = false)
    class CommandLineConfig implements ApplicationContextAware {
    
        /**
         * 
         */
        private ApplicationContext applicationContext;
    
        /**
         * 
         */
        @Override
        public void setApplicationContext(ApplicationContext applicationContext)
                throws BeansException {
            this.applicationContext = applicationContext;
        }
    
        /**
         * 
         * @return
         */
        @Bean
        public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
            PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
            Resource[] resourceArray = new Resource[2];
            resourceArray[0] = new ClassPathResource("/SampleService.properties");
            resourceArray[1] = new ClassPathResource("/Database.properties");
            ppc.setLocations(resourceArray);
            return ppc;
        }
    }
    
    0 讨论(0)
提交回复
热议问题