2016-08-18 19 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> 

春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ファイルには、コードを変更せずにアプリケーションを設定することです。それは春とは関係がありません。 dbconnection文字列、ユーザー名、パスワードなど – 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

あなたは、アプリケーションがアプリケーションのクラスパスで見つかったファイルconfig.propertiesからプロパティをロードする、すなわち

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

を定義しています。

このエラーは、このファイルが見つからないことを意味します。

正確なレイアウト、ビルドチェーン、またはアプリケーションの起動方法がわからないと、このファイルをアプリケーションで使用できるようにする場所を正確に指定することはできません。おそらく/src/main/resources/のようなものでしょう。

このファイルまたは外部化されたプロパティが一般的に必要でない場合は、アプリケーションに指示することをやめることができます。定義をspring-servlet.xmlから削除します。

+0

そのconfig.propertiesファイルは必要ありません。しかし、私は同じ例外を取得します。 – Protagonist

+0

他の方法で、ロードするために空のファイルを与えます。 – OrangeDog

1

あなたは、プロパティは、アプリケーションでファイルを持って春-servlet.xmlファイルからこの行を削除しない場合:

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

あなたはconfig.propertiesという名前のファイルのプロパティを検索するためのバネを言っていますは存在しません。

プロパティファイルが必要な場合は、config.propertiesを作成し、クラスパスに配置します。

+0

Spring-servlet.xmlファイルから削除しましたが、同じ例外が発生しました – Protagonist

+1

本当ですか?私はあなたのXMLファイルを自分のワークスペースにコピーして、* context:property *行を追加しました。エラーが出ますが、削除すると期待どおりに動作します。サーバーを再起動しましたか?きれいにしてプロジェクトを構築しますか? – amicoderozer

+0

今、その作業、ありがとう – Protagonist

1

アプリケーション変数(プロパティ)を保存するために使用されるconfig.properties。上述したよう

  • このファイルにいくつかの初期のアプリケーションcofigデータを格納することができ、DB接続などの :私はそれは非常に便利なので、見つけます。
  • 独自のプロパティを作成し、その値をSpringアプリケーション内に挿入することができます。また、このプロパティはアプリケーション実行コマンド(コマンドライン引数)でプリセットすることができます
  • 内部でconfig.propertiesを使用してアプリケーションをコンパイルできますが、デプロイ時にはコンパイル済みのアプリケーションの外にconfig.propertiesを作成し、Springはコンパイルされずファイル。
  • プロファイルをプリセットすることができます。各Beanにプロファイルが設定され、アプリケーションの実行モードに応じて注入されるため、開発、テスト、デプロイメントの段階で役立ちます。

これらは私が使用する機能であり、はるかに多くの可能性があります。実際にはconfig.propertiesについての説明は見つけられません。これらすべてのものはSpringマニュアルの別の場所で言及されています。

更新

春ブーツを試してみてください - それは春のMVC、Hibernateと私は初心者のための多くの良い例を見つけました含まれています。初期の設定作業を減らすためにビルドされました。

0

プロパティファイルを使用する場合は、Spring-servlet.xmlファイルにorg.springframework.beans.factory.config.PropertyPlaceholderConfigurerというBeanを宣言する必要があります。

あなたの春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のようなすべてのリソースファイルを含む)という2つのソースフォルダを持っています。 mkyong exampleのソースコードは、mkyong.com/spring/spring-propertyplaceholderconfigurer-exampleファイルで確認できます。 – Manzar

関連する問題