https://stackoverflow.com/questions/40384056/consider-defining-a-bean-of-type-package-in-your-configuration-spring-boot

http://blog.csdn.net/u012049760/article/details/70691925

Your Applicant class is not scanned it seems. By default all packages starting with the root as the class where you have put @SpringBootApplication will be scanned.

suppose your main class "WebServiceApplication" is in "com.service.something", then all components that fall under "com.service.something" is scanned, and "com.service.applicant" will not be scanned.

You can either restructure your packages such that "WebServiceApplication" falls under a root package and all other components becomes part of that root package. Or you can include @SpringBootApplication(scanBasePackages={"com.service.something","com.service.application"}) etc such that "ALL" components are scanned and initialized in the spring container.

Update based on comment

If you have multiple modules that are being managed by maven/gradle, all spring needs is the package to scan. You tell spring to scan "com.module1" and you have another module which has its root package name as "com.module2", those components wont be scanned. You can even tell spring to scan "com" which will then scan all components in "com.module1." and "com.module2."

I am not sure if it is because I have my project broke down in modules but this is how I solved my issue of not be able to find my repositories.

@SpringBootApplication
@ComponentScan({"com.delivery.request"})
@EntityScan("com.delivery.domain")
@EnableJpaRepositories("com.delivery.repository")
1.问题:xxx.jar中没有主清单属性 
 
命令:make debug
结果:
  1. java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5033 -Dgrpc.port=6033 -Dserver.port=8033 -jar target/demo-1.0-SNAPSHOT.jar
  2. Listening for transport dt_socket at address: 5033
  3. target/demo-1.0-SNAPSHOT.jar中没有主清单属性
解决:查找资料发现MAVEN插件打包生成的jar包中的META-INF/MANIFEST.MF文件,没有设置主函数信息。配置pom.xml即可;
  1. <plugin>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-maven-plugin</artifactId>
  4. </plugin>
(如果直接配置则会报错→问题2)
现在配置为:
 
2.问题:<plugin>不能识别
 
命令:make install
结果:
  1. mvn clean install -DskipTests
  2. [INFO] Scanning for projects...
  3. [ERROR] [ERROR] Some problems were encountered while processing the POMs:
  4. [ERROR] Malformed POM /Users/********/Documents/workspace/pom.xml: Unrecognised tag: 'plugin' (position: START_TAG seen ...</repositories>\n\n    <plugin>... @38:13)  @ /Users/********/Documents/workspace/pom.xml, line 38, column 13
  5. @
  6. [ERROR] The build could not read 1 project -> [Help 1]
  7. [ERROR]
  8. [ERROR]   The project com.mobike:demo:1.0-SNAPSHOT (/Users/********/Documents/workspace/pom.xml) has 1 error
  9. [ERROR]     Malformed POM /Users/********/Documents/workspace/pom.xml: Unrecognised tag: 'plugin' (position: START_TAG seen ...</repositories>\n\n    <plugin>... @38:13)  @ /Users/********/Documents/workspace/pom.xml, line 38, column 13 -> [Help 2]
  10. [ERROR]
  11. [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
  12. [ERROR] Re-run Maven using the -X switch to enable full debug logging.
  13. [ERROR]
  14. [ERROR] For more information about the errors and possible solutions, please read the following articles:
  15. [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
  16. [ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/ModelParseException
 
原因:未加上<build>
 
3.问题:target/xxx.jar找不到
 
命令:make debug
结果:
原因:检查target目录下的jar包名,发现与Makefile中写的不符合。将debug和run里面的名字改成对的即可。
 
4.问题:Consider defining a bean of type 'service.IUserInfoService' in your configuration.
命令:make debug
结果:
 
解决方案:加入config,并在UserController.java中加入注解@SpringBootApplication(scanBasePackages = {"service","dao","config”})
解决过程:
在工程中加入config文件,配置如下:
  1. package config;
  2. import com.alibaba.druid.pool.DruidDataSource;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.apache.ibatis.session.SqlSessionFactory;
  5. import org.mybatis.spring.SqlSessionFactoryBean;
  6. import org.mybatis.spring.annotation.MapperScan;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.beans.factory.annotation.Value;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. import org.springframework.core.io.Resource;
  12. import org.springframework.core.io.support.ResourcePatternResolver;
  13. import org.springframework.jdbc.core.JdbcTemplate;
  14. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  15. import org.springframework.stereotype.Repository;
  16. import javax.annotation.PostConstruct;
  17. import javax.annotation.PreDestroy;import javax.sql.DataSource;
  18. import java.io.IOException;import java.sql.SQLException;import java.util.ArrayList;
  19. import java.util.Arrays;import java.util.List;
  20. @Slf4j
  21. @Configuration
  22. @MapperScan(basePackages = {
  23. "dao",},    annotationClass = Repository.class,    sqlSessionFactoryRef = SysUserAuthDaoConfig.SQL_SESSION_FACTORY_NAME)
  1. public class SysUserAuthDaoConfig {
  2. public static final String SQL_SESSION_FACTORY_NAME = "opsSqlSessionFactory";
  3. @Value("${ops.database.username}")
  4. private String username;
  5. @Value("${ops.database.password}")
  6. private String password;
  7. @Value("${ops.database.url}")
  8. private String url;
  9. @Value("classpath:mybatis.userinfo/*.xml")
  10. private String mapperLocation;
  11. private DruidDataSource dataSource;
  12. private DataSourceTransactionManager transactionManager;
  13. private SqlSessionFactory sqlSessionFactory;
  14. @Autowired    private ResourcePatternResolver resourceResolver;
  15. public String getUsername() {
  16. return username;    }
  17. public void setUsername(String username) {
  18. this.username = username;    }
  19. public String getPassword() {
  20. return password;    }
  21. public void setPassword(String password) {
  22. this.password = password;    }
  23. public String getUrl() {
  24. return url;    }
  25. public void setUrl(String url) {
  26. this.url = url;    }
  27. public String getMapperLocation() {
  28. return mapperLocation;    }
  29. public void setMapperLocation(String mapperLocation) {
  30. this.mapperLocation = mapperLocation;    }
  31. public String[] getMapperLocations() {
  32. String[] mapperLocations = new String[1];        mapperLocations[0] = getMapperLocation();        return mapperLocations;    }
  33. @PostConstruct    public void init() {
  34. try {
  35. log.info("Init datasource: url: {}", url);            dataSource = new DruidDataSource();            dataSource.setDriverClassName("com.mysql.jdbc.Driver");            dataSource.setUrl(url);            dataSource.setUsername(username);            dataSource.setPassword(password);            dataSource.setTestWhileIdle(true);            dataSource.setTestOnReturn(false);            dataSource.init();
  36. transactionManager = new DataSourceTransactionManager();            transactionManager.setDataSource(dataSource);            log.info("Init done");        } catch (Throwable t) {
  37. log.error("Init error", t);        }
  38. }
  39. @PreDestroy    public void destroy() {
  40. try {
  41. log.info("Close {}", url);            dataSource.close();            log.info("Close {} done", url);        } catch (Throwable t) {
  42. log.error("Destroy error", t);        }
  43. }
  44. @Bean(name = SQL_SESSION_FACTORY_NAME)
  45. public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
  46. if (sqlSessionFactory == null) {
  47. SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();            org.apache.ibatis.session.Configuration
  48. config = new org.apache.ibatis.session.Configuration();            config.setMapUnderscoreToCamelCase(true);            sqlSessionFactoryBean.setConfiguration(config);            sqlSessionFactoryBean.setDataSource(dataSource);            List<Resource> resources = new ArrayList<>();            if (this.getMapperLocations() != null) {
  49. for (String mapperLocation : this.getMapperLocations()) {
  50. try {
  51. Resource[] mappers = resourceResolver.getResources(mapperLocation);                        resources.addAll(Arrays.asList(mappers));                    } catch (IOException e) {
  52. log.error("IOException", e);                        return null;                    }
  53. }
  54. }
  55. Resource[] arr = resources.toArray(new Resource[resources.size()]);            sqlSessionFactoryBean.setMapperLocations(arr);            sqlSessionFactory = sqlSessionFactoryBean.getObject();        }
  56. return sqlSessionFactory;    }
  57. @Bean("sysUserAuthJdbcTemplate")
  58. public JdbcTemplate jdbcTemplate() {
  59. return new JdbcTemplate(this.dataSource);    }
  60. @Bean("sysUserAuthDataSource")
  61. public DataSource getDatabase() throws SQLException {
  62. return dataSource;    }
  63. @Bean("sysUserAuthTransactionManager")
  64. public DataSourceTransactionManager transactionManager() {
  65. return transactionManager;    }
  66. }
一开始只加入了service,先扫service包,发现error变成了'dao.UserInfoMapper'
  1. Description:
  2. Field userInfoMapper in service.UserInfoServiceimpl required a bean of type 'dao.UserInfoMapper' that could not be found.
  3. Action:
  4. Consider defining a bean of type 'dao.UserInfoMapper' in your configuration.
 
可以看出实际上是因为Mapper没有被扫到,所以增加dao和config的扫描。
改成@SpringBootApplication(scanBasePackages = {"service","dao","config”})把包都扫到之后,编译通过。

Consider defining a bean of type 'package' in your configuration [Spring-Boot]的更多相关文章

  1. springboot 工程启动报错之Consider defining a bean of type ‘XXX’ in your configuration.

    一.前言: 使用springboot自动注入的方式搭建好了工程,结果启动的时候报错了!!!,错误如下图: Description: Field userEntityMapper in com.xxx. ...

  2. 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案

    搭建完spring boot的demo后自然要实现自动注入来体现spring ioc的便利了,但是我在实施过程中出现了这么一个问题,见下面,这里找到解决办法记录下来,供遇到同样的问题的同僚参考 Des ...

  3. Consider defining a bean of type `xxx` in your configuration问题解决

    在使用SpringBoot装配mybatis时出现了异常 *************************** APPLICATION FAILED TO START *************** ...

  4. IntelliJ IDEA 2017版 spring-boot 报错Consider defining a bean of type 'xxx' in your configuration问题解决方案

    问题分析: 通过问题的英文可知,这个错误是service的bean注入失败,那么为什么会产生这个问题呢? 主要是框架的Application产生的,所以我们建立项目的时候,要保证项目中的类跟Appli ...

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

    这个错误是service的bean注入失败,主要是Application位置不对,要保证项目中的类在Application启动服务器类的下一级目录,如图:

  6. spring boot注入error,Consider defining a bean of type 'xxx' in your configuration问题解决方案

    经常出现这问题一定是非spring生态圈的@标签 没被spring引入,如mybatis等 因为在默认情况下只能扫描与控制器在同一个包下以及其子包下的@Component注解,以及能将指定注解的类自动 ...

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

    果然不看教程直接使用在遇到问题会懵逼,连解决问题都得搜半天还不一定能帮你解决了... ***************************APPLICATION FAILED TO START*** ...

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

    "C:\Program Files\Java\jdk1.8.0_181\bin\java.exe" "-javaagent:C:\Program Files\JetBra ...

  9. Consider defining a bean of type 'org.springframework.data.redis.connection.RedisConnectionFactory' in your configuration

    Description: Parameter 0 of method redisTemplate in com.liaojie.cloud.auth.server.config.redis.Redis ...

随机推荐

  1. Angular4+NodeJs+MySQL 入门-02 MySql操作类

    NodeJs操作MySQL类 此类封装了几个常用的方法:插入,更新,删除,查询,开启事务,事务提交,事务回滚等操作.有一这个类,操作MYSQL就方便多了. 批处理,存储过程等方法还没有添加,因为觉得目 ...

  2. 使用SVN进行源码管理

    阅读目录: 1.SVN服务端配置 1.1 创建版本库 1.2 创建用户 1.3 设置用户权限 2.SVN客户端使用 2.1 向SVN服务器中导入源码 2.1.1 直接通过TortoiseSVN向SVN ...

  3. 实现类似tail -f file功能

    python版本py3 tail -f file是打印最后10行,然后跟踪文件追加的内容打印出来. python3 以为本方式打开的话,不能回退(f.seek(-1,1)),所有以'rb'方式打开文件 ...

  4. 【坑】自动化测试之Excel表格

    参考一位大神的博客项目架构,把元素和数据都参数化,但是总是被excel表格坑 1.无法下拉 动作列通过下拉列表来控制,点击下拉列表无反应 解决方案:不知道是不是中间动了什么,因为Excel版本的问题, ...

  5. 弱类型dynamic与var

    dynamic与var都可代替任何类型 var关键字是C# 3.0开始新增的特性,称为推断类型. 1.必须在定义时初始化 2.一但初始化完成就不能再给变量赋与初始化值类型不同的值 3.var要求是局部 ...

  6. Wp及Windows应用商店程序Logo生成器

    在开发wp或windows应用商店程序时,需要制作不同分辨率下的logo,往往不同分辨率下的logo仅仅是图片尺寸或图片的内边距不同,为了快速生成不同分辨率下的图片,减少工作量,于是就自己动手开发了个 ...

  7. win10 MySQL8.0 zip包安装及问题解决

    1.在官网下载zip包 https://dev.mysql.com/downloads/mysql/ 2.将zip包解压到自己的工作目录中 3.配置环境变量 1)添加环境变量 MYSQL_HOME E ...

  8. hdu 1255 矩形覆盖面积(面积交)

    http://www.cnblogs.com/scau20110726/archive/2013/04/14/3020998.html 面积交和面积并基本上差不多.在面积并里,len[]记录的是覆盖一 ...

  9. centos-7.2 node.js免编译安装

    cd /usr/local/ wget https://npm.taobao.org/mirrors/node/v8.9.3/node-v8.9.3-linux-x64.tar.gz tar -zxv ...

  10. ASP.NET全局异常处理

    Web项目部署后,异常直接暴露给用户会产生很不好的体验.只是暴露在服务器端又无法实时记录异常原因以便加以重现并修复.所以配合Log4Net记录日志信息,同时全局异常处理来营造良好用户体验就比较重要了. ...