1. 程式人生 > >Spring Boot:Consider defining a bean of type '*.*.*' in your configuration解決方案

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

  果然不看教程直接使用在遇到問題會懵逼,連解決問題都得搜半天還不一定能幫你解決了。。。

***************************
APPLICATION FAILED TO START
***************************
Description:
Field mapper in com.demo.service.impl.UserServiceImpl required a bean of type 'com.demo.mapper.UserMapper' that could not be found.
Action:
Consider defining a bean of type 'com.demo.mapper.UserMapper' in your configuration.

SpringBoot啟動失敗,告訴我Bean配置失敗,樓主看了看  該用的註解都用上了  這是咋的回事嘞?

mapper(Dao層)

 1 package com.demo.mapper;
 2 
 3 import org.springframework.context.annotation.ComponentScan;
 4 import org.springframework.stereotype.Repository;
 5 
 6 import com.demo.domain.User;
 7 
 8 //@Component
 9 @Repository
10 public interface
UserMapper { 11 12 public User gYeMian(User u); 13 14 public int sYeMian(User u); 15 16 }

service

package com.demo.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo.domain.User;
import com.demo.mapper.UserMapper;
import com.demo.service.UserService; @Service(value = "userService") public class UserServiceImpl implements UserService{ @Autowired private UserMapper mapper; @Override public User gYeMian(User u) { User user = mapper.gYeMian(u); return user; } @Override public int sYeMian(User u) { int i = mapper.sYeMian(u); return i; } }

controller

 1 package com.demo.controller;
 2 
 3 import javax.servlet.http.HttpServletRequest;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.ResponseBody;
 9 
10 import com.demo.domain.User;
11 import com.demo.service.UserService;
12 
13 @Controller
14 @RequestMapping(value = "/uc")
15 public class UserController {
16     
17     @Autowired
18     private UserService userService;
19     
20     @ResponseBody
21     @RequestMapping("/stemp.htm")
22     private String sYeMian(String muBan, HttpServletRequest request){
23 
24         User u = new User();
25         u.setMuBan(muBan);
26         System.out.println("muBan=" + muBan);
27         int i = userService.sYeMian(u);
28         
29         if (i>0){
30             return "儲存成功";
31         }
32             return "儲存失敗";
33     }
34 
35 }

後來在網上看到網友說要用@Mapper註解,這才把問題解決了   至於具體原因,樓主還需要好好看看文件再來解釋。

解決方案一:

mapper(Dao層)

 1 package com.demo.mapper;
 2 
 3 import org.apache.ibatis.annotations.Mapper;
 4 
 5 import com.demo.domain.User;
 6 
 7 @Mapper
 8 public interface UserMapper {
 9 
10     public User gYeMian(User u);
11 
12     public int sYeMian(User u);
13 
14 }

 解決方案二:

Application(啟動類)

 1 package com.demo;
 2 
 3 import org.mybatis.spring.annotation.MapperScan;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 
 7 @SpringBootApplication
 8 @MapperScan(value = "com.demo.mapper")
 9 public class App 
10 {
11     public static void main(String[] args) throws Exception {
12         SpringApplication.run(App.class, args);
13     }
14 }

原因:在mybatis-spring-boot-autoconfigure的jar包中有一個類 MybatisAutoConfiguration,在這個類中的registerBeanDefinitions方法告訴了我們

 1 @Override
 2     public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
 3 
 4       logger.debug("Searching for mappers annotated with @Mapper");
 5 
 6       ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
 7 
 8       try {
 9         if (this.resourceLoader != null) {
10           scanner.setResourceLoader(this.resourceLoader);
11         }
12 
13         List<String> packages = AutoConfigurationPackages.get(this.beanFactory);
14         if (logger.isDebugEnabled()) {
15           for (String pkg : packages) {
16             logger.debug("Using auto-configuration base package '{}'", pkg);
17           }
18         }
19 
20         scanner.setAnnotationClass(Mapper.class);
21         scanner.registerFilters();
22         scanner.doScan(StringUtils.toStringArray(packages));
23       } catch (IllegalStateException ex) {
24         logger.debug("Could not determine auto-configuration package, automatic mapper scanning disabled.", ex);
25       }
26     }

相關推薦

Spring BootConsider defining a bean of type '*.*.*' in your configuration解決方案

  果然不看教程直接使用在遇到問題會懵逼,連解決問題都得搜半天還不一定能幫你解決了。。。 ***************************APPLICATION FAILED TO START***************************Description:Field mapper in c

Spring BootConsider defining a bean of type '*.*.Dao' in your configuration解決方案

SpringBoot在啟動專案的時候遇到了以下情況: APPLICATION FAILED TO START *************************** Description: Field sysUserDao in com.iamapsycho.service.imp

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

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

處理異常Consider defining a bean of type 'xxx' in your config

@SpringBootApplication(scanBasePackages = {"com.dk.d.database.service.*","com.dk.d.database.repositor

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

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

關於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類要

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

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

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

在做spring cloud的hystrix的時候,寫了一個controller,注入內部類時發生了一個錯誤:看起來就是找不到這個類,問題是我已經寫了這個類:百度後發現有發生同樣問題的童鞋,原來問題出在application類的註解@SpringBootApplication

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

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

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

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

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

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

Consider defining a bean of type 'com.lvjing.dao.DeviceStatusMapper' in your configuration.

exceptio can plugin spring ould tac glib pre scripts "C:\Program Files\Java\jdk1.8.0_181\bin\java.exe" "-javaagent:C:\Program Files\JetBr

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

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

Consider defining a bean of type 'org.springframework.web.client.RestTemplate' in your configuration

如題,在springcloud開發中,當我們將RestTemplate通過@Autowired註解注入到一個類中,啟動服務報錯。 在springboot1.3版本中會預設提供一個RestTemplate的例項Bean,當在springboot1.4以及以後的版本中,需要手動

Consider defining a bean named 'entityManagerFactory' in your configuration.

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 2018-01-24 01:05:38.444 ERRO

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