本文会以 mybatis 为例,通过对比 mybatis-spring 和 mybatis-spring-boot-starter 代码示例,了解 Starter 的作用。并对 mybatis-spring-boot-starter 进行简单剖析,了解 Starter 原理。
下面还有投票,一起参与进来吧👍
文章目录
有没有在入行后直接基于 SpringBoot 开发项目,没有 spring、servlet 开发经历的,举个手😄。
有没有用 SpringBoot 开发项目,但是第一次听说 Starter 或者听过却不知道是干嘛的,举个手😄。
有没有知道 Starter 是干嘛的,但不知其原理的,举个手😄。
有没有想了解 Starter 原理或想自己实现一个 Starter 提供别人使用的,举个手😄。
如果有以上情况的,希望通过本文可以帮助你了解 Starter 。
大家都知道基于 SpringBoot 开发项目可以简化 Spring 应用的搭建以及开发过程,提高程序员开发效率,这是由于其“约定大约配置”的策略及其自动装配的特点。
约定大约配置是指 SpringBoot 指定了特定的方式进行配置(
application.properties/yam/yaml),开发人员不需要像在 Spring 框架开发时定义配置文件。
自动装配是指在使用某个组件或框架时需要引用其依赖、配置类、配置文件等工作时,SpringBoot 帮我们做了这些工作。
那跟 Starter 有关系吗?答案是:有! Starter 就是自动装配的具体实现,其就是一个 maven 项目,对某个组件的依赖、配置进行管理。通过导入 “Starter” 模块更容易使用这个组件。
我们通过对比 mybatis-spring 和 mybatis-spring-boot-starter 代码示例,了解 Starter 的作用。
先看下在 spring 项目中如何使用 mybatis 的。大概有以下几个步骤:
mybatis-config.xml 配置文件。
xxxMapper.xml 及 xxMapper.java 文件。相关依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<dependency>
<groupId>xxx</groupId>
<artifactId>xxxx</artifactId>
<version>xxx</version>
</dependency>
...
mybatis-config.xml 配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<context:property-placeholder location="classpath:jdbc.properties"/>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<mappers>
<package name="com.xxx.dao"/>
</mappers>
<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:xxxx/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xxx.dao"/>
</bean>
<bean class=".xxxxxx.xxx">
<!-- 指定SqlSessionFactory对象的名称 -->
<property name="sqlSessionFactoryBeanName" value="factory"/>
<!-- 指定基本包,dao接口所在的包名 -->
<property name="basePackage" value="com.xxx.dao"/>
</bean>
<bean class=".xxxxxx.xxx">
...
</bean>
</configuration>
业务编码调用
@Autowired
private XxxDao xxxDao;
xxxDao.insert(xx);
作为一个开发人员是否觉得很麻烦?答案一定是的,如果稍不留神少了哪个配置或依赖,那就排查问题吧😄。
这时候我们如果用基于 SpringBoot 开发,那 mybatis-spring-boot-starter 就可以帮助我们做这些事。
那我们继续看下在 SpringBoot 项目中如何使用 Mybatis 的。大概有以下几个步骤:
mybatis-spring-boot-starter 依赖。application.properties 文件中添加相关配置。xxxMapper.xml 及 xxMapper.java 文件。引入 mybatis-spring-boot-starter 依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
application.properties 文件中添加相关配置。
spring.datasource.username=x********
spring.datasource.password=********
spring.datasource.url=********
spring.datasource.driver-class-name=********
mybatis.mapper-locations=classpath:mapper/*.xml
编写 xxxMapper.xml 及 xxMapper.java 文件
@Mapper
public interface XXXMapper {
List<XXX> list(xxx);
}
业务编码调用
@Autowired
private XxxDao xxxDao;
xxxDao.insert(xx);
通过以上的代码比较可以明显的感觉到利用 Starter 后,我们编写的代码更少了,特别是 1、2 步骤,这就是 Starter 的作用。 mybatis-spring-boot-starter 帮助我们做了以下几件事:
mybatis-spring-boot-starter 依赖即可,也避免了版本冲突问题。那 mybatis-spring-boot-starter 是如何做这些事的,我们扒开裤子看个究竟😄。
首先看 mybatis-spring-boot-starter 项目结构,其只有一个pom.xml文件,文件中已经帮我们引入相关依赖,跟上面 Spring 整合 Mybatis 的依赖是不是差不多。

其中有一个 mybatis-spring-boot-autoconfigure 依赖,我们看下其项目结构

其通过 SPI 机制引入了 MybatisAutoConfiguration 配置类,该类帮我们做了以下几件事:
发现存在的 DataSource 并注入配置。
注册 SqlSessionFactory、SqlSessionTemplate 到 Spring 容器中。
内部类 AutoConfiguredMapperScannerRegistrar 扫描存在 @Mapper 注解类转化为 BeanDefinition 并注册到 Spring 容器中。
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我目前正在尝试学习RubyonRails和测试框架RSpec。assigns在此RSpec测试中做什么?describe"GETindex"doit"assignsallmymodelas@mymodel"domymodel=Factory(:mymodel)get:indexassigns(:mymodels).shouldeq([mymodel])endend 最佳答案 assigns只是检查您在Controller中设置的实例变量的值。这里检查@mymodels。 关于ruby-o
这段代码似乎创建了一个范围从a到z的数组,但我不明白*的作用。有人可以解释一下吗?[*"a".."z"] 最佳答案 它叫做splatoperator.SplattinganLvalueAmaximumofonelvaluemaybesplattedinwhichcaseitisassignedanArrayconsistingoftheremainingrvaluesthatlackcorrespondinglvalues.Iftherightmostlvalueissplattedthenitconsumesallrvaluesw
你能解释一下吗?我想评估来自两个不同来源的值和计算。一个消息来源为我提供了以下信息(以编程方式):'a=2'第二个来源给了我这个表达式来评估:'a+3'这个有效:a=2eval'a+3'这也有效:eval'a=2;a+3'但我真正需要的是这个,但它不起作用:eval'a=2'eval'a+3'我想了解其中的区别,以及如何使最后一个选项起作用。感谢您的帮助。 最佳答案 您可以创建一个Binding,并将相同的绑定(bind)与每个eval相关联调用:1.9.3p194:008>b=binding=>#1.9.3p194:009>eva
我无法运行Spring。这是错误日志。myid-no-MacBook-Pro:myid$spring/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/lib/spring/sid.rb:17:in`fiddle_func':uninitializedconstantSpring::SID::DL(NameError)from/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/li
我在RoR应用程序中有一个提交表单,是使用simple_form构建的。当字段为空白时,应用程序仍会继续下一步,而不会提示错误或警告。默认情况下,这些字段应该是required:true;但即使手动编写也行不通。该应用有3个步骤:NewPost(新View)->Preview(创建View)->Post。我的Controller和View的摘录会更清楚:defnew@post=Post.newenddefcreate@post=Post.new(params.require(:post).permit(:title,:category_id))ifparams[:previewButt
我一直在Heroku上尝试不同的缓存策略,并添加了他们的memcached附加组件,目的是为我的应用程序添加Action缓存。但是,当我在我当前的应用程序上查看Rails.cache.stats时(安装了memcached并使用dalligem),在执行应该缓存的操作后,我得到current和total_items为0。在Controller的顶部,我想缓存我有的Action:caches_action:show此外,我修改了我的环境配置(对于在Heroku上运行的配置)config.cache_store=:dalli_store我是否可以查看其他一些统计数据,看看它是否有效或我做错
我在我的机器上安装了ruby版本1.9.3,并且正在为我的个人网站开发一个octopress项目。我为我的gems使用了rvm,并遵循了octopress.org记录的所有步骤。但是我在我的rake服务器中发现了一些错误。这是我的命令日志。Tin-Aung-Linn:octopresstal$ruby--versionruby1.9.3p448(2013-06-27revision41675)[x86_64-darwin12.4.0]Tin-Aung-Linn:octopresstal$rakegenerate##GeneratingSitewithJekyllidenticals
我是RubyonRails的新手,我正在尝试编写一个morethan表达式:5%>大于号不断抛出异常捕获错误。我不确定如何解决这个问题?编辑:这不是rails,也不是View,它是一个Ruby构造 最佳答案 使用5%>错误来自photo_limit而不是从Integer延伸类(猜测它真的是一个字符串),因此没有混合比较方法/s有关更多信息,请参阅:http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/特别是你必须混入Comparable并定义方法。虽然在这
在回答另一个问题时,我意识到下面的程序并没有完全按照我的想法去做。puts"test"self.puts"test"#=>privatemethod`puts'calledformain:Object(NoMethodError)异常让我感到惊讶,因为我一直认为顶级方法调用将由main对象实例解决,但事实似乎并非如此。谁是第一个电话的实际接收者?如何解决?这是仅适用于顶级范围内的方法调用的特殊规则吗? 最佳答案 这是一个gooddiscussion说的就是这个问题。顶级方法,由Kernel提供,自动包含在Object类中。这意味着内