2016-08-18 176 views
0

我試圖在任務管理上構建一個簡單的CRUD操作。什麼是Spring中的config.properties,它的用途是什麼

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

爲spring-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

config.properties文件是在不改變代碼配置應用程序。它與春天無關。如db連接字符串,用戶名,密碼等 – Jens

+0

https://www.mkyong.com/spring/spring-propertyplaceholderconfigurer-example/ –

+0

@Jens那麼爲什麼我要獲得FileNotFoundexception?我試着創建屬性文件,並在spring-servlet文件中提到它。但是得到相同的錯誤。 – Protagonist

回答

0

確保您有config.properties文件放在/ WEB-INF/classes文件夾中,並使用classpath:/config.properties。

1

您已經定義

<context:property-placeholder location="classpath:/config.properties"/> 

也就是說,你想要的應用程序從應用的類路徑中找到的文件config.properties加載性能。

該錯誤表示無法找到該文件。

不知道你的確切佈局,構建鏈或你如何啓動應用程序我不能確切地說你應該把這個文件放在哪裏才能讓它可用於應用程序。也許像/src/main/resources/某處。

如果您不需要此文件或通常的外部屬性,您可以停止告訴應用程序您執行以下操作:從spring-servlet.xml中刪除其定義。

+0

我不需要該config.properties文件,我也刪除它。但即時獲得相同的例外。 – Protagonist

+0

換個方式,給它一個空文件來加載。 – OrangeDog

1

如果你沒有的屬性在你的應用程序文件,從Spring-servlet.xml文件刪除此行:

<context:property-placeholder location="classpath:/config.properties"/> 

你告訴春天來搜索屬性文件名爲config.properties哪些不存在。

如果您需要一個屬性文件,然後創建config.properties並將其放入您的類路徑中。

+0

我把它從Spring-servlet.xml文件中刪除了,仍然是我得到相同的異常 – Protagonist

+1

你確定嗎?我複製你的XML文件在我的工作區,並添加*上下文:屬性*行我得到的錯誤,但是當我刪除,它按預期工作。你重新啓動了服務器嗎?清理並建立該項目? – amicoderozer

+0

現在它的工作,謝謝 – Protagonist

1

config.properties用於保存應用程序變量(屬性)。我發現它非常有用,因爲:

  • 該文件提到可以 存儲一些初次申請cofig數據,如數據庫連接。
  • 您可以創建自己的屬性並在Spring應用程序中注入它們的值。此屬性可以使用應用程序運行命令(命令行參數)預設
  • 您可以使用config.properties在內部編譯應用程序,但在部署時在編譯的應用程序之外創建config.properties,Spring將使用它的屬性而不是編譯文件。
  • 您可以預設配置文件,它們將幫助開發,測試和部署階段,因爲每個bean都可以使用配置文件進行標記,並根據您的應用程序運行模式進行注入。

這些是我使用的功能,可能更多。其實我沒有找到關於config.properties的任何描述。所有這些東西都在Spring手冊的不同地方提到過。

更新

嘗試春季啓動 - 它包含了Spring MVC的,Hibernate和我發現對於初學者許多很好的例子。它的構建是爲了減少初始配置工作。

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 
+0

我應該在哪裏放置config.properties文件? – Protagonist

+0

我總是習慣遵循我的java項目中的約定,以擁有兩個源文件夾「src/main/java」(包含所有java文件)和「src/main/resources」(包含所有資源文件,如config.properties)。您可以查看mkyong示例mkyong.com/spring/spring-propertyplaceholderconfigurer-examp le的源代碼。 – Manzar

相關問題