spring boot自定义starter
来源:互联网 发布:chromium linux 64 编辑:程序博客网 时间:2024/04/20 08:59
在springboot中,使用的最多的就是starter。starter可以理解为一个可拔插式的插件,例如,你想使用jdbc插件,那么可以使用jdbc-starter;如果想使用mongo-starter,可以使用mongo-starter。
如下,一个简单的springboot starter依赖,
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
其中,依赖了starter-log4j2
和starter-web
两个插件。更多的如何使用springboot可以去spring官网了解更多的使用。
上面了springboot中starter中的基础使用,下面自己定义一个starter。
需求:
自定义一个名为user-starter
的starter项目,通过springboot创建一个boss项目,并在项目中调用user-starter
中的函数,并输出hello starter
。
第一步
创建一个maven项目boss,怎么创建我就不多说了,不熟悉的可以百度创建maven项目,我们重点看下第二步。
第二部
首先在pom.xml中进行依赖,如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.eju.ess</groupId> <artifactId>user-starter</artifactId> <version>0.0.2-SNAPSHOT</version> <packaging>jar</packaging> <name>user-starter</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot</artifactId> <version>1.4.3.RELEASE</version> </dependency> </dependencies></project>
由于starter项目需要使用到spring的一些特性,因此在starter项目中至少需要依赖一个对org.springframework.boot
的依赖。
接着,创建一个maven项目user-starter
,创建UserService
类,代码如下
UserService
package com.eju.ess;public class UserService { public void helloStarter(){ System.out.println(">> hello starter"); }}
创建一个UserAutoConfiguration
类,代码如下
UserAutoConfiguration
package com.eju.ess;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class UserAutoConfiguration { @Bean public UserService dbCountRunner() { return new UserService(); }}
==接着在resources目录中创建META-INF文件夹,然后在META-INF文件夹中创建spring.factories文件==,这个文件用于告诉spring boot去找指定的自动配置文件。在应用程序启动过程中,spring boot使用springfactoriesloader类加载器查找org.springframework.boot.autoconfigure.enableautoconfiguration关键字对应的java配置文件。spring boot会遍历在各个jar包种meta-inf目录下的spring.factories文件,构建成一个配置文件链表。
spring.factories内容如下
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.eju.ess.UserAutoConfiguration
第三部
在boss项目中进行依赖,在pom中增加如下代码
<dependency> <groupId>com.eju.ess</groupId> <artifactId>user-starter</artifactId> <version>0.0.2-SNAPSHOT</version> </dependency>
在Controller中使用@Autowired
进行注入,代码如下,
package com.banana;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.eju.ess.UserService;import lombok.extern.slf4j.Slf4j;@Slf4j@RestControllerpublic class SampleController { @Autowired private UserService t=null; @RequestMapping("/") String home() { t.helloStarter(); return "Hello World!"; }}
接着启动,springboo程序,访问http://127.0.0.1:8080/
在console输入如下
15:04:29.148 [restartedMain] INFO org.apache.tomcat.util.net.NioSelectorPool - Using a shared selector for servlet write/read15:04:29.186 [restartedMain] INFO org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer - Tomcat started on port(s): 9900 (http)15:04:29.195 [restartedMain] INFO com.banana.Startup - Started Startup in 4.519 seconds (JVM running for 5.464)15:04:37.260 [http-nio-9900-exec-1] INFO org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring FrameworkServlet 'dispatcherServlet'15:04:37.261 [http-nio-9900-exec-1] INFO org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization started15:04:37.277 [http-nio-9900-exec-1] INFO org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization completed in 16 ms>> hello starter
可以看到我们前面的需求hello starter
已经输出到了console。
文章参考:
http://www.jianshu.com/p/85460c1d835a
==手机QQ扫描下方二维码,快速加入Java架构师交流群==
- spring boot自定义starter
- Spring Boot 自定义starter
- 自定义spring-boot-starter-hbase
- 自定义spring-boot-starter-dubbox
- Spring-boot实例学习之 自定义starter
- 自定义spring-boot-starter(IDEA开发)
- Spring Boot自定义starter【周三更新】【 Spring Boot视频】
- spring boot starter 详解
- spring-boot-starter-dubbo
- spring-boot-starter-security
- spring boot 启动器starter
- spring-boot-starter
- Spring Boot Starter 介绍
- Druid Spring Boot Starter
- mybatis-spring-boot-starter
- druid-spring-boot-starter
- Spring-boot-Mybatis-Starter
- Spring Boot 自动配置理解 以及实现自定义Starter
- WebView控件之WebSettings各种配置方法源码总结
- win10 python35 安装 opencv
- JAVA多线程
- LightOJ - 1265 Island of Survival 脑洞 概率DP 1月3日
- Activiti 中的 Start event(启动事件元素)介绍
- spring boot自定义starter
- Opencv全屏显示IplImage
- MIDL语法详解 (译)
- S2_day_02 使用sql语句实现数据库
- 微信小程序源码Demo收集
- 导入开源Plugin with id 'com.jfrog.bintray' not found.问题
- LintCode 7-二叉树的序列化和反序列化
- SQLite数据库sql语句查询以及API查询
- PAT 乙级 1041. 考试座位号(15)