gpt4 book ai didi

java - 出现错误考虑在配置中定义类型为 'org.springframework.jdbc.core.JdbcTemplate' 的 bean

转载 作者:行者123 更新时间:2023-12-01 18:39:00 25 4
gpt4 key购买 nike

我在运行 spring-boot 应用程序时遇到错误。

2020-01-30 10:33:19.294  WARN 11168 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mailManagerService': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userRepository': Unsatisfied dependency expressed through field 'jdbcTemplate'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.jdbc.core.JdbcTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2020-01-30 10:33:19.294 INFO 11168 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2020-01-30 10:33:19.309 INFO 11168 --- [ restartedMain] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-01-30 10:33:19.434 ERROR 11168 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :

***************************
APPLICATION FAILED TO START
***************************

描述:

字段jdbcTemplatecom.service.el.microservice.user.UserRepository需要 org.springframework.jdbc.core.JdbcTemplate 类型的 bean找不到。

注入(inject)点有以下注解: -@org.springframework.beans.factory.annotation.Autowired(required=true)

行动:

考虑定义 org.springframework.jdbc.core.JdbcTemplate 类型的 bean在您的配置中。

但问题是我已经在配置中为 JdbcTemplate 定义了 @Bean 。我使用的是java 1.8,spring-boot:2.2.4

有人可以帮我解决这个问题吗?



@Configuration
@Component
public class someclass {

@Bean
public DataSource getDatasource() {
DriverManagerDataSource datasource = new DriverManagerDataSource();
datasource.setDriverClassName(this.dbDriver);
datasource.setUrl(this.dbURL);
datasource.setUsername(this.dbUserName);
datasource.setPassword(this.dbPassword);
return datasource;
}

@Bean
public JdbcTemplate getJdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
//return jdbcTemplate;
}

@Bean
public ResourceBundle getQueryResourceBundle() {
ResourceBundle rb = ResourceBundle.getBundle("query");
return rb;
}
}

这是使用了 JDBCtemplate 的类

@Repository
public class AutoEventRepository {

@Autowired JdbcTemplate jdbcTemplate;
@Autowired ResourceBundle resourceBundle;
private static final Logger log = LoggerFactory.getLogger(AutoEventRepository.class);

public List<AutoEventVO> getExistPielData(String esn) {
try{
List<AutoEventVO> List = jdbcTemplate.query(resourceBundle.getString(AutoEventQueryConstant.GET_EXIST_PIEL_DATA),
new Object[] { esn }, new existPIELMapper());
return List;
}

最佳答案

包含

的类

@Bean JdbcTemplate

方法应该有

@Configuration

注释,以便在组件扫描期间 spring 可以创建 JdbcTemplate 的 bean。

使用以下代码片段更改您的代码,然后重试。

@Configuration
public class SomeClass {

@Bean
public DataSource getDatasource() {
DriverManagerDataSource datasource = new DriverManagerDataSource();
datasource.setDriverClassName(this.dbDriver);
datasource.setUrl(this.dbURL);
datasource.setUsername(this.dbUserName);
datasource.setPassword(this.dbPassword);
return datasource;
}

@Bean("jdbcTemplate")
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}

@Bean("resourceBundle")
public ResourceBundle resourceBundle() {
ResourceBundle rb = ResourceBundle.getBundle("query");
return rb;
}
}

关于java - 出现错误考虑在配置中定义类型为 'org.springframework.jdbc.core.JdbcTemplate' 的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59982119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com