【问题标题】:Spring Boot MVC "Consider defining a bean"Spring Boot MVC“考虑定义一个 bean”
【发布时间】: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'
    }
}

【问题讨论】:

标签: java spring spring-boot rest java-ee-8


【解决方案1】:

您可以尝试将@Component 添加到 IVectorDataEndpoint 类吗?另外你确定在初始化TestTableRepository时没有错误吗?

【讨论】:

    猜你喜欢
    • 2019-12-05
    • 2021-04-04
    • 2019-03-16
    • 2022-01-19
    • 2021-11-25
    • 2017-11-14
    • 2017-03-16
    • 2019-01-27
    • 1970-01-01
    相关资源
    最近更新 更多