在自定义jar依赖项上加载spring API

时间:2016-07-02 10:58:14

标签: spring maven

我对Spring Web应用程序中jar的使用感到困惑(jar中的war Web应用程序中包含了pom.xml

我有一个Maven父项目,其中包含war网络应用和jar应用。

在我的战争网络应用中,我在jar中添加了pom.xml个应用。

父:

<modules>
    <module>serv.app</module>
    <module>serv.batch</module>
</modules>

网络应用:

<parent>
    <groupId>com.hw</groupId>
    <artifactId>HwParent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>serv.app</artifactId>

<packaging>war</packaging>

    <dependency>
        <groupId>com.hw</groupId>
        <artifactId>serv.batch</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

Jar app:

<parent>
    <groupId>com.hw</groupId>
    <artifactId>HwParent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>serv.batch</artifactId>

<properties>
    <spring.version>4.3.0.RELEASE</spring.version>
    <ehcache.version>2.10.2.2.21</ehcache.version>
</properties>

<dependencies>

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

    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>${ehcache.version}</version>
    </dependency>

</dependencies>

我的问题在这里解释(:

在我的jar应用中,我想加载ClassPathXmlApplicationContext

/serv.batch-0.0.1-SNAPSHOT/src/main/webapp/WEB-INF/applicationContext.xml
像这样:

public void batchApp() {
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:**/WEB-INF/applicationContext.xml");

    BatchModel bModel = (BatchModel) context.getBean("batchModel");

    System.out.println(bModel.getBatchValue());
}

我的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="batchModel" class="com.hw.batch.model.BatchModel">
        <property name="batchValue" value="10" />
    </bean>

</beans>

最后,当我想通过我的Web应用程序中的控制器调用此方法时,此方法总是返回错误,因为他找不到我的batchModel

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'batchModel' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:701)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1180)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076)
at com.hw.batch.app.BatchApp.batchApp(BatchApp.java:13)
at com.hw.serv.app.controllers.TestController.max(TestController.java:53)

我哪里错了?有人可以帮帮我吗? :)

非常感谢!

0 个答案:

没有答案