【发布时间】:2022-06-17 22:39:47
【问题描述】:
我的 Spring Boot MVC REST 应用程序有问题。 几天我试图自己解决这个问题,但还没有找到解决方案。 希望你能帮助我:)
我的主应用:
package com.abc.mapserver;
@SpringBootApplication
public class MapServerApplication {
public static void main(String[] args) {
SpringApplication.run(MapServerApplication.class, args);
}
}
我的配置类:
package com.abc.mapserver;
@Configuration
@EnableWebMvc
public class MapServerConfiguration implements WebMvcConfigurer {
@Value("${mapserver.connection-string}")
private String connectionString;
}
TestTableRepository.java:
package com.abc.mapserver.infrastructure.repository;
@Repository
public interface TestTableRepository extends JpaRepository<TestTable, Long> {}
IVectorData:
package com.abc.mapserver.infrastructure.service;
public interface IVectorData {
// Interface Methods...
}
这里是:
package com.abc.mapserver.infrastructure.endpoints;
public class IVectorDataEndpoint {
IVectorData iVectorData;
TestTableRepository testTableRepository;
**@Autowired
public void setTestTableRepository(TestTableRepository testTableRepository) {
this.testTableRepository = testTableRepository;
}**
@Autowired
public void setiVectorData(IVectorData iVectorData) {
this.iVectorData = iVectorData;
}
}
问题是 Spring 找不到“testTableRepository”Bean。
错误代码:
Description:
Parameter 0 of method setTestTableRepository in com.abc.mapserver.infrastructure.endpoints.IVectorDataEndpoint required a bean of type 'com.abc.mapserver.infrastructure.repository.TestTableRepository' that could not be found.
Action:
Consider defining a bean of type 'com.abc.mapserver.infrastructure.repository.TestTableRepository' in your configuration.
但一件有趣的事情是,第二个自动连接的候选“IVectorData”可以正常工作,已经与 Postman 一起测试,可以正常工作。
相同的过程,相同的文件结构,与其他 bean 不工作。
分级:
dependencies {
implementation "org.springframework.boot:spring-boot-starter-jdbc:${spring_boot_version}"
implementation "org.springframework.boot:spring-boot-starter-web:${spring_boot_version}"
implementation "org.springframework.boot:spring-boot-starter-thymeleaf:${spring_boot_version}"
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.hibernate:hibernate-spatial'
implementation 'org.apache.commons:commons-lang3:3.0'
implementation 'org.springdoc:springdoc-openapi-ui:1.3.7'
implementation 'org.apache.commons:commons-lang3:3.10'
implementation 'com.google.guava:guava:29.0-jre'
implementation 'org.locationtech.jts:jts-core:1.18.1'
runtimeOnly 'org.postgresql:postgresql:42.2.13'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
【问题讨论】:
-
您能展示一下您是如何配置数据源的吗?我的猜测是 Spring 无法实例化存储库,因为数据源丢失或出现故障。您按照哪个说明设置项目?我建议考虑以下几点:baeldung.com/the-persistence-layer-with-spring-and-jpa
标签: java spring spring-boot rest java-ee-8