Spring JUnit ClassPathResource FileNotFoundException
Spring JUnit ClassPathResource FileNotFoundException
我正在 Spring 迈出第一步。在我的项目中,我需要使用 SOAP HTTPS 网络服务。我添加了一个 JKSKeyManager
bean 来测试带有证书的配置 xml 和 ClassPathResource
bean。但是当我 运行 JUnit 测试时我得到
java.io.FileNotFoundException: class path resource [src/main/resources/cacerts] cannot be resolved to URL because it does not exist
但文件肯定在该目录中。
下面是我的配置 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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">`
<bean id="cucmsql" class="com.myname.myproject.CUCMDatabaseConnector" factory-method="getInstance">
<property name="marshaller" ref="marshaller"></property>
<property name="unmarshaller" ref="marshaller"></property>
<property name="properties" ref="properties"/>
</bean>
<bean id="properties" class="com.myname.myproject.SystemProperties" factory-method="getInstance">
</bean>
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="com.cisco.axl" />
</bean>
<bean id="cacertsFile" class="org.springframework.core.io.ClassPathResource">
<constructor-arg value="src/main/resources/cacerts"/>
</bean>
<util:property-path id="cacertsUtil" path="cacertsFile.file"/>`
<bean id="keyManager" class="org.springframework.security.saml.key.JKSKeyManager">
<constructor-arg ref="cacertsUtil"></constructor-arg>
<constructor-arg>
<map>
<entry key="cucmpublisher" value="changeit"></entry>
<entry key="cucmsubcriber" value="changeit"></entry>
</map>
</constructor-arg>
<constructor-arg type="java.lang.String" value="changeit"></constructor-arg>
</bean>
</beans>
我做错了什么?
根据您的项目结构判断,您可能正在使用 Maven,这意味着 src/main/resources
是一个源文件夹。
因此,您需要将配置更改为:
<bean id="cacertsFile" class="org.springframework.core.io.ClassPathResource">
<constructor-arg value="/cacerts" />
</bean>
类路径资源的路径不是相对于项目根目录,而是相对于项目源文件夹。
我正在 Spring 迈出第一步。在我的项目中,我需要使用 SOAP HTTPS 网络服务。我添加了一个 JKSKeyManager
bean 来测试带有证书的配置 xml 和 ClassPathResource
bean。但是当我 运行 JUnit 测试时我得到
java.io.FileNotFoundException: class path resource [src/main/resources/cacerts] cannot be resolved to URL because it does not exist
但文件肯定在该目录中。
下面是我的配置 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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">`
<bean id="cucmsql" class="com.myname.myproject.CUCMDatabaseConnector" factory-method="getInstance">
<property name="marshaller" ref="marshaller"></property>
<property name="unmarshaller" ref="marshaller"></property>
<property name="properties" ref="properties"/>
</bean>
<bean id="properties" class="com.myname.myproject.SystemProperties" factory-method="getInstance">
</bean>
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="com.cisco.axl" />
</bean>
<bean id="cacertsFile" class="org.springframework.core.io.ClassPathResource">
<constructor-arg value="src/main/resources/cacerts"/>
</bean>
<util:property-path id="cacertsUtil" path="cacertsFile.file"/>`
<bean id="keyManager" class="org.springframework.security.saml.key.JKSKeyManager">
<constructor-arg ref="cacertsUtil"></constructor-arg>
<constructor-arg>
<map>
<entry key="cucmpublisher" value="changeit"></entry>
<entry key="cucmsubcriber" value="changeit"></entry>
</map>
</constructor-arg>
<constructor-arg type="java.lang.String" value="changeit"></constructor-arg>
</bean>
</beans>
我做错了什么?
根据您的项目结构判断,您可能正在使用 Maven,这意味着 src/main/resources
是一个源文件夹。
因此,您需要将配置更改为:
<bean id="cacertsFile" class="org.springframework.core.io.ClassPathResource">
<constructor-arg value="/cacerts" />
</bean>
类路径资源的路径不是相对于项目根目录,而是相对于项目源文件夹。