简体   繁体   English

在JMH应用程序中使用Spring Bean

[英]Using Spring Beans in a JMH Application

I'm trying to use a spring context in a simple JMH application ( http://openjdk.java.net/projects/code-tools/jmh/ ), but when running the benchmarks.jar it seems to have trouble reading the spring schema document even though though it works locally. 我正在尝试在一个简单的JMH应用程序( http://openjdk.java.net/projects/code-tools/jmh/ )中使用spring上下文,但是在运行Benchmarks.jar时似乎无法读取spring模式文档,即使它在本地也可以。

It's a standard jmh project set up from the base maven archetype. 这是从基本Maven原型建立的标准jmh项目。 For simplicity sake: 为了简单起见:

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 // Spring配置

<?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>

When running from main there are java -jar target/benchmarks.jar no errors, but after building the jmh uber jar and running 从main运行时,没有java -jar target / benchmarks.jar没有错误,但是在构建了jmh uber jar并运行之后

java -jar target/benchmarks.jar java -jar target / benchmarks.jar

I get the following error 我收到以下错误

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'.

Any suggestions? 有什么建议么?

Figured it out: 弄清楚了:

Adding this to my maven shaded jar plugin fixed it. 将其添加到我的Maven阴影jar插件中修复了它。 H/T http://www.eorlovsky.com/2010/03/maven-shade-spring-core-handlers_27.html 电话: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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM