gpt4 book ai didi

java - 在 JMH 应用程序中使用 Spring Bean

转载 作者:太空宇宙 更新时间:2023-11-04 13:07:10 24 4
gpt4 key购买 nike

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

这是一个从基本 Maven 原型(prototype)建立的标准 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配置

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

有什么建议吗?

最佳答案

弄清楚了:

将此添加到我的 Maven 着色 jar 插件中修复了它。电话 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>

关于java - 在 JMH 应用程序中使用 Spring Bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34316830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com