最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot 無法掃描到父類模塊中Bean的原因及解決

 更新時間:2021年08月13日 09:24:49   作者:懵懂學子  
這篇文章主要介紹了springboot 無法掃描到父類模塊中Bean的原因及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

springboot 無法掃描到父類模塊中Bean

現(xiàn)象:

我定義了兩個模塊 A 和 B 。B模塊依賴A模塊

A模塊中我定義了一個@Component

卻發(fā)現(xiàn)在B模塊中我無法掃描到這個Bean導入注入失敗

如何解決

查閱得知,在springboot中的bean掃描是掃描同級目錄或者下級目錄,也就是不會掃描到依賴包里面的東西。

但是我又想定義公共Bean,該怎么做呢。

解決方案

手動注入 @Bean

如果你定義的是實體類之類的Bean,那么可以在子類中手動Bean

@Bean
Result result(){
 new Result;
}

配置掃描 @ComponentScan

但是如果你定義的Bean是類似于接口的文件,那你使用手動定義的方法就會發(fā)現(xiàn)要寫很長一段,把所有的方法都定義一下。所以還有另一種方法

@SpringBootApplication
@ComponentScan(basePackages = {"cn.o"})
public class ProxyDataSourceApplication {
 ...main(){
 }
}

如果定義了@ComponentScan掃描路徑,注意不要讓@Bean多處定義,否則會報重復注入的錯誤。

spring boot 啟動就自動關閉 之 找不到bean

創(chuàng)建的bean

package com.springboot.entity; 
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
 
/**
 * Created by ASUS on 2018/3/20.
 */
@Component
@ConfigurationProperties(prefix = "author")
public class AuthorBean {
    private String name;
    private Long age;
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getName() {
        return name;
    }
 
    public void setAge(Long age) {
        this.age = age;
    }
 
    public Long getAge() {
        return age;
    }
}

寫的application類

package com.springboot.demo; 
import com.springboot.entity.AuthorBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * Created by ASUS on 2018/3/20.
 */
@RestController
@org.springframework.boot.autoconfigure.SpringBootApplication
 
public class ApplicationDemo {
    @Autowired
    private AuthorBean authorBean;
 
    @RequestMapping("/kkk")
    String index(){
        return "author name ="+authorBean.getName()+"author age="+authorBean.getAge();
    }
 
    public static void main(String[] args) {
        SpringApplication.run(ApplicationDemo.class,args);
    }
}

控制臺報錯:

2018-03-20 22:22:02.070 INFO 11360 --- [ main] com.springboot.demo.ApplicationDemo : Starting ApplicationDemo on DESKTOP-IV2AEJK with PID 11360 (D:\IDEA\IDEAWorkSpace\SpringBootDemo\target\classes started by ASUS in D:\IDEA\IDEAWorkSpace\SpringBootDemo)
2018-03-20 22:22:02.074 INFO 11360 --- [ main] com.springboot.demo.ApplicationDemo : No active profile set, falling back to default profiles: default
2018-03-20 22:22:02.144 INFO 11360 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3fa980b: startup date [Tue Mar 20 22:22:02 CST 2018]; root of context hierarchy
2018-03-20 22:22:03.453 INFO 11360 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 9090 (http)
2018-03-20 22:22:03.462 INFO 11360 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-03-20 22:22:03.463 INFO 11360 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.27
2018-03-20 22:22:03.557 INFO 11360 --- [ost-startStop-1] o.a.c.c.C.[.[localhost].[/helloboot] : Initializing Spring embedded WebApplicationContext
2018-03-20 22:22:03.558 INFO 11360 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1416 ms
2018-03-20 22:22:03.704 INFO 11360 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2018-03-20 22:22:03.707 INFO 11360 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-03-20 22:22:03.707 INFO 11360 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-03-20 22:22:03.707 INFO 11360 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-03-20 22:22:03.708 INFO 11360 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-03-20 22:22:03.741 WARN 11360 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'applicationDemo': Unsatisfied dependency expressed through field 'authorBean'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.springboot.entity.AuthorBean' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2018-03-20 22:22:03.742 INFO 11360 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2018-03-20 22:22:03.761 INFO 11360 --- [ main] utoConfigurationReportLoggingInitializer :

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-03-20 22:22:03.858 ERROR 11360 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :

***************************
APPLICATION FAILED TO START
***************************
Description:
Field authorBean in com.springboot.demo.ApplicationDemo required a bean of type 'com.springboot.entity.AuthorBean' that could not be found.
Action:
Consider defining a bean of type 'com.springboot.entity.AuthorBean' in your configuration.
Process finished with exit code 1

其中:

意思就是找不到bean,沒有注入進去。

解決方法:

@RestController
@org.springframework.boot.autoconfigure.SpringBootApplication
@ComponentScan(basePackages = {"com.springboot.entity"})
public class ApplicationDemo {
    @Autowired
    private AuthorBean authorBean;

加注解

@ComponentScan(basePackages = {"com.springboot.entity"})

原因:

@SpringBootApplication 注解組合了@Configuration @EnableAutoConfiguration,@ComponentScan.

spring boot 會自動掃描@SpringBootApplication 所在類的同級包以及下級包里的Bean ,建議入口類放置在groupid+arctifactId 組合的包名下

以下收集別的解釋:

正常情況下加上@Component注解的類會自動被Spring掃描到生成Bean注冊到spring容器中,既然他說沒找到,也就是該注解被沒有被spring識別,問題的核心關鍵就在application類的注解SpringBootApplication上

這個注解其實相當于下面這一堆注解的效果,其中一個注解就是@Component,在默認情況下只能掃描與控制器在同一個包下以及其子包下的@Component注解,以及能將指定注解的類自動注冊為Bean的@Service@Controller和@ Repository,至此明白問題所在,之前我將接口與對應實現(xiàn)類放在了與控制器所在包的同一級目錄下,這樣的注解自然是無法被識別的

所以有兩種解決辦法:

1 .將接口與對應的實現(xiàn)類放在與application啟動類的同一個目錄或者他的子目錄下,這樣注解可以被掃描到,這是最省事的辦法

2 .在指定的application類上加上這么一行注解,手動指定application類要掃描哪些包下的注解。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 解析Hibernate + MySQL中文亂碼問題

    解析Hibernate + MySQL中文亂碼問題

    如果持久化的類中有包括了漢字的String對象,那么對應到數(shù)據(jù)庫中漢字的部分就會是亂碼。這主要是由于MySQL數(shù)據(jù)表的字符集與我們當前使用的本地字符集不相同造成的
    2013-07-07
  • SpringMVC學習之JSTL條件行為和遍歷行為詳解

    SpringMVC學習之JSTL條件行為和遍歷行為詳解

    這篇文章主要介紹了SpringMVC學習之JSTL條件行為和遍歷行為詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • SpringBoot使用Apache Tika實現(xiàn)多種文檔的內(nèi)容解析

    SpringBoot使用Apache Tika實現(xiàn)多種文檔的內(nèi)容解析

    在日常開發(fā)中,我們經(jīng)常需要解析不同類型的文檔,如PDF、Word、Excel、HTML、TXT等,Apache Tika是一個強大的內(nèi)容解析工具,可以輕松地提取文檔中的內(nèi)容和元數(shù)據(jù)信息,本文將通過SpringBoot和Apache Tika的結合,介紹如何實現(xiàn)對多種文檔格式的內(nèi)容解析
    2024-12-12
  • SpringSecurity多認證器配置多模式登錄自定義認證器方式

    SpringSecurity多認證器配置多模式登錄自定義認證器方式

    這篇文章主要介紹了SpringSecurity多認證器配置多模式登錄自定義認證器方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • Java 時間日期詳細介紹及實例

    Java 時間日期詳細介紹及實例

    這篇文章主要介紹了Java 時間日期詳細介紹及實例的相關資料,需要的朋友可以參考下
    2017-01-01
  • Java源碼解析HashMap的tableSizeFor函數(shù)

    Java源碼解析HashMap的tableSizeFor函數(shù)

    今天小編就為大家分享一篇關于Java源碼解析HashMap的tableSizeFor函數(shù),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Java解決xss轉(zhuǎn)義導致轉(zhuǎn)碼的問題

    Java解決xss轉(zhuǎn)義導致轉(zhuǎn)碼的問題

    跨站腳本攻擊XSS是最普遍的Web應用安全漏洞,本文主要介紹了Java解決xss轉(zhuǎn)義導致轉(zhuǎn)碼的問題,具有一定的參考價值,感興趣的可以了解一下
    2023-08-08
  • 一文帶你搞懂Java8的LocalDateTime

    一文帶你搞懂Java8的LocalDateTime

    LocalDateTime?是Java8中新加入的日期時間類,現(xiàn)在都?Java20?了,不會還有人沒用過?LocalDateTime?吧?今天給大家演示一下?LocalDateTime?的常用方法
    2023-04-04
  • Java多線程之Semaphore實現(xiàn)信號燈

    Java多線程之Semaphore實現(xiàn)信號燈

    這篇文章主要給大家分享的是Java多線程之Semaphore實現(xiàn)信號燈的練習,emaphore是計數(shù)信號量。Semaphore管理一系列許可證。每個acquire方法阻塞,直到有一個許可證可以獲得然后拿走一個許可證;下面一起進入文章學習Semaphore的具體內(nèi)容
    2021-10-10
  • 在項目中集成jetty server步驟解析

    在項目中集成jetty server步驟解析

    這篇文章主要介紹了在項目中集成jetty server步驟解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02

最新評論

黄山市| 紫阳县| 同心县| 长海县| 菏泽市| 大竹县| 榆林市| 榆中县| 台前县| 丰台区| 安宁市| 台安县| 德格县| 图木舒克市| 新化县| 苍梧县| 如东县| 徐闻县| 德阳市| 称多县| 库车县| 尚义县| 巴东县| 桐庐县| 泽普县| 乌审旗| 新巴尔虎左旗| 丹凤县| 昌乐县| 定襄县| 渭南市| 广汉市| 淅川县| 呼图壁县| 鄂托克前旗| 珲春市| 贡嘎县| 大同县| 大安市| 九江市| 宜春市|