Home > Mobile >  Spring Boot MVC "Consider defining a bean"
Spring Boot MVC "Consider defining a bean"

Time:06-18

i have a Problem with my Spring Boot MVC REST Application. I tried to solve the issue by myself a few days, but didn´t find a solution yet. Hope you can help me :)

My Main App:

package com.abc.mapserver;

@SpringBootApplication
public class MapServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(MapServerApplication.class, args);
    }
}

My Config Class:

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...
}

And this here:

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;
    }
}

The Problem is that Spring can´t find the "testTableRepository" Bean.

Error Code:

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.

But one interesting thing is that the second autowired candidate "IVectorData" works properly, already testet out with Postman, workes everything.

Same procedure, same file structure, with other bean, doesn´t work.

Gradle:

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'
    }
}

CodePudding user response:

Can you try adding @Component to the IVectorDataEndpoint class? Also are you sure there is no error when the TestTableRepository is being initialized?

CodePudding user response:

Can you try enabling JPA repositories explicitly ? Like below

@SpringBootApplication

@EnableJpaRepositories(basePackages = { com.abc.mapserver.infrastructure.repository })

public class MapServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(MapServerApplication.class, args);
    }
}

Also, while starup check for the logs , it should display something like

"Finished Spring data repository scanning in 100 ms etc etc " Found 1 repository....

  • Related