依赖jar中类的Spring autowire

时间:2015-08-03 22:12:16

标签: java spring hibernate spring-mvc

我有一个spring-hibernate功能,作为一个单独的jar(Collection.jar)构建,供多个webapps使用。这个collection.jar有一个spring-config.xml,其中定义了bean。当这个jar包含在webapp(spring-application1)中时,collection.jar中定义的类不会自动连接。

来自Collection.jar的

spring-config.xml

<context:annotation-config/>

<context:component-scan base-package="com.antk.pkg" />

<import resource="spring-datasource.xml" />

<bean id="delegator" class="com.antk.pkg.Delegator">
    <property name="dbUtils" ref="dbUtils" />
</bean>


<bean id="dbUtils" class="com.antk.pkg.util.DBUtils">
    <property name="dao" ref="collectorDao" />
</bean>

<bean id="collector" class="com.antk.pkg.Collector">
    <property name="collectorService" ref="collectorServiceImpl" />
</bean>

<bean id="collectorServiceImpl" class="com.antk.pkg.service.CollectorService">
    <property name="collectorDao" ref="collectorDaoImpl" />
    <property name="delegator" ref="delegator" />
</bean>
...

并且在spring-application1中,我将上述内容包括在内:

<bean id="com.antk.pkg.collector.spring"
class="org.springframework.context.support.ClassPathXmlApplicationContext" abstract="false"
    scope="prototype" lazy-init="default" autowire="default">
    <constructor-arg>
        <list>
            <value>spring-config.xml</value>
        </list>
    </constructor-arg>
</bean>

但是当我调用collector时,collectorservice一直被设置为null。它没有得到自动装配。任何想法。

1 个答案:

答案 0 :(得分:0)

假设你的spring-config.xml存在于你的类路径中 将以下内容放在您的web.xml中 -
1)指定要加载的弹簧配置文件

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-config.xml</param-value>
</context-param>

2)定义ContextLoaderListener: -

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

从jar加载spring-config.xml文件时也存在问题。 “如果类路径资源驻留在文件系统中,资源实现支持解析为java.io.File,但不支持驻留在jar中但尚未扩展的类路径资源(由servlet引擎或任何环境) is)到文件系统。http://docs.spring.io/spring/docs/current/spring-framework-reference/html/resources.html

从jar加载config.xml文件时,避免依赖类路径资源加载器。

相关问题