Postgres驱动程序加载与heroku上的maven

时间:2014-02-17 19:03:06

标签: java spring postgresql maven heroku

我正在尝试使用spring jdbc连接连接到postgres数据库。但是,当应用程序部署到tomcat时,它似乎无法找到postgres驱动程序。我可以在intellij中查看该类,它在我的外部依赖项文件夹中列出,但是抛出了ClassNotFoundException。

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
   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-3.0.xsd
   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

<context:component-scan base-package="com.planit.mvc.*"/>

<mvc:resources mapping="/resources/**" location="/resources/"/>
<!--https://github.com/priyatam/springmvc-bootstrap-showcase/tree/master/src/main/webapp  base resources off this.-->
<mvc:annotation-driven/>

<mvc:view-controller path="/" view-name="hello"/>


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

<!-- DB CONNECTION -->
<bean class="java.net.URI" id="dbUrl">
    <constructor-arg value="#{T(com.ProjectUtils).getDBUrl()}"/>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="#{ 'jdbc:postgresql://' + @dbUrl.getHost() + ':' + @dbUrl.getPort() + @dbUrl.getPath() }"/>
    <property name="username" value="#{ @dbUrl.getUserInfo().split(':')[0] }"/>
    <property name="password" value="#{ @dbUrl.getUserInfo().split(':')[1] }"/>
</bean>




<util:properties id="socialAuthProperties" location="/WEB-INF/oauth_consumer.properties"></util:properties>

<bean id="socialAuthConfig" class="org.brickred.socialauth.SocialAuthConfig">
    <property name="applicationProperties">
        <ref bean="socialAuthProperties"/>
    </property>
</bean>

<bean id="socialAuthManager" class="org.brickred.socialauth.SocialAuthManager" scope="session">
    <property name="socialAuthConfig">
        <ref bean="socialAuthConfig"/>
    </property>
    <aop:scoped-proxy/>
</bean>

<bean id="socialAuthTemplate" class="org.brickred.socialauth.spring.bean.SocialAuthTemplate" scope="session">
    <aop:scoped-proxy/>
</bean>


<bean id="socialAuthWebController" class="org.brickred.socialauth.spring.controller.SocialAuthWebController">
    <constructor-arg value="#{T(com.ProjectUtils).getBaseUrl()}"/>
    <constructor-arg value="authSuccess"/>
    <constructor-arg value="jsp/accessDenied.jsp"/>
</bean>


<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="5"/>
    <property name="maxPoolSize" value="10"/>
    <property name="WaitForTasksToCompleteOnShutdown" value="true"/>
</bean>

我的POM如下。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springapp</groupId>
<artifactId>planit-dev</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>planit-dev</name>

<properties>
    <spring.version>4.0.0.RELEASE</spring.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.0.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>

    <dependency>
        <groupId>org.brickred</groupId>
        <artifactId>socialauth</artifactId>
        <version>4.3</version>
    </dependency>

    <dependency>
        <groupId>org.brickred</groupId>
        <artifactId>socialauth-spring</artifactId>
        <version>2.4</version>
    </dependency>

    <dependency>
        <groupId>com.restfb</groupId>
        <artifactId>restfb</artifactId>
        <version>1.6.12</version>
    </dependency>

    <dependency>
        <groupId>com.google.apis</groupId>
        <artifactId>google-api-services-calendar</artifactId>
        <version>v3-rev73-1.17.0-rc</version>
    </dependency>

    <dependency>
        <groupId>com.google.oauth-client</groupId>
        <artifactId>google-oauth-client</artifactId>
        <version>1.17.0-rc</version>
    </dependency>

    <dependency>
        <groupId>com.google.oauth-client</groupId>
        <artifactId>google-oauth-client-servlet</artifactId>
        <version>1.17.0-rc</version>
    </dependency>

    <dependency>
        <groupId>com.google.http-client</groupId>
        <artifactId>google-http-client-jackson2</artifactId>
        <version>1.15.0-rc</version>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3.1</version>
    </dependency>

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>r09</version>
    </dependency>

    <dependency>
        <groupId>org.optaplanner</groupId>
        <artifactId>optaplanner-core</artifactId>
        <version>6.0.1.Final</version>
    </dependency>

     <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.3-1100-jdbc41</version>
    </dependency>




</dependencies>

<build>

    <finalName>planit-dev</finalName>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <includes>
                    <include>**/*Tests.java</include>
                </includes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals><goal>copy</goal></goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>com.github.jsimone</groupId>
                                <artifactId>webapp-runner</artifactId>
                                <version>7.0.40.0</version>
                                <destFileName>webapp-runner.jar</destFileName>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

在intellij中构建时,似乎jar不在web-inf目录下的libs文件夹中,并且当我调用Class.forName(&#34; org.postgresql.Driver&#34;)时;我得到了类未找到的异常。

如何使用maven正确添加?

非常感谢任何帮助。

编辑:

我刚刚添加的Spring JDBC依赖项似乎也发生了这种情况。

org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.jdbc.datasource.DriverManagerDataSource] for bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.DriverManagerDataSource
org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1327)

也不在maven输出文件夹中。

这是部署到heroku,所以我不能创建一个我不想的libs文件夹!

3 个答案:

答案 0 :(得分:1)

驱动程序需要放在$ TOMCAT_HOME / lib中,而不是放在WEB-INF / lib中的WAR内部,而在pom中,驱动程序依赖项应该设置为provided

 <dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>9.3-1100-jdbc41</version>
    <scope>provided</scope>
</dependency>

通常,JDBC驱动程序应始终安装在服务器级别而不是应用程序级别。原因是驱动程序在第一次在JVM类的单例中初始化时注册自身,并将其部署到多个WAR将导致内存泄漏。

您的Tomcat可能检测到这种情况并取消注册驱动程序,防止内存泄漏但也无法正确初始化 - 请查看此answer以获取更多详细信息。

答案 1 :(得分:1)

在我的POM中证明我曾以某种方式设法将包装类型从战争改为pom。 更改此功能已将库成功添加到war文件中!

<packaging>war</packaging>

答案 2 :(得分:0)

我在我的项目中使用spring和postgresql。这里有一些片段: (ps:不记得我是否在某处手动放了postgresql驱动程序) (maven包装为战争)

<强> MAVEN

...
<properties>
    <postgresql.version>9.2-1003-jdbc4</postgresql.version>
</properties>
...

<dependencies>
    <!--PostgreSQL driver-->
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>${postgresql.version}</version>
    </dependency>
...

SPRING CONFIG CONTEXT

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="au.com.xxx.core.modules.*.persistence"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="false"/>
            <property name="generateDdl" value="false"/>
            <property name="database" value="POSTGRESQL"/>
        </bean>
    </property>
</bean>

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jdbc/DatabaseName"/>
    </bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

TOMCAT背景

<Resource name="jdbc/DatabaseName" auth="Container" type="javax.sql.DataSource"
          username="myuser" password="secret"
          url="jdbc:postgresql://172.16.1.2:5432/db_name"
          driverClassName="org.postgresql.Driver"
          initialSize="5" maxWait="5000"
          maxActive="120" maxIdle="5"
          validationQuery="select 1"
          poolPreparedStatements="true"/>

<ResourceLink name="jdbc/DatabaseName"
              global="jdbc/DatabaseName"
              type="javax.sql.DataSource"/>