1、springboot学习有感
使用SpringBoot框架也有一段时间了。说是使用过,但实际上是参考网上的教程搭建的。遇到问题并没有过多花心思去思考和理解,因此实际上,并不了解Springboot。
都说SpringBoot的核心思想是——约定大于配置。可是一直没能理解。
于是,看了许多文章,再结合以前遇到的一些问题,以及自己的一些思考,总算理解了为什么说“约定大于配置”是SpringBoot的核心思想了。
先抛出这么几个问题。
2、自动装配引起的问题
2.1、问题1
在使用SpringBoot搭建新模块时,会参考之前的SpringBoot模块把相关的依赖给拷贝过来,然后就会遇到这样的问题:明明只是导入了Mybatis依赖,但是代码中并没有涉及到,启动程序的时候竟然报错提示说我没有配置datasource
的url
:
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
把代码检查了几篇,并没有发现有数据库操作相关的代码。可是以前没使用Springboot时,建立新模块时,也是直接拷贝其他模块pom
里的依赖,却没出现过这样的问题啊。
当时是怎么解决这个问题的?估计也是稀里糊涂、乱打乱撞就给解决掉的吧。
2.2、问题2
搭建SpringBoot模块时,如果没有用@Mapper注解修饰dao层的接口时或者当没有在启动类中使用@MapperScan注解扫描dao层的包时,在执行dao方法时会报错,如:
Caught exception while allowing TestExecutionListener [org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@7de62196] to prepare test instance [com.uroad.model.dao.TestDaoTest@38f617f4]
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'com.uroad.model.dao.TestDaoTest':
Unsatisfied dependency expressed through field 'testDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'com.uroad.model.dao.TestDao' available:
expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
并且,在其前面还有一串让人困扰的红字信息,截取其中部分信息:
============================
CONDITIONS EVALUATION REPORT
============================
Positive matches:
-----------------
AopAutoConfiguration matched:
- @ConditionalOnClass found required classes 'org.springframework.context.annotation.EnableAspectJAutoProxy', 'org.aspectj.lang.annotation.Aspect', 'org.aspectj.lang.reflect.Advice', 'org.aspectj.weaver.AnnotatedElement' (OnClassCondition)
- @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)
Negative matches:
-----------------
ActiveMQAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)
XADataSourceAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'javax.transaction.TransactionManager' (OnClassCondition)
Exclusions:
-----------
None
Unconditional classes:
----------------------
org.springframework.boot.autoconfigure.context.Configuration