加载类路径中 jar 内的 spring 应用程序上下文文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6303242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:32:36  来源:igfitidea点击:

Loading spring application context files that are inside a jar in classpath

springdependency-injectionclasspath

提问by Abhishek

I am trying to use ClassPathXmlApplicationContext in my java standalone code to load applicationContext.xml that is inside a jar file which is in my class path.

我试图在我的 java 独立代码中使用 ClassPathXmlApplicationContext 来加载位于我的类路径中的 jar 文件中的 applicationContext.xml。

ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:**/applicationContext*.xml");

applicationContext.xml entry as follows,

applicationContext.xml 条目如下,

 <bean id="myAdder" class="com.foo.bar.MyAdder">
        <property name="floatAdder" ref="floatAdder"/>        
    </bean>

And, when I try to load a bean that way I am getting NoSuchBeanException. Can't a bean by loaded in this way?

而且,当我尝试以这种方式加载 bean 时,我收到了 NoSuchBeanException。不能以这种方式加载 bean 吗?

The jar file is added to my classpath as a maven dependency. When I see the Java Build Path in Eclipse for this project, I see this jar linked as M2_REPO/.../..

jar 文件作为 maven 依赖项添加到我的类路径中。当我在 Eclipse 中看到这个项目的 Java Build Path 时,我看到这个 jar 链接为 M2_REPO/.../..

I was assuming I can load the bean inside the jar file as the jar is in classpath this way. Am I missing something?

我假设我可以在 jar 文件中加载 bean,因为 jar 以这种方式在类路径中。我错过了什么吗?

Thanks,Abi

谢谢,阿比

回答by abalogh

This should work, I just created the 2 projects and checked.

这应该有效,我刚刚创建了 2 个项目并进行了检查。

Project A (standard Maven project created with STS) has applicationContext.xmlin src/main/resources.

项目 A(使用 STS 创建的标准 Maven 项目)applicationContext.xml在 src/main/resources 中。

pom.xml:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.D</groupId>
<artifactId>A</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>        
    <spring.version>3.0.5.RELEASE</spring.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
</dependencies>
</project>

applicationContext.xml:

应用上下文.xml:

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

<bean id="myAdder" class="com.foo.bar.MyAdder">
    <property name="foo" value="bar" />
</bean>

</beans>

Project B:

项目乙:

pom.xml: same as A, except A is added as dependency:

pom.xml:与A相同,除了A作为依赖添加:

<dependency>
   <groupId>org.D</groupId>
   <artifactId>A</artifactId>
   <version>0.0.1-SNAPSHOT</version>
</dependency>

Start.java in project B:

项目 B 中的 Start.java:

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext(
            "classpath*:**/applicationContext*.xml");

    MyAdder myAdder = (MyAdder) context.getBean("myAdder");
    System.out.println(myAdder.getFoo());
}

mvn install A first, then run Start in project B.

首先 mvn install A,然后在项目 B 中运行 Start。

回答by Darien

Do you really need the classpath*:prefix on that location? (Is that *legal?) I would have expected something more like:

你真的需要classpath*:那个位置的前缀吗?(这*合法吗?)我会期待更像:

ApplicationContext context = new ClassPathXmlApplicationContext("**/applicationContext*.xml);