java入门 自定义springboot starter

一、 参考资料

参考黑马视频

二、实现starter

pom.xml

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.sht</groupId>
	<artifactId>hello-spring-boot-starter</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>hello-spring-boot-starter</name>
	<url>http://maven.apache.org</url>

	<properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-autoconfigure</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.12</version>
		</dependency>
		
	</dependencies>
</project>

HelloProperties.java

配置类,配置用于封装application中的yml

java 复制代码
package com.sht.hello.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

import lombok.Data;

/**
 * Hello模块的配置属性类,封装配置文件中的参数信息
 * @author leon
 *
 */
@Data
@ConfigurationProperties(prefix = "hello")
public class HelloProperties {
	
	private String name;
	private String address;
	
	@Override
	public String toString() {
		return "HelloProperties [name=" + name + ", address=" + address + "]";
	}
}

HelloService.java服务类

服务类需要使用到application.yml中的配置项

java 复制代码
package com.sht.hello.service;

import com.sht.hello.config.HelloProperties;

/**
 * Hello模块中的服务
 * @author leon
 *
 */
public class HelloService {

	private HelloProperties helloProperties;
	
	public HelloService(HelloProperties helloProperties) {
		this.helloProperties = helloProperties;
	}
	
	public String sayHello()
	{
		return helloProperties.getName() + "-" + helloProperties.getAddress();
	}
}

HelloAutoConfiguration.java自动配置类

java 复制代码
package com.sht.hello.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.sht.hello.service.HelloService;

/**
 * Hello模块的自动配置类
 * @author leon
 *
 */
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfiguration {

	private HelloProperties helloProperties;
	
	//构造中注入配置参数对象
	public HelloAutoConfiguration(HelloProperties helloProperties)
	{
		this.helloProperties = helloProperties;
	}
	
	@Bean
	@ConditionalOnMissingBean
	public HelloService helloService() {
		return new HelloService(helloProperties);
	}
}

spring.factories文件

路径为maven项目中的resource/META-INF/spring.factories

java 复制代码
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.sht.hello.config.HelloAutoConfiguration

三、使用starter

pom.xml

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.sht</groupId>
	<artifactId>startertest</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>startertest</name>
	<url>http://maven.apache.org</url>

	<properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
	</parent>

	<dependencies>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	
		
		<dependency>
			<groupId>com.sht</groupId>
			<artifactId>hello-spring-boot-starter</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.12</version>
		</dependency>


	</dependencies>
</project>

application.yml

java 复制代码
server:
    port: 8200

hello:
    name: leon
    address: shagnhai

App.java

java 复制代码
package com.sht.startertest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 测试定义的starter
 *
 */
@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
    	SpringApplication.run(App.class, args);
    }
}

HelloController.java

java 复制代码
package com.sht.startertest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sht.hello.service.HelloService;

@RequestMapping("/hello")
@RestController
public class HelloController {
	
	@Autowired
	private HelloService helloService;
	
	@RequestMapping("/hello")
	public String hello(){
		return helloService.sayHello();
	}
}

三、源码

https://download.csdn.net/download/tangliang_cn/90021183?spm=1001.2014.3001.5501

相关推荐
weixin_4407841110 小时前
【HandlerThread实现原理】
android·java·开发语言
天空'之城10 小时前
C 语言工业级通用组件手写 24:互补滤波
c语言·开发语言·互补滤波·嵌入式算法·工业级组件
leoZ23110 小时前
本地跑大模型实战(七):llama.cpp 性能调优,让推理更快更省
java·人工智能·spring·生成对抗网络·语言模型·自然语言处理·llama
XR12345678810 小时前
工厂车间无线网络方案对比:AGV漫游哪家强?
开发语言·php
傻啦嘿哟11 小时前
某短视频平台视频爬虫实战:抓取推荐流视频信息,绕过反爬的3种技巧
开发语言·爬虫·python
現実君11 小时前
【硬件进阶】AD22上位替换JLEDA教程
开发语言·php
mubei-12311 小时前
SpringDAO的用法
java·开发语言·数据库
happymagic12 小时前
java spring boot做的jar包程序,如何实现自动运行启动
java·运维·服务器·spring boot·jar
格林威14 小时前
多相机微秒级对齐:硬件触发 vs PTP(IEEE 1588)方案实战对比
开发语言·人工智能·数码相机·机器学习·计算机视觉·视觉检测·机器视觉
都叫我大帅哥14 小时前
给 Java 程序员的 CSRF 攻击详解:从原理到防御实战
java