首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Springboot :扫描bean时失败

Springboot :扫描bean时失败
EN

Stack Overflow用户
提问于 2020-08-18 12:13:23
回答 2查看 311关注 0票数 0

我在试着理解弹跳力的概念。

我有如下所述的项目结构。有两个包,我在每个包中有一个类。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 src/main/java
      > com.emerald.paymentengine
         ApplicationRun.java
      > com.emerald.paymentengine.config
         DataSourceDbConfig.java

当我试图按照上面的结构运行ApplicationRun.java时,我得到的错误如下:

错误:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
2020-08-19 01:33:08.673  INFO 30128 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-08-19 01:33:08.673  INFO 30128 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-19 01:33:08.749  INFO 30128 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-08-19 01:33:08.750  INFO 30128 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 657 ms
2020-08-19 01:33:08.779  WARN 30128 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'applicationRun': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.emerlad.paymentengine.config.DataSourceDbConfig' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2020-08-19 01:33:08.781  INFO 30128 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-08-19 01:33:08.791  INFO 30128 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-08-19 01:33:08.883 ERROR 30128 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Parameter 0 of constructor in com.emerald.paymentengine.ApplicationRun required a bean of type 'com.emerlad.paymentengine.config.DataSourceDbConfig' that could not be found.


Action:

Consider defining a bean of type 'com.emerlad.paymentengine.config.DataSourceDbConfig' in your configuration.

但是,当我在下面提到的同一个包中移动DataSourceDbConfig.java时,它正在运行,并且我的输出低于:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
src/main/java
      > com.emerald.paymentengine
         ApplicationRun.java
         DataSourceDbConfig.java

输出:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
2020-08-19 01:37:24.726  INFO 34364 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-08-19 01:37:24.726  INFO 34364 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-19 01:37:24.810  INFO 34364 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-08-19 01:37:24.810  INFO 34364 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 722 ms
2020-08-19 01:37:24.849  INFO 34364 --- [  restartedMain] f.a.AutowiredAnnotationBeanPostProcessor : Autowired annotation is not supported on static fields: static com.emerald.paymentengine.DataSourceDbConfig com.emerald.paymentengine.ApplicationRun.dbConfig
Connection established !!
2020-08-19 01:37:24.981  INFO 34364 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-19 01:37:25.116  INFO 34364 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2020-08-19 01:37:25.140  INFO 34364 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8082 (http) with context path ''
2020-08-19 01:37:25.148  INFO 34364 --- [  restartedMain] c.emerald.paymentengine.ApplicationRun   : Started ApplicationRun in 1.335 seconds (JVM running for 2.406)

我认为@SpringBootApplication将自动扫描主包和子包,并能够选择所需的bean。正如第一个场景中提到的那样,如何通过将Config.java文件放置在不同的包中来使其工作呢?

代码:

ApplicationRun.java

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package com.emerald.paymentengine;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ApplicationRun {
    
    @Autowired
    static
     DataSourceDbConfig dbConfig;
    
    
    public ApplicationRun(DataSourceDbConfig dbConfig){
        ApplicationRun.dbConfig = dbConfig  ;
    }
    

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

}

DataSourceDbConfig.java

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package com.emerlad.paymentengine.config;;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import oracle.jdbc.pool.OracleDataSource;
import java.sql.SQLException;
import javax.sql.DataSource;

@Component
@Configuration
@ConfigurationProperties("spring.datasource")

public class DataSourceDbConfig {
    
    @Value("${spring.datasource.url}")
    private String dbUrl;
        
    @Value("${spring.datasource.username}")
    private String dbUser;
        
    @Value("${spring.datasource.secure}")
    private String dbPasswrd;
    
    @Bean
    public DataSource dataSource() {
        
        OracleDataSource dataSource = null;
        try {
            dataSource = new OracleDataSource();
            dataSource.setUser(dbUser);
            dataSource.setPassword(dbPasswrd);
            dataSource.setURL(dbUrl);
            
            System.out.println("Connection established !!");
            
        } catch (SQLException e) {
            System.out.println("An issue occured while establishing connection !!");
            e.printStackTrace();
        }
        
        return dataSource;
    }

}
EN

回答 2

Stack Overflow用户

发布于 2020-08-18 12:42:01

老实说,我不明白您想要实现什么,但这就是如何在您的主要方法中获得DataSource bean:

ApplicationRun.java:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package com.emerald.paymentengine;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.emerlad.paymentengine.config.DataSourceDbConfig;
import org.springframework.context.annotation.Import;
import javax.sql.DataSource;

@SpringBootApplication
@Import(DataSourceDbConfig.class)
public class ApplicationRun {
    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(ApplicationRun.class, args);
        
        DataSource dataSource = context.getBean(DataSource.class)
    }
}

DataSourceDbConfig.java:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package com.emerlad.paymentengine.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import oracle.jdbc.pool.OracleDataSource;
import java.sql.SQLException;
import javax.sql.DataSource;

@Configuration
@ConfigurationProperties("spring.datasource")
public class DataSourceDbConfig {
    @Value("${spring.datasource.url}")
    private String dbUrl;
        
    @Value("${spring.datasource.username}")
    private String dbUser;
        
    @Value("${spring.datasource.secure}")
    private String dbPasswrd;
    
    @Bean
    public DataSource dataSource() {
        OracleDataSource dataSource = null;
        try {
            dataSource = new OracleDataSource();
            dataSource.setUser(dbUser);
            dataSource.setPassword(dbPasswrd);
            dataSource.setURL(dbUrl);
            System.out.println("Connection established !!");
        } catch (SQLException e) {
            System.out.println("An issue occured while establishing connection !!");
            e.printStackTrace();
        }
        return dataSource;
    }
}
票数 0
EN

Stack Overflow用户

发布于 2020-08-18 12:45:39

使用@

  • DataSourceDbConfig --使用@Configuration包括@Component,这样您就可以摆脱它
  • ApplicationRun --我将保持它的简单性并删除

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@SpringBootApplication
public class ApplicationRun {
    
    public static void main(String[] args) {
        ApplicationContext appContext = SpringApplication.run(ApplicationRun.class, args);

        // Getting datasource from application context.
        DataSource dataSource = appContext.getBean(DataSource.class);
    
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63475961

复制
相关文章
在.Net Core中记录日志
一个完善的系统,必然会有非常完善的日志记录,用户的操作、系统的运行状况等信息被完整的记录下来,方便我们对系统进行维护和改进。.net core 也为日志记录提供了内置的支持。
拓荒者IT
2019/09/23
1.3K0
【Log日志】打印mybatis中sql日志并存放到指定文件中
logback-spring.xml (如果是logbackx.xml 动态路径会失效)
石臻臻的杂货铺[同名公众号]
2021/07/14
2.1K0
Keepalived指定日志文件
 /usr/local/keepalived/sbin/keepalived --vrrp -P Only run with VRRP subsystem. /usr/local/keepalived/sbin/keepalived --check -C Only run with Health-checker subsystem. /usr/local/keepalived/sbin/keepalived --dont-release-vrrp -V Dont remove VRRP VIPs & VROUTEs on daemon stop. /usr/local/keepalived/sbin/keepalived --dont-release-ipvs -I Dont remove IPVS topology on daemon stop. /usr/local/keepalived/sbin/keepalived --dont-fork -n Dont fork the daemon process. /usr/local/keepalived/sbin/keepalived --use-file -f Use the specified configuration file. Default is /etc/keepalived/keepalived.conf. /usr/local/keepalived/sbin/keepalived --dump-conf -d Dump the configuration data. /usr/local/keepalived/sbin/keepalived --log-console -l Log message to local console. /usr/local/keepalived/sbin/keepalived --log-detail -D Detailed log messages. /usr/local/keepalived/sbin/keepalived --log-facility -S 0-7 Set syslog facility to LOG_LOCAL[0-7]. (default=LOG_DAEMON) /usr/local/keepalived/sbin/keepalived --help -h Display this short inlined help screen. /usr/local/keepalived/sbin/keepalived --version -v Display the version number /usr/local/keepalived/sbin/keepalived --pid -p pidfile /usr/local/keepalived/sbin/keepalived --checkers_pid -c checkers pidfile /usr/local/keepalived/sbin/keepalived --vrrp_pid -r vrrp pidfile 然后通过如下命令启动keepalived,
星哥玩云
2022/06/29
6690
在 Linux 中实时监控日志文件
当你在你的 Linux 桌面、服务器或任何应用中遇到问题时,你会首先查看各自的日志文件。日志文件通常是来自应用的文本和信息流,上面有一个时间戳。它可以帮助你缩小具体的实例,并帮助你找到任何问题的原因。它也可以帮助从网络上获得援助。
用户9042463
2021/09/26
2.6K0
python 查找指定目录下的指定类型文件 脚本
""" Find the largest file of a given type in an arbitrary directory tree. Avoid repeat paths, catch errors, add tracing and line count size. Also uses sets, file iterators and generator to avoid loading entire file, and attempts to work around undecodable dir/file name prints. """
用户5760343
2022/05/13
1.6K0
在日志中记录Java异常信息的正确姿势
今天遇到一个线上的BUG,在执行表单提交时失败,但是从程序日志中看不到任何异常信息。 在Review源代码时发现,当catch到异常时只是输出了e.getMessage(),如下所示:
编程随笔
2019/06/27
2.6K0
在日志中记录Java异常信息的正确姿势
Ffmpeg 配置输出日志到指定文件
云函数(SCF) 一个主要场景就是跑视频任务,比如视频转码、推流等,常用方法就是基于 ffmpeg 来执行。
keke.wang
2022/09/26
2.4K0
批量去除文件,文件夹的名称中指定的字符。
效果如下 执行前 执行后 完整代码 package com.we; import java.io.File; /** * 批量去除文件、文件夹的名称中指定的字符 * @author Walte
崔笑颜
2020/06/08
1.3K0
【说站】Python脚本如何指定文件
(1)dest=files,是说将命令行中,--file的参数值赋值给变量files,你可以用args.files访问。
很酷的站长
2022/11/23
4990
【说站】Python脚本如何指定文件
Android保存日志记录到sd卡的文件中
一、自己写代码 Android日志服务例子,将日志记录在文件中并每天生成一个日志文件 - - ITeye技术网站 Android将应用log信息保存文件 - way - 博客频道 - CSDN.NET Android开发调试日志工具类[支持保存到SD卡] - OPEN 开发经验库 二、使用工具 android上如何实现后台日志记录并写文件到sd卡 - 博客频道 - CSDN.NET
用户1733354
2018/05/22
2.7K0
指定日志配置文件和日志profile功能
在类路径下放每个日志框架自己的配置文件;SpringBoot就不再使用默认配置文件了。但要注意的是:logback.xml直接就被日志框架识别了;logback-spring.xml日志框架就不直接加载日志但配置项,由SpringBoot解析日志配置,可以使用SpringBoot的高级Profile功能
桑鱼
2020/03/18
1K0
在Oracle中,如何定时删除归档日志文件?
1、在Oracle用户下,创建归档日志删除文件del_OCPLHR1_arch.sh
AiDBA宝典
2018/11/29
3.4K0
在Oracle中,如何定时删除归档日志文件?
日志文件记录着电脑的所有操作,如何查看日志文件?
1、点击[确定] 2、点击[系统和安全] 3、点击[查看事件日志] 4、点击[Windows日志] 5、点击[应用程序] 6、点击[应用程序] 7、点击[将所有事件另存为] 8、点击[文件名] 9、点击[事件文件] 10、点击[文本文件(制表符分隔)] 11、点击[保存] 12、点击[1.txt]
裴来凡
2022/05/28
4.3K0
日志文件记录着电脑的所有操作,如何查看日志文件?
在 Linux 中如何按名称和 Grep 内容查找文件?
如果您使用该find命令递归搜索某些文件,然后将结果通过管道传递给该grep命令,那么您实际上将解析文件路径/名称,而不是它们的内容。
网络技术联盟站
2022/05/11
6.6K0
在 Linux 中如何按名称和 Grep 内容查找文件?
在Oracle中,如何定时删除归档日志文件?
1、在Oracle用户下,创建归档日志删除文件del_OCPLHR1_arch.sh
AiDBA宝典
2022/11/07
2.1K0
尴尬:在Excel中为指定数据插入饼图失败
本来是非常非常简单的一个需求,即便不会,随便百度下也都有说明。 可自己却在一次紧急工作中因此耽误了时间,需求是需要插入一个饼图但因操作错误一直无法正确显示饼图数据,非常尴尬,干脆记录下这一刻。
Alfred Zhao
2022/06/16
1.7K0
Linux/Unix shell 脚本清除归档日志文件
      对于DEV以及UAT环境,有些时候,数据库需要处于归档模式,但并不需要备份数据库。因此,archive归档日志不停的增长导致磁盘空间被大量耗用。对于这种情形,可以使用一个shell脚本来定时自动清除这些归档日志。本文给出了清除归档日志的脚本。
Leshami
2018/08/14
1.2K0
gdb加载python脚本的方法
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xuzhina/article/details/76733977
血狼debugeeker
2018/09/20
2K0
git中忽略指定文件
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/caomage/article/details/82844233
从入门到进错门
2018/10/10
7520
点击加载更多

相似问题

http post请求离子上未返回数据

118

离子角HTTP POST请求到PHP

13

HTTP POST请求,400错误请求

10

发送HTTP POST请求错误

23

http POST请求错误400

11
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文