[英]Spring app run work in intellij, but java -jar doesnt
I create restfull spring app with mySQL database. 我用mySQL数据库创建了restfull spring app。 MyApp running well in Intellij, i build the jar with build artifacts in intellij.
MyApp在Intellij中运行良好,我在intellij中构建了带有构建工件的jar。 When i run the jar (java -jar) i got error.
当我运行jar(java -jar)时,我收到了错误。
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0;
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration'的bean时出错:通过构造函数参数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$Hikari.class]: Bean instantiation via factory method failed;
嵌套异常是org.springframework.beans.factory.BeanCreationException:在类路径资源中定义名为'dataSource'的bean时出错[org / springframework / boot / autoconfigure / jdbc / DataSourceConfiguration $ Hikari.class]:通过工厂方法进行Bean实例化失败; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]:
嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[com.zaxxer.hikari.HikariDataSource]:
here is my application.properties 这是我的application.properties
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/TestApp?useSSL=false
spring.datasource.username=root
spring.datasource.password=password
here is my pom.xml 这是我的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.example MyApp 1.0-SNAPSHOT jar http://maven.apache.org/xsd/maven-4.0.0.xsd“> 4.0.0 com.example MyApp 1.0-SNAPSHOT jar
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.8</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<version>4.5.8</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Can anyone help?Thank you 有人可以帮忙吗?谢谢
That would suggest you don't have all you dependencies in you jar. 这表明你没有所有的依赖关系。 Are you running correct jar - you might have more than one - one with app and one from boot in a different dir.
你正在运行正确的jar - 你可能有多个 - 一个有app,一个来自boot在另一个dir。 Would be good to see a command you use to build as well.
很高兴看到你用来构建的命令。
使用mvn包构建命令,它将构建一个带有依赖项的jar并使用该jar
My jar running well, after i choose to manual config Database connection 我选择手动配置数据库连接后,我的jar运行良好
add in Application class 在Application类中添加
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
and then create 然后创建
@Configuration
@PropertySource({ "classpath:app.datasource.properties" })
class DatabaseConfig {
@Autowired
private Environment env;
@Bean
@Primary
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("app.datasource.driverClassName"));
dataSource.setUrl(env.getProperty("app.datasource.url"));
dataSource.setUsername(env.getProperty("app.datasource.username"));
dataSource.setPassword(env.getProperty("app.datasource.password"));
return dataSource;
}
i don't know why my jar can't get autoconfiguration from spring, maybe someone can explain in comments below 我不知道为什么我的罐子不能从春天开始自动配置,也许有人可以在下面的评论中解释
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.