Java 在heroku上与maven一起加载Postgres驱动程序
我正在尝试使用SpringJDBC连接连接到PostgresDB。但是,当应用程序部署到tomcat时,似乎找不到postgres驱动程序。我可以在intellij中查看这个类,它列在我的external dependencies文件夹中,但是很遗憾,抛出了一个ClassNotFoundExceptionJava 在heroku上与maven一起加载Postgres驱动程序,java,spring,postgresql,maven,heroku,Java,Spring,Postgresql,Maven,Heroku,我正在尝试使用SpringJDBC连接连接到PostgresDB。但是,当应用程序部署到tomcat时,似乎找不到postgres驱动程序。我可以在intellij中查看这个类,它列在我的external dependencies文件夹中,但是很遗憾,抛出了一个ClassNotFoundException <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/20
<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>
也不在maven输出文件夹中
这是部署到heroku,所以我不能只是创建一个libs文件夹,我不认为 驱动程序需要放在$TOMCAT_HOME/lib中,而不是WEB-INF/lib中的WAR中,并且在pom中,驱动程序依赖项应该设置为
提供的:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.3-1100-jdbc41</version>
<scope>provided</scope>
</dependency>
org.postgresql
查看更多详细信息。在我的POM中发现,我设法将包装类型从war更改为POM。
更改此项已成功地将库添加到war文件中
<packaging>war</packaging>
战争
我在项目中使用spring和postgresql。以下是一些片段:
(注:不记得我是否手动将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>
...
。。。
9.2-1003-jdbc4
...
org.postgresql
postgresql
${postgresql.version}
...
SPRING配置上下文
<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>
<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"/>
TOMCAT上下文
<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>
<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"/>
这实际上是在heroku上部署的,那么我如何补偿呢?那么heroku应该在服务器上提供驱动程序,您可以在默认情况下选择Postgresql,也可以通过在线控制台中的一些配置,按照本指南检查驱动程序,以确定我是否真的遇到了问题,由于我的pom导入它的方式似乎与指南相同。请尝试将其放入pom中提供的范围内,这意味着它不会进入战争,但应该由服务器提供。我不熟悉Maven,但我看到它使用的是一个名为jimone的插件。我如何要求将JAR添加到此目标文件中?