当前位置: 首页 > 知识库问答 >
问题:

Spring Boot:NoSuchBeanDefinitionException

墨星鹏
2023-03-14
spring.bat init --artifactId=dbproto4 --boot-version=2.1.7.RELEASE --dependencies=jdbc,data-rest,web,thymeleaf,devtools,lombok,configuration-processor,security,data-jpa,data-jdbc,postgresql,actuator --description=dbproto4 --groupId=com.test --java-version=11 --name=dbproto4 --package-name=com.test.dbproto4 --version=0.0.1-SNAPSHOT
Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'testService' available

DBProto4Application:

@SpringBootApplication公共类Dbproto4Application{

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

    ApplicationContext javaConfigContext = new AnnotationConfigApplicationContext(SpringConfig.class);
    TestService testServiceObj = (TestService) javaConfigContext.getBean("testService");        
    testServiceObj.addNewUser();

SpringConfig:

@EnableJdbcRepositories(basePackages = "com.test.dbproto4.infrastructure.storage.jdbcrepos")
@Configuration
public class SpringConfig { }
@Entity
@Table(name = "userstab")
public class User {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    private String name;

    private String email;

...

}
public interface UserRepository extends CrudRepository<User, Integer> {

}
server.port = 8082

## default connection pool
spring.datasource.hikari.connectionTimeout=20000
spring.datasource.hikari.maximumPoolSize=5


## PostgreSQL
spring.datasource.url=jdbc:postgresql://localhost:5432/example_db4
spring.datasource.username=someuser
spring.datasource.password=123456

spring.jpa.hibernate.ddl-auto=update
"testService": {
"aliases": [],
"scope": "singleton",
"type": "com.test.dbproto4.TestService",
"resource": "file [E:\\Java\\JavaProjects\\dbproto4\\target\\classes\\com\\test\\dbproto4\\TestService.class]",
"dependencies": [
"userRepository"
]
}

"userRepository": {
"aliases": [],
"scope": "singleton",
"type": "com.test.dbproto4.UserRepository",
"resource": null,
"dependencies": [
"(inner bean)#667aba3e",
"(inner bean)#22b3117e",
"(inner bean)#2607b66f",
"jpaMappingContext"
]
}

共有1个答案

苏宜人
2023-03-14

解决方案

对Dbproto4Application类执行以下更改

ApplicationContext javaConfigContext = SpringApplication.run(Dbproto4Application.class, args); 

TestService testServiceObj = (TestService) javaConfigContext.getBean("testService");  

问题

// 1st application context and the one which Actuator endpoint is referring to
SpringApplication.run(Dbproto4Application.class, args); 

// 2nd application context which is created here from which you want testService bean
ApplicationContext javaConfigContext = new AnnotationConfigApplicationContext(SpringConfig.class);
TestService testServiceObj = (TestService) javaConfigContext.getBean("testService");  
 类似资料:

相关问答

相关文章

相关阅读