微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

春季启动:NoSuchBeanDefinitionException

如何解决春季启动:NoSuchBeanDefinitionException

我的Spring Boot应用程序有问题:

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

http:// localhost:8084 / actuator / beans /:

"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": [],"type": "com.test.dbproto4.UserRepository","resource": null,"dependencies": [
"(inner bean)#667aba3e","(inner bean)#22b3117e","(inner bean)#2607b66f","jpaMappingContext"
]
}

执行器显示名为“ testService”的bean,但是Spring没有看到它。 如何从ApplicationContext获取bean“ testService”?

解决方法

解决方案

对您的Dbproto4Application类执行这些更改

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

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

问题

在Spring引导应用程序中,您已经配置了2个不同的Spring应用程序上下文。

// 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");  

请注意,您创建的第二个应用程序上下文是从SpringConfig构建的,该上下文只能访问在 com.test.dbproto4.infrastructure.storage.jdbcrepos 包中定义的bean。它无权访问testService bean。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。