加载类路径中 jar 内的 spring 应用程序上下文文件 [英] Loading spring application context files that are inside a jar in classpath

查看:20
本文介绍了加载类路径中 jar 内的 spring 应用程序上下文文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

applicationContext.xml 条目如下,

applicationContext.xml entry as follows,

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

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

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

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

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

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

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

谢谢,阿比

推荐答案

这应该可行,我刚刚创建了 2 个项目并进行了检查.

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

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

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

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:

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

项目 B:

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

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

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

项目 B 中的 Start.java:

Start.java in project B:

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,然后在项目 B 中运行 Start.

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

这篇关于加载类路径中 jar 内的 spring 应用程序上下文文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆