我正在尝试在任务管理上构建一个简单的CRUD操作。
Web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>SampleApplication</display-name>
<welcome-file-list>
<welcome-file>html/homepage.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
弹簧servlet.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:property-placeholder location="classpath:/config.properties"/>
<!-- Spring will search in the bellow paths controller an services annotations-->
<context:component-scan base-package="org.itc" />
<mvc:resources mapping="/html/**" location="/html/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:annotation-driven />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/task" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
</beans>
我得到以下错误。
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [config.properties] cannot be opened because it does not exist
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:151)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:265)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:162)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:609)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4842)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5303)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.FileNotFoundException: class path resource [config.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:143)
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98)
at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:175)
at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:156)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:142)
... 16 more
缺少config.properties文件是什么意思?我刚开始尝试配置Spring MVC。
答案 0 :(得分:1)
您已定义
<context:property-placeholder location="classpath:/config.properties"/>
也就是说,您希望应用程序从应用程序类路径中找到的文件config.properties
加载属性。
错误表示找不到此文件。
在不知道您的确切布局,构建链或如何启动应用程序的情况下,我无法确切地说明您应该将此文件放在何处以便将其提供给应用程序。也许在某个地方/src/main/resources/
。
如果您不需要此文件或外部化属性,则可以停止告知应用程序您执行的操作:从spring-servlet.xml
删除其定义。
答案 1 :(得分:1)
如果您的应用程序中没有属性文件,请从Spring-servlet.xml文件中删除此行:
<context:property-placeholder location="classpath:/config.properties"/>
您告诉Spring要搜索一个名为 config.properties 的属性文件,该文件不存在。
如果您需要属性文件,请创建 config.properties 并将其放入类路径中。
答案 2 :(得分:1)
config.properties用于保存应用程序变量(属性)。我发现它非常有用,因为:
这些是我使用的功能,可能会更多。实际上我找不到有关config.properties的任何描述。所有这些东西都在Spring手册的不同地方提到过。
<强>更新强>
尝试Spring Boot - 它包含Spring MVC,Hibernate,我为初学者找到了很多很好的例子。它是为减少初始配置工作而构建的。
答案 3 :(得分:0)
确保您拥有config.properties并将该文件放在/ WEB-INF / classes文件夹中并使用classpath:/config.properties。
答案 4 :(得分:0)
如果要使用属性文件,则必须在Spring-servlet.xml文件中声明bean org.springframework.beans.factory.config.PropertyPlaceholderConfigurer。
您的Spring-servlet.xml:
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:config.properties</value>
</property>
</bean>
您的数据源bean将如下所示。
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="db.url.property" />
<property name="username" value="db.user" />
<property name="password" value="db.password" />
</bean>
您的类路径中应该包含config.properties,其中包含以下属性。
您的config.properties
db.url.property=jdbc:mysql://localhost:3306/task
db.user=root
db.password=root