Spring-boot principle (comes with the realization of a spring-boot-starter examples and code download)

(I was a cover)

Spring-boot since appeared, and now the fire is, we seemingly are using, even the recruitment which will also require this. But frankly, spring-boot program wants to achieve nothing less than a way of pluggable, saying that simplify configuration, in fact, did not reduce the number, but fell to the configuration code inside, through a variety of annotation to mark up.

In fact, as an individual is not willing to team with spring-boot this way layers of packaging, various jar overwhelming, and ultimately we were kidnapped, their ability is not necessarily what the underlying increase.

spring-boot the starter is what the hell?
Word, spring-boot via the starter is a complete self-contained and through maven annotation may be configured such that the spring assembly or service discovery and the context of a pluggable instance.

There are two spring-boot starter

One is already supported inside, one is a third party.

Internal support for starter:

By @ConditionalOnClass decide whether to instantiate the real (ConditionalOnClass refers to an instance of the class found to be dependent, when required in the CLASSPATH), wants to start the service, as long as the configuration corresponding to starter, the starter will be able to require jar to the association.

Similar to what we use to log in, use this slf4j api interface, providing a hook default, classpath inside who achieved this institution, who hung on the hook to become log specific implementation, which is an implementation of log4j.

By maven spring configuration which can be seen already by dependencyManagement (this time maven dependency management, do not load when) you configure all of the built-in starter. Just use the time to depend on the configuration corresponding starter on the line.

<dependency>

 <groupId>org.springframework.boot</groupId>

 <artifactId>spring-boot-starter</artifactId> <

/dependency>

All through the code actually starter configuration is found in the context, it can be spring-boot-autoconfigure-xxx.jar see follows:

Built-in tomcat, jetty also inside:

Specific code:

By ConditionalOnClass (there will depend on the configuration of the maven spring-boot-starter-web of) if they find the tomcat class, will start tomcat service.

Third Party starter:

For example mybatis and so on.

The principle is the same, that is, you have achieved over the service code package layer configuration code on the line, take a look at mybitis, which is mybatis-spring-boot-starter dependent.

<dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>1.3.0</version>

</dependency>

This dependence is a pom, dependencies can see point into a self-contained, one of which is of the autoConfigure mybatis, the instantiation of the configuration mybatis by @bean like Annotation, as follows:

 

Specific code:

 

Based on this mechanism, we ourselves can achieve a

The realization of a simple database operation component, similar to mybatis, but only to achieve a simple query service.

Entire sample has two projects:

A realization of our own starter (named the spring-boot-mydao-starter), the final will be labeled ar package, and install a local factory to the local maven repository.

Another project for testing, will ultimately depend on spring-boot-mydao-starter the entire assembly, and calls inside the query service to test.

engineering

具体类如下:

1、创建spring-boot-mydao-starter工程,添加相关依赖配置

我们实现的这个组件仅仅依赖于mysql的驱动以及spring-boot的上下文环境。其中mysql驱动用的spring-boot-starter-parent里面配置好的,这里直接用即可,maven配置如下:

 

2、实现以下几个类:

Mydao:封装数据库的连接和查询服务,和spring没有任何关系。

MyDaoAutoConfiguration:提供MyDao的配置用于启动时被spring上下文发现并解析。

DbProperties:完成对数据库配置参数文件的读取(db.properties,当然spring-boot也支持其他格式的配置文件,个人还是喜欢传统样式)。

其中Mydao:

 

MyDaoAutoConfiguration(将配置参数对象传给Mydao并配置Mydao实例):

Spring在启动时会通过@Bean实例化Mydao并将其放入ConfigurableApplicationContext容器中。

配置文件类DbProperties:

自动读取classpath下的db.properties的配置文件,并实例化成类对象。配置文件需要在测试的工程(test-spring-boot-mydao-starter)里提供。

 

配置文件db.properties

 

以上就是真个starter的类容。下面描述如何使用这个starter。

 

新建测试工程test-spring-boot-mydao-starter(起啥名都行)。配置上对我们的starter的依赖:

因为starter里面已经依赖了spring-boot的上下文,所以这里就不用配置了。

 

然后就是测试:

我们用mysql自带的数据库里面的user表做测试。以下是测试类,需要注入starter里面的mydao:

 

然后就是启动测试:

也可以用spring的junit来测试。

 

~以上便是本次分享所有内容~

源码下载

To prevent failure of the address, all download links are maintained in a public number, please pay attention to the public number, reply "R003" for a complete source code.

Further reading: modular programming best practices and the Maven configuration one

Guess you like

Origin www.cnblogs.com/qiaobing/p/10985748.html