Spring console application configured using annotations

后端 未结 7 2001
谎友^
谎友^ 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:09

    You can do it this way :

    • Do the initialization in your main method
    • You can then use the start method as your sudo controller
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.stereotype.Component;
    
    import com.org.service.YourService;
    
    @Component
    public class YourApp{
    
        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext(
                    "ApplicationContext.xml");
    
            YourApp p = context.getBean(App.class);
            p.start(args);
        }
    
        @Autowired
        YourService yourService;
        private void start(String[] args) {
    
            yourService.yourMethod();
    
        }
    
    }
    

提交回复
热议问题