没有数据源的spring-boot应用程序
Posted
技术标签:
【中文标题】没有数据源的spring-boot应用程序【英文标题】:spring-boot application without a datasource 【发布时间】:2015-11-11 12:30:26 【问题描述】:是否可以创建没有数据源的 spring-boot 应用程序?就我而言,我只需要一个简单的 REST 应用程序,但似乎在启动时尝试自动初始化数据源
我的 pom.xml 是
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>a.b.c.d.test</groupId>
<artifactId>rest-customer-builder</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>rest-customer-builder</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
<!--<relativePath/> lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我的 application.properties 是
server.contextPath=/rest-customer-builder
server.port=9090
当我跑步时
mvn clean install spring-boot:run
我看到了这个错误
015-08-18 14:54:31.870 INFO 12530 --- [lication.main()] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2015-08-18 14:54:31.903 WARN 12530 --- [lication.main()] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/apache/geronimo/osgi/locator/ProviderLocator
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:199)
at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:615)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:465)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at com.bearingpoint.eircom.test.rest.customerbuilder.RestCustomerBuilderApplication.main(RestCustomerBuilderApplication.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.boot.maven.RunMojo$LaunchRunner.run(RunMojo.java:418)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: org/apache/geronimo/osgi/locator/ProviderLocator
at javax.validation.Validation$DefaultValidationProviderResolver.getValidationProviders(Validation.java:209)
at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:173)
at org.springframework.validation.beanvalidation.LocalValidatorFactoryBean.afterPropertiesSet(LocalValidatorFactoryBean.java:223)
at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor$Jsr303ValidatorFactory.run(ConfigurationPropertiesBindingPostProcessor.java:381)
at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.afterPropertiesSet(ConfigurationPropertiesBindingPostProcessor.java:174)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
... 21 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.apache.geronimo.osgi.locator.ProviderLocator
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
【问题讨论】:
是的,这是可能的。能否请您出示您的 pom.xml 文件或 gradle 等效文件 你必须在类路径上比你在 pom.xml 中列出的更多。Jsr303ValidatorFactory
只有在类路径上有各种 javax.validation
类时才会运行。看起来你在类路径上有一些东西,其中包含指向 org/apache/geronimo/osgi/locator/ProviderLocator
的元数据
Disable all Database related auto configuration in Spring Boot的可能重复
【参考方案1】:
至于 Spring Boot 当前版本(2.5.4)可以放在@SpringBootApplication 注解上 像这样:
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class)
public class YourApplication
public static void main(String[] args)
SpringApplication.run(YourApplication.class, args);
【讨论】:
【参考方案2】:我遇到了这个问题,一旦我删除了以下依赖项,它就开始工作了。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
【讨论】:
【参考方案3】:可以在没有数据源的情况下运行 Spring Boot 应用程序。 您必须禁用数据源的自动配置,也可能适用于 JPA:
@EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class)
您发布的异常是其他内容,如 cmets 中所写,您在类路径中有一些内容引用了缺少的 apache geronimo 类。 因此,您必须摆脱引用 geronimo 的代码/jar 或将 geronimo 添加到您的依赖项中。
【讨论】:
这对我有用。我只需要排除DataSourceAutoConfiguration
。【参考方案4】:
@Stefan +1 如果您使用 YAML 文件进行配置,就是这样
spring:
profiles: dev
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
- org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
对我来说效果很好。
【讨论】:
【参考方案5】:在 application.properties 文件中提供 spring.datasource.url。它对我有用。 例子: spring.datasource.url=jdbc:mysql://localhost/Test_DB
【讨论】:
上面提到不需要数据源,所以绝对不需要为mysql添加配置。【参考方案6】:解决此问题的更好方法是从 POM/Gradle 配置文件中删除依赖项,Spring Boot 不会尝试自动配置 DataSource。
【讨论】:
例如如果您使用的是 Spring boot,请从依赖项中删除“org.springframework.boot:spring-boot-starter-data-jpa”。 从 POM 中删除 org.springframework.boot:spring-boot-starter-data-jpa 成功了,谢谢以上是关于没有数据源的spring-boot应用程序的主要内容,如果未能解决你的问题,请参考以下文章
Heroku H14(没有运行 Web 进程)+ Spring-boot
如何在没有spring-boot的情况下使用eureka+feign?
哪个数据存储(数据库)应该用于带有 AZURE 的 Spring-boot REST API 应用程序