首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在码头上转换普通java主程序

如何在码头上转换普通java主程序
EN

Stack Overflow用户
提问于 2019-08-21 08:48:29
回答 1查看 109关注 0票数 0
  • 我想使用Jenkins将旧的java代码迁移/运行到docker
代码语言:javascript
复制
1. It is structured to run using normal main method of java (Jar file having main method is executed through some script). 
2. Its making use of spring.xml(applicationContext.xml) files with spring-context-2.5.xsd 
3. Uses properties file for all configurations. 

  • 我现正就以下事项寻求建议:
代码语言:javascript
复制
1. Does this project needs to be migrated to spring-boot application for migrating to/creating docker image?
2. If yes, please have a look at current code block
3. Do I need to replace properties files by yml files?

  • 当前的主类代码可以被定义为:
代码语言:javascript
复制
public class SIIRunner {   

    public static void main(String[] args){   

        String envStr = null;
        if (args != null && args.length > 0) {
            envStr = args[0];
        }

        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        SIIExecutor siiExecutor= (SIIExecutor) ctx.getBean("SIIExecutor");
        siiExecutor.pollAndOperate();
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-21 09:40:15

代码语言:javascript
复制
@SpringBootApplication
public class Application {
    public static void main(String[] args) throws Exception {
        ApplicationContext app = SpringApplication.run(Application.class, 
         args);//init the context
        SIIExecutor siiExecutor = (SIIExecutor) 
         app.getBean(SIIExecutor.class);//get the bean by type
    }
    @Bean // this method is the equivalent of the <bean/> tag in xml
    public SIIExecutor getBean(){
         return new SIIExecutor();
    }
}

只要您开始使用@Configuration基类(听起来可能像使用@SpringBootApplication ),就可以使用@ImportResource注释来包含XML文件。

代码语言:javascript
复制
@SpringBootApplication
@ImportResource("classpath:beanFileName.xml")
public class SpringConfiguration {
  //
}

Spring引导的理想概念是避免xml文件。但是,如果您想保留xml,只需添加@ImportResource("classPath:beanFileName.xml")即可。

我建议删除beanFileName.xml文件。并将此文件转换为基于spring注释的bean。因此,任何类都是作为bean创建的。只需在类名之前编写@Service@Component注释即可。例如:

基于XML的:

代码语言:javascript
复制
<bean ID="id name" class="com.example.MyBean">

基于注释的:

代码语言:javascript
复制
@Service or @Component
class MyBean {
}

再加上@ComponentScan("Give the package name")

这是最好的办法。希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57587804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档