我正在使用Jboss Fuse 6.2.0并尝试从我的项目建立一个jdbc连接。我正在使用blueprint.xml来定义postgresql数据源,我正在尝试将数据插入到我的系统本地托管的数据库中。这是我在blueprint.xml中的代码:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/database-name"/>
<property name="username" value="admin"/>
<property name="password" value="admin"/>
</bean>
<bean id="transactionManager" class="com.example.TransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
我在下面添加这些依赖项来使用它。下面是我的pom.xml的代码:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1200-jdbc41</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.enterprise</artifactId>
<version>4.2.0</version>
</dependency>
当我尝试在容器(Karaf)中部署我的应用程序时,我收到此错误。找到下面的堆栈跟踪:
> org.osgi.service.blueprint.container.ComponentDefinitionException:
> Error setting property: PropertyDescriptor <name: driverClassName,
> getter: null, setter: [class
> org.springframework.jdbc.datasource.DriverManagerDataSource.setDriverClassName(class
> java.lang.String)]
> at org.apache.aries.blueprint.container.BeanRecipe.setProperty(BeanRecipe.java:941)[21:org.apache.aries.blueprint.core:1.4.2]
> at org.apache.aries.blueprint.container.BeanRecipe.setProperties(BeanRecipe.java:907)[21:org.apache.aries.blueprint.core:1.4.2]
> at org.apache.aries.blueprint.container.BeanRecipe.setProperties(BeanRecipe.java:888)[21:org.apache.aries.blueprint.core:1.4.2]
> at org.apache.aries.blueprint.container.BeanRecipe.internalCreate2(BeanRecipe.java:820)[21:org.apache.aries.blueprint.core:1.4.2]
> at org.apache.aries.blueprint.container.BeanRecipe.internalCreate(BeanRecipe.java:787)[21:org.apache.aries.blueprint.core:1.4.2]
> at org.apache.aries.blueprint.di.AbstractRecipe$1.call(AbstractRecipe.java:79)[21:org.apache.aries.blueprint.core:1.4.2]
> at java.util.concurrent.FutureTask.run(FutureTask.java:266)[:1.8.0_45]
> at org.apache.aries.blueprint.di.AbstractRecipe.create(AbstractRecipe.java:88)[21:org.apache.aries.blueprint.core:1.4.2]
> at org.apache.aries.blueprint.container.BlueprintRepository.createInstances(BlueprintRepository.java:245)[21:org.apache.aries.blueprint.core:1.4.2]
> at org.apache.aries.blueprint.container.BlueprintRepository.createAll(BlueprintRepository.java:183)[21:org.apache.aries.blueprint.core:1.4.2]
> at org.apache.aries.blueprint.container.BlueprintContainerImpl.instantiateEagerComponents(BlueprintContainerImpl.java:682)[21:org.apache.aries.blueprint.core:1.4.2]
> at org.apache.aries.blueprint.container.BlueprintContainerImpl.doRun(BlueprintContainerImpl.java:377)[21:org.apache.aries.blueprint.core:1.4.2]
> at org.apache.aries.blueprint.container.BlueprintContainerImpl.run(BlueprintContainerImpl.java:269)[21:org.apache.aries.blueprint.core:1.4.2]
> at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)[:1.8.0_45]
> at java.util.concurrent.FutureTask.run(FutureTask.java:266)[:1.8.0_45]
> at org.apache.aries.blueprint.container.ExecutorServiceWrapper.run(ExecutorServiceWrapper.java:106)[21:org.apache.aries.blueprint.core:1.4.2]
> at org.apache.aries.blueprint.utils.threading.impl.DiscardableRunnable.run(DiscardableRunnable.java:48)[21:org.apache.aries.blueprint.core:1.4.2]
> at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)[:1.8.0_45]
> at java.util.concurrent.FutureTask.run(FutureTask.java:266)[:1.8.0_45]
> at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)[:1.8.0_45]
> at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)[:1.8.0_45]
> at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)[:1.8.0_45]
> at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)[:1.8.0_45]
> at java.lang.Thread.run(Thread.java:745)[:1.8.0_45]
> Caused by: java.lang.IllegalStateException: Could not load JDBC driver class [org.postgresql.Driver]
> at org.springframework.jdbc.datasource.DriverManagerDataSource.setDriverClassName(DriverManagerDataSource.java:150)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.8.0_45]
> at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)[:1.8.0_45]
> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.8.0_45]
> at java.lang.reflect.Method.invoke(Method.java:497)[:1.8.0_45]
> at org.apache.aries.blueprint.utils.ReflectionUtils$MethodPropertyDescriptor.internalSet(ReflectionUtils.java:628)[21:org.apache.aries.blueprint.core:1.4.2]
> at org.apache.aries.blueprint.utils.ReflectionUtils$PropertyDescriptor.set(ReflectionUtils.java:378)[21:org.apache.aries.blueprint.core:1.4.2]
> at org.apache.aries.blueprint.container.BeanRecipe.setProperty(BeanRecipe.java:939)[21:org.apache.aries.blueprint.core:1.4.2]
> ... 23 more
> Caused by: java.lang.ClassNotFoundException: org.postgresql.Driver
> at java.net.URLClassLoader.findClass(URLClassLoader.java:381)[:1.8.0_45]
> at java.lang.ClassLoader.loadClass(ClassLoader.java:424)[:1.8.0_45]
> at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)[:1.8.0_45]
> at java.lang.ClassLoader.loadClass(ClassLoader.java:357)[:1.8.0_45]
> at java.lang.Class.forName0(Native Method)[:1.8.0_45]
> at java.lang.Class.forName(Class.java:348)[:1.8.0_45]
> at org.springframework.jdbc.datasource.DriverManagerDataSource.setDriverClassName(DriverManagerDataSource.java:147)
> ... 30 more
以下是我的容器上安装的软件包列表:
hawtio :: project (1.4.0.redhat-133)
JBoss Fuse :: Support :: Fabric8 (1.2.0.redhat-133)
hawtio :: Red Hat Fuse Branding (1.4.0.redhat-133)
jsr311-api (1.1.1)
Apache ServiceMix :: Bundles :: jdom (1.1.0.4)
Apache ServiceMix :: Bundles :: oro (2.0.8.6)
Apache ServiceMix :: Bundles :: ant (1.7.0.6)
camel-velocity (2.15.1.redhat-620133)
wrap_mvn_org.springframework_spring-jdbc_3.2.3.RELEASE (0)
osgi.enterprise (4.2.0.201003190513)
我无法部署我的捆绑包。我添加了postgres作为依赖项。有人能告诉我如何使这项工作?
答案 0 :(得分:4)
DriverManagerDataSource在OSGi中无效。它使用了一个无法看到postgres驱动程序的类加载器。
相反,您应该使用pax jdbc。
它允许在简单配置中定义DataSource,并自动添加池和XA支持。然后,您可以将DataSource用作OSGi服务。
不幸的是,pax-jdbc-postgresql功能中存在一个小错误。所以你必须单独安装postgresql驱动程序:
install -s mvn:org.postgresql/postgresql/9.4-1200-jdbc41