对于想了解springboot启动报错:requiredabeanoftype''xxxRepository''thatcouldnotbefound的读者,本文将是一篇不可错过的文章,我们将详细介绍
对于想了解springboot 启动报错:required a bean of type ''xxxRepository'' that could not be found的读者,本文将是一篇不可错过的文章,我们将详细介绍springboot 启动报错,并且为您提供关于"One or more types required to compile a dynamic expression cannot be found. Are you missing...、2. springboot 启动报错:Field userMapper in com.service.UserService required a bean of type ''com.da...、A component required a bean named ''cacheManager'' that could not be found.、A component required a bean of type ''com.example...'' that could not be found 解决办法的有价值信息。
本文目录一览:- springboot 启动报错:required a bean of type ''xxxRepository'' that could not be found(springboot 启动报错)
- "One or more types required to compile a dynamic expression cannot be found. Are you missing...
- 2. springboot 启动报错:Field userMapper in com.service.UserService required a bean of type ''com.da...
- A component required a bean named ''cacheManager'' that could not be found.
- A component required a bean of type ''com.example...'' that could not be found 解决办法
springboot 启动报错:required a bean of type ''xxxRepository'' that could not be found(springboot 启动报错)
springboot 启动的时候报错,错误如下:
Field demoRepository in com.ge.serviceImpl.DemoServiceImpl required a bean of type ''com.ge.dao.DemoRepository'' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type ''com.ge.dao.DemoRepository'' in your configuration.
原因分析:
由于搭建的 springboot 项目是分模块搭建的,使用的是 spring-data-jpa
com.ge.dao.DemoRepository 是在 ge-springboot-dao 模块下。代码如下
public interface DemoRepository extends JpaRepository<Demo, Integer> {}
但是 SpringBootApplication 是在 ge-springboot-web 这个 module 下。
com.ge.MainApplication
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
看错误信息的意思是 spring 找不到这个 bean,也就是扫描不到。
尝试的方法:
1. 在 DemoRepository 加注解 @Repository,然而好像并没有什么卵用,还是同样的错误。
2. 手动添加一个 jpa 相关的 configuration 类,同样没什么卵用。。
参考:https://stackoverflow.com/questions/46315919/spring-boot-2-0-0-m4-required-a-bean-named-entitymanagerfactory-that-could-not
1 @Configuration
2 @ComponentScan(basePackages = "com.ge")
3 @EnableJpaRepositories(
4 basePackages = "com.ge",
5 entityManagerFactoryRef = "entityManagerFactory",
6 transactionManagerRef = "transactionManager")
7 @EnableTransactionManagement
8 public class DaoConfiguration {
9
10 @Autowired private Environment environment;
11
12 @Value("${datasource.sampleapp.maxPoolSize:10}")
13 private int maxPoolSize;
14
15 /*
16 * Populate SpringBoot DataSourceProperties object directly from
17 application.yml
18 * based on prefix.Thanks to .yml, Hierachical data is mapped out of
19 the box with matching-name
20 * properties of DataSourceProperties object].
21 */
22 @Bean
23 @Primary
24 @ConfigurationProperties(prefix = "spring.datasource")
25 public DataSourceProperties dataSourceProperties() {
26 return new DataSourceProperties();
27 }
28
29 /*
30 * Configure HikariCP pooled DataSource.
31 */
32 @Bean
33 public DataSource dataSource() {
34 DataSourceProperties dataSourceProperties = dataSourceProperties();
35 HikariDataSource dataSource =
36 (HikariDataSource)
37 org.springframework.boot.jdbc.DataSourceBuilder.create(
38 dataSourceProperties.getClassLoader())
39 .driverClassName(dataSourceProperties.getDriverClassName())
40 .url(dataSourceProperties.getUrl())
41 .username(dataSourceProperties.getUsername())
42 .password(dataSourceProperties.getPassword())
43 .type(HikariDataSource.class)
44 .build();
45 dataSource.setMaximumPoolSize(maxPoolSize);
46 return dataSource;
47 }
48
49 /*
50 * Entity Manager Factory setup.
51 */
52 @Bean
53 public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException {
54 LocalContainerEntityManagerFactoryBean factoryBean =
55 new LocalContainerEntityManagerFactoryBean();
56 factoryBean.setDataSource(dataSource());
57 factoryBean.setPackagesToScan(new String[] {"webroot.websrv"});
58 factoryBean.setJpaVendorAdapter(jpaVendorAdapter());
59 factoryBean.setJpaProperties(jpaProperties());
60 return factoryBean;
61 }
62
63 /*
64 * Provider specific adapter.
65 */
66 @Bean
67 public JpaVendorAdapter jpaVendorAdapter() {
68 HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
69 return hibernateJpaVendorAdapter;
70 }
71
72 /*
73 * Here you can specify any provider specific properties.
74 */
75 private Properties jpaProperties() {
76 Properties properties = new Properties();
77 properties.put(
78 "hibernate.dialect",
79 environment.getRequiredProperty("spring.jpa.properties.hibernate.dialect"));
80 return properties;
81 }
82
83 @Bean
84 @Autowired
85 public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
86 JpaTransactionManager txManager = new JpaTransactionManager();
87 txManager.setEntityManagerFactory(emf);
88 return txManager;
89 }
90 }
3. 把主启动类 MainRepository 上的 exclude= {DataSourceAutoConfiguration.class} 去掉,然后就可以了。
原来为什么加这个参数?因为没有在 application.yml 或者 application.properties 中配置 spring.datasource.url 这个属性,所以启动会报错。
但是后来我配置了数据源相关的属性,应该把 exclude= {DataSourceAutoConfiguration.class} 去掉,而且 spring-data-jpa 是操作数据库相关的框架,可能 exculde 数据源配置导致 spring 不会自动扫描 repository。
总结:
这是我出现 @autowired 失败的原因。可能对别人并不适用。
但是对于 springboot 多模块 project 来说,能够 @autowired 自动注入的前提的,我举个例子来说明
我这里有 3 个模块:
ge-springboot-dao 模块中有 DemoRepositry
package com.ge.repository;
public interface DemoRepository extends JpaRepository<Demo, Integer> {}
ge-springboot-service 模块中有 DemoServiceImpl
package com.ge.service.impl;
@Service
public class DemoServiceImpl implements DemoService {
@Autowired private DemoRepository demoRepository;
@Override
public Demo save(Demo demo) {
return demoRepository.save(demo);
}
@Override
public Demo get(Integer id) {
return demoRepository.getOne(id);
}
}
ge-springboot-web 模块中有 DemoController 和 SpringBootApplication
package com.ge.controller;
@Controller
@RequestMapping("/demo")
public class DemoController {
@Autowired private DemoService demoService;
@ResponseBody
@GetMapping("/save")
public Demo save() {
Demo demo = new Demo();
demo.setName("gejunling");
return demoService.save(demo);
}
}
web 模块能自动注入 service 模块的 bean,service 模块能自动注入 dao 模块的 bean,前提就是所有的这些 bean 所在的 java 类都要在同一个 package 下。
可以发现,我这 3 个 bean 虽然在不同的 module 下,但是所在的 packge 都是在 com.ge.xxx.xxx
然后在 @SpringBootApplication 添加 scanBasePackge="com.ge" 即可。如下
@SpringBootApplication(scanBasePackages = {"com.ge"})
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
当然,如果不使用 scanBasePackge="com.ge" 来指指定扫描的 packge 也可以,但是要保证 springboot 主启动类要在所有 bean 的同级或者上级 packge。
比如我的 MainApplication 就是 com.ge 这个包下。
如果有不同的 package 需要 spring 自动扫描,同样可以使用 scanBasePackge={"com.ge1","com.ge2","com.ge3"}
"One or more types required to compile a dynamic expression cannot be found. Are you missing...
#事故现场:
在一个.net 4.0 的项目中使用 dynamic,示例代码如下:
1 private static void Main(string[] args)
2 {
3 dynamic obj;
4 obj = new { name = "jack" };
5 Console.WriteLine(obj.name);
6 }
在读取 obj.name 时,报错:
One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll?
#解决方法:
在项目中,添加 Microsoft.CSharp.dll 的引用;
#参考:
https://stackoverflow.com/questions/11725514/one-or-more-types-required-to-compile-a-dynamic-expression-cannot-be-found-are
——————————————————————————————————————————————————
2. springboot 启动报错:Field userMapper in com.service.UserService required a bean of type ''com.da...
报错信息:
2018-06-25 14:26:17.103 WARN 49752 --- [ restartedMain] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ''centerController'': Unsatisfied dependency expressed through field ''userService''; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ''userService'': Unsatisfied dependency expressed through field ''userMapper''; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ''com.dao.UserMapper'' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2018-06-25 14:26:17.105 INFO 49752 --- [ restartedMain] o.apache.catalina.core.StandardService : Stopping service Tomcat
2018-06-25 14:26:17.119 INFO 49752 --- [ restartedMain] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report re-run your application with ''debug'' enabled.
2018-06-25 14:26:17.228 ERROR 49752 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userMapper in com.service.UserService required a bean of type ''com.dao.UserMapper'' that could not be found.
Action:
Consider defining a bean of type ''com.dao.UserMapper'' in your configuration.
解决方法:
1. 检查接口类有没加上注解 @Mapper
A component required a bean named ''cacheManager'' that could not be found.
***************************
APPLICATION FAILED TO START
***************************
Description:
A component required a bean named ''cacheManager'' that could not be found.
Action:
Consider defining a bean named ''cacheManager'' in your configuration.
在使用 springboot 集成 hazelcast 时候,启动时候报错。
需要在启动类中加入一个 bean
@Bean
public CacheManager cacheManager() {
return new HazelcastCacheManager();
}
这样启动后,可以进行正常启动
或者添加配置文件为:
@Configuration
public class HazelcaseConfig {
/**
* @description 3.创建Hazelcase的Config类
*/
@Bean
public Config getConfig() {
Config hazelcaseConfig = new Config();
// MapConfig mapConfig = new MapConfig();
// mapConfig.setName("myMap");// 设置当前的mapConfig的名称
hazelcaseConfig.setInstanceName("test-hazelcase");// 设置当前创建的实例的名称
// .addMapConfig(mapConfig);//添加当前的map
return hazelcaseConfig;
}
}
可以进行实现
A component required a bean of type ''com.example...'' that could not be found 解决办法
工程启动报错
A component required a bean of type ‘com.example…’ that could not be found
解决办法一:
1、再启动类添加 mapper 包扫描注解即可
@MapperScan(“com.example.firstspringboot.dao”)
解决办法二:
在每个 mapper 接口上添加 @mapper 注解
转载自:https://blog.csdn.net/qq_34206683/article/details/86179789
关于springboot 启动报错:required a bean of type ''xxxRepository'' that could not be found和springboot 启动报错的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于"One or more types required to compile a dynamic expression cannot be found. Are you missing...、2. springboot 启动报错:Field userMapper in com.service.UserService required a bean of type ''com.da...、A component required a bean named ''cacheManager'' that could not be found.、A component required a bean of type ''com.example...'' that could not be found 解决办法的相关知识,请在本站寻找。
本文标签: