20190822 SpringBoot自动配置学习总结

本文通过探讨SpringBoot自动配置引发的问题,深入解析自动配置的工作原理,包括条件化注解的应用,揭示了“约定大于配置”的核心思想。文章通过实例解释了为何在SpringBoot中添加Mybatis依赖后需配置数据源,以及如何通过自动配置简化Redis的使用。作者强调理解自动配置的底层机制对于更好地使用SpringBoot至关重要。
摘要由CSDN通过智能技术生成

1、springboot学习有感

使用SpringBoot框架也有一段时间了。说是使用过,但实际上是参考网上的教程搭建的。遇到问题并没有过多花心思去思考和理解,因此实际上,并不了解Springboot。

都说SpringBoot的核心思想是——约定大于配置。可是一直没能理解。

于是,看了许多文章,再结合以前遇到的一些问题,以及自己的一些思考,总算理解了为什么说“约定大于配置”是SpringBoot的核心思想了。

先抛出这么几个问题。

2、自动装配引起的问题

2.1、问题1

在使用SpringBoot搭建新模块时,会参考之前的SpringBoot模块把相关的依赖给拷贝过来,然后就会遇到这样的问题:明明只是导入了Mybatis依赖,但是代码中并没有涉及到,启动程序的时候竟然报错提示说我没有配置datasourceurl

***************************
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值