在JMH应用程序中使用Spring Beans

时间:2015-12-16 16:11:10

标签: java spring jmh

我正在尝试在简单的JMH应用程序(http://openjdk.java.net/projects/code-tools/jmh/)中使用spring上下文,但是在运行benchmarks.jar时,即使它在本地工作,它似乎也无法读取spring schema文档。

这是一个基础maven原型设置的标准jmh项目。为简单起见:

package org.sample;

import org.openjdk.jmh.annotations.Benchmark;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BenchmarkTest {

public BenchmarkTest() {
    ApplicationContext context = new ClassPathXmlApplicationContext("springContext.xml");
}

@Benchmark
public void benchmarkSomething() {
    // BENCHMARKS
}

public static void main(String [] args) {
    BenchmarkTest benchmarkTest = new BenchmarkTest();
    benchmarkTest.benchmarkSomething();
 }
}

// Spring config

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- spring beans -->
</beans>

从main运行时,有java -jar target / benchmarks.jar 没有错误,但在构建了jmh uber jar并运行

之后
  

java -jar target / benchmarks.jar

我收到以下错误

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 8 in XML document from class path resource [springContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 8; columnNumber: 80; cvc-elt.1: Cannot find the declaration of element 'beans'.

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

想出来:

将此添加到我的maven阴影jar插件中修复它。 H / T http://www.eorlovsky.com/2010/03/maven-shade-spring-core-handlers_27.html

<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>