通过注解实现自定义Spring Boot Starter自动装配

  • A+
所属分类:体育平台

通过注解实现自定义Spring Boot Starter自动装配

以Spring boot 作为微服务开发的项目中会用到很多第三方,或者自建的一些依赖SDK,例如 分布式锁、文件管理、数据库连接、统一错误拦截等组件。为了达到通用的目的,都会创建自定义的spring boot stater 组件。以下介绍创建步骤:

一、创建Maven测试项目test-starter pom文件如下:

<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">
	<parent>
		<groupId>com.test.starter</groupId>
		<artifactId>test-starter</artifactId>
		<version>1.0.0</version>
	</parent>
	<modelVersion>4.0.0</modelVersion>
	<artifactId>test-spring-boot-starter</artifactId>
	<packaging>jar</packaging>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-autoconfigure</artifactId>
      <version>2.3.2.RELEASE</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>test-spring-boot-starter</finalName>
	</build>
</project>

二、创建自动装配类TestServiceConfiguration.java

package com.test.spring.boot.starter.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.test.spring.boot.starter.TestService;
import com.test.spring.boot.starter.TestServiceImpl;


@Configuration
@EnableConfigurationProperties(value = TestServiceProperties.class)
public class TestServiceConfiguration {

        @Autowired
        private TestServiceProperties properties;


        @Bean
        public TestService  getTestService() {
          return new TestServiceImpl(properties);
        }
}

三、创建配置类TestServiceProperties.java

package com.test.spring.boot.starter.config;

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

import lombok.Data;
import lombok.ToString;

@Data
@ToString
@ConfigurationProperties(prefix = "test.starter")
public class TestServiceProperties {

        /**
         * 客户端ID
         */
        private String clientId;

        /**
         * 客户端名称
         */
        private String clientName;
}

四、创建提供服务的接口TestService.java

package com.test.spring.boot.starter;

public interface TestService {

      /**
       * 测试
       * @param name
       * @return
       */
      String sayHello(String name);
}

五、创建TestService的实现类型TestServiceImpl.java

package com.test.spring.boot.starter;

import com.test.spring.boot.starter.config.TestServiceProperties;

/**
 * 服务实现
 * @author test
 *
 */
public class TestServiceImpl implements TestService {

        private TestServiceProperties properties;

        public TestServiceImpl(TestServiceProperties properties) {
          this.properties = properties;
        }

        @Override
        public String sayHello(String name) {
          return "clientId: "+properties.getClientId()+",clientName: "
                       +properties.getClientName()+",welcome "+name+"!";
        }
}

六、创建自定义注解@EnableTest

package com.test.spring.boot.starter.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.context.annotation.Import;

import com.test.spring.boot.starter.config.TestServiceConfiguration;

/**
 * 自定义自动装配启动注解
 * @author test
 *
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import({TestServiceConfiguration.class})
public @interface EnableTest {

}

通过以上步骤就实现了自定义Spring boot starter test-spring-boot-starter 组件,下面创建测试项目去调用这个starter组件。

创建starter组件使用项目

一、创建maven测试项目test-service,pom文件如下

<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">
	<parent>
		<groupId>com.test.starter</groupId>
		<artifactId>test-starter</artifactId>
		<version>1.0.0</version>
	</parent>
	<modelVersion>4.0.0</modelVersion>
	<artifactId>test-service</artifactId>
	<packaging>jar</packaging>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
  <!-- 把自定义组件依赖进来-->
		<dependency>
		    <groupId>com.test.starter</groupId>
			<artifactId>test-spring-boot-starter</artifactId>
			<version>1.0.0</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>test-service</finalName>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>${spring.boot.version}</version>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

二、创建启动类Application.java

package com.test.service;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import com.test.spring.boot.starter.annotation.EnableTest;

@EnableTest    //启动自定义组件
@ComponentScan(basePackages = { 
		"com.test.service"})
@SpringBootApplication
public class Application {
        public static void main(String[] args) {
          SpringApplication.run(Application.class, args); 
        }
}

三、创建配置文件application.yml

test:
  starter:
     client-id: test-service-01
     client-name: test-service

四、创建测试类TestController.java

package com.test.service.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.test.spring.boot.starter.TestService;

@RestController
@RequestMapping("/v1/test")
public class TestController {
  
        @Resource
        private TestService testService;

        @GetMapping("/{name}")
        public String hello(@PathVariable("name")String name) {
          return testService.sayHello(name);
        }
}

到这里整个自定义spring boot starter 创建就完成了,同时也依赖到项目中进行使用了。

项目代码:starter-test: 自定义starter组件

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: