• Consider defining a bean of type 'package' in your configuration [Spring-Boot]


    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
    结果:
    [html] view plain copy
     
    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即可;
    [html] view plain copy
     
    1. <plugin>  
    2.         <groupId>org.springframework.boot</groupId>  
    3.         <artifactId>spring-boot-maven-plugin</artifactId>  
    4. </plugin>  
    (如果直接配置则会报错→问题2)
    现在配置为:
     
    2.问题:<plugin>不能识别
     
    命令:make install
    结果:
    [html] view plain copy
     
    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>     <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>     <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文件,配置如下:
    [java] view plain copy
     
    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)  
    [java] view plain copy
     
    1. public class SysUserAuthDaoConfig {  
    2.   
    3.     public static final String SQL_SESSION_FACTORY_NAME = "opsSqlSessionFactory";  
    4.     @Value("${ops.database.username}")  
    5.     private String username;  
    6.     @Value("${ops.database.password}")  
    7.     private String password;  
    8.     @Value("${ops.database.url}")  
    9.     private String url;  
    10.     @Value("classpath:mybatis.userinfo/*.xml")  
    11.     private String mapperLocation;  
    12.     private DruidDataSource dataSource;  
    13.     private DataSourceTransactionManager transactionManager;  
    14.     private SqlSessionFactory sqlSessionFactory;  
    15.     @Autowired    private ResourcePatternResolver resourceResolver;  
    16.     public String getUsername() {  
    17.         return username;    }  
    18.   
    19.     public void setUsername(String username) {  
    20.         this.username = username;    }  
    21.   
    22.     public String getPassword() {  
    23.         return password;    }  
    24.   
    25.     public void setPassword(String password) {  
    26.         this.password = password;    }  
    27.   
    28.     public String getUrl() {  
    29.         return url;    }  
    30.   
    31.     public void setUrl(String url) {  
    32.         this.url = url;    }  
    33.   
    34.     public String getMapperLocation() {  
    35.         return mapperLocation;    }  
    36.   
    37.     public void setMapperLocation(String mapperLocation) {  
    38.         this.mapperLocation = mapperLocation;    }  
    39.   
    40.     public String[] getMapperLocations() {  
    41.         String[] mapperLocations = new String[1];        mapperLocations[0] = getMapperLocation();        return mapperLocations;    }  
    42.   
    43.     @PostConstruct    public void init() {  
    44.         try {  
    45.             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();  
    46.             transactionManager = new DataSourceTransactionManager();            transactionManager.setDataSource(dataSource);            log.info("Init done");        } catch (Throwable t) {  
    47.             log.error("Init error", t);        }  
    48.     }  
    49.   
    50.     @PreDestroy    public void destroy() {  
    51.         try {  
    52.             log.info("Close {}", url);            dataSource.close();            log.info("Close {} done", url);        } catch (Throwable t) {  
    53.             log.error("Destroy error", t);        }  
    54.     }  
    55.   
    56.     @Bean(name = SQL_SESSION_FACTORY_NAME)  
    57.     public SqlSessionFactory sqlSessionFactoryBean() throws Exception {  
    58.         if (sqlSessionFactory == null) {  
    59.             SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();            org.apache.ibatis.session.Configuration  
    60.                 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) {  
    61.                 for (String mapperLocation : this.getMapperLocations()) {  
    62.                     try {  
    63.                         Resource[] mappers = resourceResolver.getResources(mapperLocation);                        resources.addAll(Arrays.asList(mappers));                    } catch (IOException e) {  
    64.                         log.error("IOException", e);                        return null;                    }  
    65.                 }  
    66.             }  
    67.             Resource[] arr = resources.toArray(new Resource[resources.size()]);            sqlSessionFactoryBean.setMapperLocations(arr);            sqlSessionFactory = sqlSessionFactoryBean.getObject();        }  
    68.         return sqlSessionFactory;    }  
    69.   
    70.     @Bean("sysUserAuthJdbcTemplate")  
    71.     public JdbcTemplate jdbcTemplate() {  
    72.         return new JdbcTemplate(this.dataSource);    }  
    73.   
    74.     @Bean("sysUserAuthDataSource")  
    75.     public DataSource getDatabase() throws SQLException {  
    76.         return dataSource;    }  
    77.   
    78.     @Bean("sysUserAuthTransactionManager")  
    79.     public DataSourceTransactionManager transactionManager() {  
    80.         return transactionManager;    }  
    81.   
    82. }  


    一开始只加入了service,先扫service包,发现error变成了'dao.UserInfoMapper'
    [html] view plain copy
     
    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”})把包都扫到之后,编译通过。
  • 相关阅读:
    linux常用的基础知识
    【AW346】走廊泼水节
    【AW355】异象石
    【POJ3417】闇の連鎖
    【APIO2010】巡逻
    【SDOI2011】消防
    【BJWC2010】次小生成树
    【POJ3613】Cow Relays
    【POJ1734】Sightseeing trip
    【POJ1094】Sorting it all out
  • 原文地址:https://www.cnblogs.com/exmyth/p/7119225.html
Copyright © 2020-2023  润新知