当前位置: 首页 > 知识库问答 >
问题:

运行Spring防尘套Rest

汝墨一
2023-03-14

我在本地系统上创建了一个带有Rest控制器和Oracle的spring boot应用程序,通过IDE它运行良好,mvn build was fine包也很好,但如果我将其作为可执行jar运行,我会得到以下错误。我有申请表。我提供的所有spring的属性。数据源,但这里我得到了错误。请告知。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: oracle.jdbc.driver.OracleDriver
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1154) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1056) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:372) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1134) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1028) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081) ~[spring-context-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:856) ~[spring-context-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.3.RELEASE.jar!/:1.4.3.RELEASE]
    at com.sumit.location.AppBootConfig.main(AppBootConfig.java:22) [classes!/:0.0.1-SNAPSHOT]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_112]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_112]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_112]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_112]
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [location.jar:0.0.1-SNAPSHOT]
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [location.jar:0.0.1-SNAPSHOT]
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [location.jar:0.0.1-SNAPSHOT]
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [location.jar:0.0.1-SNAPSHOT]


Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: oracle.jdbc.driver.OracleDriver
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1134) ~[spring-beans-4.3.5.RELEASE.jar!/:4.3.5.RELEASE]

这是我的pom.xml:

<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.sumit.location</groupId>
<artifactId>location</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>location Maven Webapp</name>
<url>http://maven.apache.org</url>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.3.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <!-- <dependency> <groupId>org.springframework.security.oauth</groupId> 
        <artifactId>spring-security-oauth2</artifactId> <version>2.0.5.RELEASE</version> 
        </dependency> -->
    <!-- added the below oracle jdbc dependency to get the ojdbc7 jar into 
        classpath -->
    <!-- the oracle jars are not publically hosted in maven repo so we have 
        to download it seperatly and install it to maven -->
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc7</artifactId>
        <version>12.1.0.1</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/src/lib/ojdbc7.jar</systemPath>
    </dependency>



</dependencies>
<properties>
    <java.version>1.8</java.version>
</properties>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
    </plugins>
    <finalName>location</finalName>
</build>

我正在尝试Spring Boot并尝试将其作为可执行的jar运行。

共有1个答案

谢正初
2023-03-14

oracle。jdbc。驾驶员运行时未找到OracleDriver,因为ojdbc7。jar包含在系统范围内,就可见性而言,它与提供的范围类似。它可以在Eclipse中工作,因为Eclipse不会破坏作用域,只会在运行时包含所有内容。

您至少需要依赖项是compileruntime。我相信它不能用文件依赖项来完成。

定义maven存储库以指向您的jar位置:

<repositories>
  <repository>
    <id>project-local-repo</id>
    <url>file://${project.basedir}/src/lib/</url>
  </repository>
</repositories>

将库移动/重命名为“${project.basedir}/src/lib/com/oracle/ojdbc7-12.1.0.1”。jar

然后正常地包括它:

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc7</artifactId>
    <version>12.1.0.1</version>
</dependency>

使用以下命令将jar安装到。m2存储库:

mvn install:install-file -Dfile=<jarfilePath> \
    -DgroupId=<groupId> \
    -DartifactId=<artifactId> \
    -Dversion=<version> \
    -Dpackaging=<packaging> \
    -DlocalRepositoryPath=<repoPath>

我认为以下内容应该可以完成这项工作:

mvn install:install-file -Dfile=..../src/lib/ojdbc7.jar \
    -DgroupId=com.oracle \
    -DartifactId=ojdbc7 \
    -Dversion=12.1.0.1 \
    -Dpackaging=jar

然后声明一个普通依赖项:

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc7</artifactId>
    <version>12.1.0.1</version>
</dependency>
 类似资料:
  • 我正在尝试使用spring boot和hibernate。当我使用存储库时,它工作得很好,但我正在尝试使用Hibernate会话来创建DAO,而这个DAO不是事务的一部分。 这是测试代码: 应用Java语言 UserBusinessImpl。java: 用户存储库。Java语言 用户DAO: 当我尝试getCurrentSession()时,它抛出了一个错误。openSession()与我的事务分

  • 我想将Spring启动执行器添加到我的应用程序,但当我添加此依赖项时 我得到以下错误 java.lang.reflect.InvocationTargetException at_NativeMethodAccessorImpl.invoke0(Native Method)at(...)由以下原因引起:org.springframework.beans.factory.不满意DependencyE

  • 我试图通过遵循这里的教程,使用Spring boot在Java中创建一个RESTful应用程序。我想修改它,以便可以从URL中提取标识符,并使用它来服务请求。 所以

  • 我正在尝试使用现有的Gradle Spring MVC项目设置Spring执行器。我无法使用@EnableAutoConfiguration。不幸的是,我无法到达执行器endpoint,我想我遗漏了一些东西。 项目中的Spring依赖项包括: 我正在尝试使用以下内容配置project: 在属性文件中,我添加了: 没有启用执行器endpoint,当尝试访问它们时,我得到404。我经历了许多相关问题,

  • 我目前正在尝试编写一个Spring Boot启动器,它将使用API网关自动验证我们的微服务,并在所有传出请求(针对网关)的标头中包含访问令牌。 我正在创建一个RestTemboard bean并为其提供我们的自定义拦截器,但我的问题是,通过这样做,我阻止其他团队(将使用此启动器)使用他们自己的RestTemboard配置,因为他们必须定义导致多个bean存在的相同bean。 是否有其他方法可以拦截

  • 我使用了不同端口的Spring启动执行器,如下所示 在应用程序中,我想在执行器端口中使用启用csrf=true,但我不想使用csrf。因为我想对jolokia使用批量POST请求。 只排除并不聪明。 下面的属性对我很好(bt管理。安全。启用csrf不存在)。 有什么好的解决办法吗?