Spring context from within a jar file

前端 未结 4 1704
無奈伤痛
無奈伤痛 2021-02-05 17:01

I\'m developing a desktop application in java using spring and hibernate. I want to package it as a executable jar but I\'m having problems loading the context configuration XML

4条回答
  •  感动是毒
    2021-02-05 17:37

    I suppose you want to run your application as java -jar myapp.jar

    In this case, use class ClassPathXmlApplicationContext within your class with the method main.

    public static void main( String[] args ) {
        String config[] = { "spring-beans.xml" };
        ApplicationContext ctx = new ClassPathXmlApplicationContext(config);
        DataSource ds = (DataSource) ctx.getBean("dataSource", DataSource.class);
    }
    

    It's a terrible idea to try to implement your own ApplicationContext. That's too much work and too much room for errors.

提交回复
热议问题