验证码: 看不清楚,换一张 查询 注册会员,免验证
  • {{ basic.site_slogan }}
  • 打开微信扫一扫,
    您还可以在这里找到我们哟

    关注我们

Spring导出可以运行的jar包问题如何解决

阅读:902 来源:乙速云 作者:代码code

Spring导出可以运行的jar包问题如何解决

最近需要解决Maven项目导入可执行的jar包的问题,如果项目不包含Spring,那么使用mvn assembly:assembly即可

可是如果包含Spring,那么这么方法就不可行,报错:

Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace

我在网上折腾了两天,这是assembly的一个bug。

据说原因是spring的多个jar包中都含有spring.handlers和spring.schemas文件,而assembly只会把第一次遇到的文件打入jar包,后面遇到的都会skip掉。

解决方法就是放弃assembly,使用shade插件来打包.在shade的打包配制中指明spring.handlers和spring.schemas文件会以append方式加入进来,从而确保其他spring的jar中的这两个文件的信息不会被遗漏。

下面是一个非常简单的例子,只有四个文件的Maven工程,代码再附件内:

1、pom.xml


    4.0.0
    com.javaee
    main-spring
    0.0.1-SNAPSHOT
    
        
            org.springframework
            spring-context
            3.0.6.RELEASE
        
    
    
        
            
                org.apache.maven.plugins
                maven-shade-plugin
                1.7

                
                    
                        package
                        
                            shade
                        
                        
                            my-spring-app
                            true
                            jar-with-dependencies
                            
                                
                                    com.test.Main
                                
                                
                                    META-INF/spring.handlers
                                
                                
                                    META-INF/spring.schemas
                                
                                
                                    META-INF/spring.tooling
                                
                            

                        
                    
                
            
        
    

2、applicationContext.xml(Spring的配置文件)



    
    

3、代码事例

package com.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {
    public String findUser() {
        return "find liqiu";
    }
}
package com.exec;

import org.springframework.context.support.GenericXmlApplicationContext;

import com.service.UserService;

public class Main {

    public static void main(String[] args) throws InterruptedException {
        GenericXmlApplicationContext context = new GenericXmlApplicationContext();
        context.setValidating(false);
        context.load("classpath:applicationContext.xml");
        context.refresh();
        UserService userService = context.getBean(UserService.class);
        while (true) {
            System.out.println(userService.findUser());
            Thread.sleep(10000);
        }
    }
}

在命令行运行:mvn package

然后在target目录会产出一个jar包:my-spring-app.jar

运行即可:java -jar target/my-spring-app.jar

五月 16, 2015 10:46:52 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
五月 16, 2015 10:46:53 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericXmlApplicationContext@4a5e88f7: startup date [Sat May 16 22:46:53 CST 2015]; root of context hierarchy
五月 16, 2015 10:46:53 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@18fd54ec: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,userService]; root of factory hierarchy
find liqiu
find liqiu

当然也可以这么运行:java -classpath target/my-spring-app.jar com.exec.Main

分享到:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: hlamps#outlook.com (#换成@)。
相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>