1. 程式人生 > >Field XX required a bean of type 使用@componentscan 解決

Field XX required a bean of type 使用@componentscan 解決

很少寫部落格了,似乎就為了讓自己不斷加深印象來寫部落格啦~ 

遇到的問題 是 spring boot 啟動不起來 問題 是 

Field XXX  that could not be found.

給出的建議Action:

Consider defining a bean of type 'XX ‘ in your configuration.



Spring載入外部Bean兩種方式
將定義在另外一個帶有@Configuration的類中的Bean載入
1. 在Application類中使用@Import指定該類,
2.讓@ComponentScan掃描到該類。

大多數使用的2. 實際上是要在spring boot 初始化前加入該類所在的包 進行掃描

在spring boot 主啟動main函式上面加上預設的 掃描就好

@ComponentScan(basePackageClasses = {"XX", "XX.XX"})
這句會將相應的包進行掃描

如果想用自己的註解時可以使用SpringBootApplication 進行中間轉化 

自己封裝一個啟動註解時 

public @interface App {
   
    @AliasFor(annotation = SpringBootApplication.class, attribute = "scanBasePackages")
    String[] scanBasePackages() default {};

    @AliasFor(annotation = SpringBootApplication.class, attribute = "scanBasePackageClasses")
    Class<?>[] scanBasePackageClasses() default {};
}

當然如果就為了這個兩個註解就咩有必要封裝啦~ 就是啟動是如果加入了其餘的還要用這個註解時可以這樣封裝 

要不就直接用

SpringBootApplication
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.springframework.boot.autoconfigure;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.core.annotation.AliasFor;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
    Class<?>[] exclude() default {};

    String[] excludeName() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackages"
    )
    String[] scanBasePackages() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackageClasses"
    )
    Class<?>[] scanBasePackageClasses() default {};
}


相關推薦

Field XX required a bean of type 使用@componentscan 解決

很少寫部落格了,似乎就為了讓自己不斷加深印象來寫部落格啦~  遇到的問題 是 spring boot 啟動不起來 問題 是  Field XXX  that could not be found. 給出的建議Action: Consider defining a bean

Springboot啟動application報錯:Field userMapper in xxx required a bean of type xx that could not be found

2018-07-24 15:38:07.647 INFO 20368 --- [ main] c.e.playspring.PlayspringApplication : Starting PlayspringApplication on LAP

SpringBoot Field aFeign in xxxx required a bean of type 'xxx' that could not be found.

Field aFeign in xxxx required a bean of type ‘xxx’ that could not be found. SpringBoot 同一個專案,多模組之間呼叫 報錯 APPLICATION FAILED TO START

Field userService in com.wuji.controller.UserController required a bean of type 'com.wuji.service.UserService' that could not be found

Field userService in com.wuji.controller.UserController required a bean of type 'com.wuji.service.UserService' that could not be found. 原因:說明IOC裡沒有建立

springboot 啟動報錯 Field roleMapper in ‘’ required a bean of type '' that could not be found.

其實在報錯資訊上就能看出來問題是什麼,但是因為現在文件少,不容易找到解決方案。特此記錄 意思是掃描不到mapper層,我們需要在Application.java這個啟動檔案上加一個註解告訴spri

Autowired注入Service失敗!required a bean of type 'com.lncywlc.rio.service.UserService' that could not be

錯誤資訊: Field userService in com.lncywlc.rio.controller.LoginController required a bean of type ‘com.lncywlc.rio.service.UserService’ that could not

SpringBoot中service注入失敗(A component required a bean of type 'XXService' that could)

本人的錯誤原因是:entity,service,serviceImpl,controller等這些包和Application.java     SpringBoot程式的入口不在同一個包且不在Application.java的子包中。原因是:SpringBoot執行時所載入的

SpringBoot掃描不到類,註入失敗A component required a bean of type 'XXService' that could...

加載 required 運行時 錯誤 如果 bean 原因 rdquo 成了 SpringBoot項目的Bean裝配默認規則是根據Application類所在的包位置從上往下掃描! “Application類”是指SpringBoot項目入口類。這個

Consider defining a bean of type 'xx.xx.dao.UserDao' in your configuration.

首先因為沒配資料來源的問題在Application啟動類上加了@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}),解決啟動報錯的問題,後來資料來源配好忘了去掉,所以後來變報了

SpringBoot整合Mybatis報錯:Consider defining a bean of type 'xx.xx.xx' in your configur

SpringBoot和mybatis整合完後,如果按照spring的整合習慣,可能會發現一個錯誤: APPLICATION FAILED TO START **********************

Spring Boot:Action:Consider defining a bean of type '*.*.*' in your configuration解決方案

rri framework 成功 ould script ota http per apache   果然不看教程直接使用在遇到問題會懵逼,連解決問題都得搜半天還不一定能幫你解決了。。。 ***************************APPLICATION FAIL

springboot Consider defining a bean of type 'xxx' in your configuration

bubuko post mil ini 分享 cat bsp ring fig 這個錯誤是service的bean註入失敗,主要是Application位置不對,要保證項目中的類在Application啟動服務器類的下一級目錄,如圖: springboot Consi

兩招解決異常_Cannot find any information on property 'XXX' in a bean of type 'XXX'的問題

第一招  在進行Java Web專案開發的時候,我碰到過下面這個異常: Cannot find any information on property 'XXX' in a bean of type 'XXX' 如圖:   這是javaBean中: 這是

啟動項目,編譯報錯:Consider defining a bean of type 'XXX' in your configuration.

定義 多個 ica 錯誤 stc 就是 dea component 查看 在controller層註入ConfigBean,編譯器報錯: 一開是以為是intellij idea 的告警級別設定的問題,就沒有在意,繼續啟動項目,結果控制臺報錯:Consider defi

關於spring boot自動注入出現Mapper Consider defining a bean of type 'xxx' in your configuration問題解決方案

問題: Mapper註解不能被識別, Consider defining a bean of type 'XXX' in you configuration 參考其他部落格得到如下解決辦法: 在指定的application類上加上這麼一行註解,手動指定application類要

SpringBoot報Consider defining a bean of type ‘xxx’ in your configuration怎麼解決

今天再跑SpringBoot專案時,在新加入SpringSecurity的時候,專案一直報錯,跑不起來,報錯原因如下: 報錯的原因就是因為我引入的PasswordEncoder沒有被注入到spring bean容器,導致無法啟動,所以我們需要手動就PasswordEncoder注入到bea

關於spring boot自動注入出現Consider defining a bean of type 'xxx' in your configuration問題解決方案

轉載自:   搭建完spring boot的demo後自然要實現自動注入來體現spring ioc的便利了,但是我在實施過程中出現了這麼一個問題,見下面,這裡找到解決辦法記錄下來,供遇到同樣的問題的同僚參考    Description: Field hel

springBoot中引用redis報錯, Consider defining a bean of type 'org.springframework.data.redis.core.RedisTem

我們在springboot中經常引用redis,因為springBoot中自帶了許多起步依賴,我們不要給他加入自己的版本號,要不然很容易造成版本號衝突,導致redis引入不進來,報 Consider defining a bean of type 'org.springfra

Consider defining a bean of type 'com.pty.es.api.ArticleSearchRepository' in your configuration.問題解決

一: 真正原因: pty的maven下面匯入了com.pty.api等等的jar包 之前的專案groupId是pty所以打成包放在了pty的maven下面 所以: 選中pty-elasticsearch和pty 首先 刪除maven庫中的對應目錄的jar包 然後 Run as –

踩坑: springboot專案整合mongodb, 報Consider defining a bean of type com.xxRepository'in your configuration.

踩坑背景:  專案採用gradle分散式開發,現如今產品需要新增新功能,新功能我這邊準備開啟一個新的module來進行開發,在原來的基礎上開發出一個新的模組,一切依賴都搞定後,程式碼也寫了,就啟動準備測試一下,結果發現專案居然報錯.啟動不起來. 如上圖,直接報錯,一看是Acti