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

发布于 2024-11-15 07:32:10 字数 678 浏览 2 评论 0原文

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

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

applicationContext.xml 条目如下,

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

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

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

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

谢谢,阿比

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 entry as follows,

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

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

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

Thanks,Abi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝影如岚 2024-11-22 07:32:10

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

项目 A(使用 STS 创建的标准 Maven 项目)在 src/main/resources 中有 applicationContext.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 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 添加为依赖项:

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

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

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

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:

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

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

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 first, then run Start in project B.

何以心动 2024-11-22 07:32:10

您真的需要该位置的 classpath*: 前缀吗? (这个 * 合法吗?)我本来期望的更像是:

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

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

ApplicationContext context = new ClassPathXmlApplicationContext("**/applicationContext*.xml);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文