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

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

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

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

現(xiàn)象:

我定義了兩個(gè)模塊 A 和 B 。B模塊依賴A模塊

A模塊中我定義了一個(gè)@Component

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

如何解決

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

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

解決方案

手動(dòng)注入 @Bean

如果你定義的是實(shí)體類之類的Bean,那么可以在子類中手動(dòng)Bean

@Bean
Result result(){
 new Result;
}

配置掃描 @ComponentScan

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

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

如果定義了@ComponentScan掃描路徑,注意不要讓@Bean多處定義,否則會(huì)報(bào)重復(fù)注入的錯(cuò)誤。

spring boot 啟動(dòng)就自動(dòng)關(guān)閉 之 找不到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);
    }
}

控制臺(tái)報(bào)錯(cuò):

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,沒有注入進(jìn)去。

解決方法:

@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 會(huì)自動(dòng)掃描@SpringBootApplication 所在類的同級(jí)包以及下級(jí)包里的Bean ,建議入口類放置在groupid+arctifactId 組合的包名下

以下收集別的解釋:

正常情況下加上@Component注解的類會(huì)自動(dòng)被Spring掃描到生成Bean注冊(cè)到spring容器中,既然他說沒找到,也就是該注解被沒有被spring識(shí)別,問題的核心關(guān)鍵就在application類的注解SpringBootApplication上

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

所以有兩種解決辦法:

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

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

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

相關(guān)文章

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

    解析Hibernate + MySQL中文亂碼問題

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

    SpringMVC學(xué)習(xí)之JSTL條件行為和遍歷行為詳解

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

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

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

    SpringSecurity多認(rèn)證器配置多模式登錄自定義認(rèn)證器方式

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

    Java 時(shí)間日期詳細(xì)介紹及實(shí)例

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

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

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

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

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

    一文帶你搞懂Java8的LocalDateTime

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

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

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

    在項(xiàng)目中集成jetty server步驟解析

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

最新評(píng)論

新乐市| 贺兰县| 清流县| 怀远县| 甘孜| 山阳县| 金川县| 大石桥市| 高雄市| 百色市| 包头市| 宁阳县| 河东区| 平顶山市| 久治县| 辰溪县| 霸州市| 衡东县| 西藏| 肥东县| 祁阳县| 眉山市| 乌恰县| 溧阳市| 阜宁县| 竹山县| 新密市| 隆安县| 柳州市| 德阳市| 丰顺县| 方城县| 淅川县| 庆云县| 马鞍山市| 浮梁县| 克山县| 余干县| 抚远县| 汽车| 榆林市|