2014-09-28 6 views
1

Я столкнулся с одной неприятной ситуацией и не смог ее решить. Я написал приложение SpringMVC + Hibernate с Intellij IDEA. Я хочу поместить все мои свойства jdbc (url подключения, имя пользователя и т. Д.) В jdbc.properties, но IDEA не может обнаружить их в этом файле. Вот мой WEB-INF/диспетчер-servlet.xml:Intellij IDEA не обнаруживает свойства из файла свойств

<?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:p="http://www.springframework.org/schema/p" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" 
     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 
     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 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 

    <mvc:annotation-driven/> 
    <context:component-scan base-package="com.controllers"/> 

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
     <property name="prefix" value="/WEB-INF/jsp/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 

    <bean id="fileCreator" class="com.utilites.FileCreator"/> 

    <tx:annotation-driven transaction-manager="transactionManager"/> 

    <bean id="dataSource" 
      class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
      p:driverClassName="${jdbc.driverClassName}" 
      p:url="${jdbc.url}" 
      p:username="${jdbc.username}" 
      p:password="${jdbc.password}"/> 

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="configLocation"> 
      <value>classpath:hibernate.cfg.xml</value> 
     </property> 
     <!--<property name="configurationClass">--> 
     <!--<value>org.hibernate.cfg.AnnotationConfiguration</value>--> 
     <!--</property>--> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
       <prop key="hibernate.connection.charSet">UTF-8</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="transactionManager" 
      class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 
</beans> 

и мой jdbc.properties содержит:

jdbc.driverClassName=com.mysql.jdbc.Driver 
jdbc.dialect=org.hibernate.dialect.MySQLDialect 
jdbc.url=jdbc:mysql://localhost:3306/Overhaul 
jdbc.username=root 
jdbc.password= 

и MySQL-зависимость в pom.xml:

<dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.32</version> 
     </dependency> 

и мой фрагмент структуры проекта: enter image description here

Когда я строю проект, nd развернуть его, Tomcat показывает мне эту ошибку:

PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [${jdbc.driverClassName}]; 

Что не так в моем приложении или где-то в esle?

+1

Я уверен, что это не проблема IDEA IntelliJ в –

+0

ли вы импортировать драйвер в проект либо из файла или в pom.xml? и если да, пожалуйста, укажите соответствующий код/​​файл. – ThMBc

+1

Вы объявили PropertyPlaceholderConfigurer в файле конфигурации боба –

ответ

1

Держите файл .properties под resources и добавить это выше вашего bean определения:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations" value="classpath:jdbc.properties"/> 
</bean> 
+0

спасибо большое !!!! –

Смежные вопросы