自定義Spring Boot starter

 

我們用spring boot 快速開發應用程序時,經常會引入很多這樣以spring-boot-starter開頭的的庫,

我就演示下自定義一個這樣的庫,功能是日誌輸出到什麼地方(控制檯、文件還是數據庫)

前提是maven已經配置好了,參考 https://my.oschina.net/u/154866/blog/3223749

1  新建maven項目,參考spring官網  https://start.spring.io/,按實際情況填寫基本信息和選擇要依賴的庫

然後點擊“GENERATE”按鈕,  注意生成前可以點擊EXPLORE預覽下項目結構

然後把下載後的文件解壓後導入到開發工具(比如eclipse)中,很簡單,打開eclipse,找到import按鈕

出現以下提示

點擊next:

找到路徑後點擊"確定"按鈕,出現

最後點擊“finish”完成,即可完成maven項目的導入。

2.  正式進入編碼階段

新建主包名, 比如com.dongguangming,  然後分別建子包:

com.dongguangming.service, com.dongguangming.service.impl,
com.dongguangming.annotation,com.dongguangming.condition,com.dongguangming.autoconfigure

2.1 建日誌服務接口

/**
 * 
 * @author dgm
 * @describe "日誌服務接口"
 */
public interface LogService {

    void print(String message);
}

2.2  實現日誌服務接口,分三種實現,控制檯、文件、數據庫mysql

import com.dongguangming.service.LogService;

/**
 * @author dgm
 * @describe "日誌到控制檯"
 */
public class StdOutLogServiceImpl implements LogService {

	@Override
	public void print(String message) {
        System.out.println(message);
        System.out.println("寫日誌到控制檯!");
	}
}

/**
 * 
 * @author dgm
 * @describe "日誌到文件"
 */
public class FileLogServiceImpl implements LogService {

	private static final String FILE_NAME="d://LogService.txt";
	@Override
	public void print(String message) {
		try {
			File file = new File(FILE_NAME);
			FileWriter fw = null;
			// true:表示是追加的標誌
			fw = new FileWriter(file, true);
			fw.write(message+"\n");
			fw.close();

	        System.out.println(message);
			System.out.println("寫日誌入文件!");
		} catch (IOException e) {
		}
	}
}

/**
 * @author dgm
 * @describe "寫日誌入mysql數據庫"
 */
public class MysqlLogServiceImpl implements LogService {

	@Override
	public void print(String message) {
        System.out.println(message);
        System.out.println("寫日誌入數據庫");
	}
}

2.3  建日誌類型註解

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

import org.springframework.context.annotation.Conditional;

import com.dongguangming.condition.LogServiceTypeCondition;

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Conditional(LogServiceTypeCondition.class)
public @interface LogServiceType
{
	//日誌輸出到什麼地方去(控制檯,file還是寫到數據庫mysql)
    String value() default "stdout";
}

2.4  通過Condition條件判斷寫日誌

import java.util.Map;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import com.dongguangming.annotation.LogServiceType;

public class LogServiceTypeCondition implements Condition {

	    @Override
	    public boolean matches(ConditionContext conditionContext,
	    AnnotatedTypeMetadata metadata)
	    {
	        Map<String, Object> attributes = metadata.getAnnotationAttributes(LogServiceType.class.getName());
	        String type = (String) attributes.get("value");
	        System.out.println("value:"+type);
	        String enabledLogType = conditionContext.getEnvironment().getProperty("logType","StdOut");
	        System.out.println("enabledLogType:"+enabledLogType);

	        return (enabledLogType != null && type != null && enabledLogType.equalsIgnoreCase(type));
	    }
}

2.5  創建自動配置類

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

import com.dongguangming.annotation.LogServiceType;
import com.dongguangming.service.LogService;
import com.dongguangming.service.impl.FileLogServiceImpl;
import com.dongguangming.service.impl.MysqlLogServiceImpl;
import com.dongguangming.service.impl.StdOutLogServiceImpl;

@Configuration
@ComponentScan
public class LogServiceAutoConfiguration
{
	@Bean
	@LogServiceType("STDOUT")
	@ConditionalOnMissingBean
    public LogService stdOutLogServiceImpl(){
        return new StdOutLogServiceImpl();
    }

    @Bean
    @LogServiceType("FILE")
    @ConditionalOnMissingBean
    public LogService fileLogServiceImpl(){
        return new FileLogServiceImpl();
    }
    
    @Bean
    @LogServiceType("MYSQL")
    @ConditionalOnMissingBean
    public LogService mysqlLogServiceImpl(){
        return new MysqlLogServiceImpl();
    }
}

2.6 創建配置性文件

在目錄 src/main/resources/下創建約定的配置文件,先建目錄 META-INF ,然後在其目錄下創建很重要的配置文件 spring.factories ,填入以下內容

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.dongguangming.autoconfigure.CustomAutoConfiguration,\
com.dongguangming.autoconfigure.LogServiceAutoConfiguration

特別注意,該目錄結構是約定好的,目錄名必須這樣命名,屬性配置文件名也必須這樣命名

沒辦法了,spring太強大了,直接給你定好約束了,不讓你瞎起名。

 

2.7   設置日誌類型

在項目啓動配置文件application.properties中設置日誌輸出類型,logType=File

然後運行主程序,效果如圖示

2.8  打包成jar

用maven構建工具生成jar,

然後把這個jar 安裝到maven倉庫中

2.9   引用自定義的starter

回到自定義項目的pom文件中,查看以下信息

然後新的項目配置依賴就可以引用了,像引用其他第三方starter一樣

 <!--官網自帶的start,很多-->
 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

<!--記得嗎,這是我剛纔自定義的starter-->
<dependency>
	<groupId>com.dongguangming</groupId>
	<artifactId>custom-logservice-spring-boot-starter</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</dependency>

然後通過配置文件設置日誌類型

#注意日誌輸出類型logtype有三種選擇:stdout, file, mysql,任選其一
logType=File

代碼中這樣使用

@Autowired
LogService logService;

logService.print("自定義日誌輸出"));

至此一個自定義starter的庫就結束了,如果可以,你可以把自定義的starter放置到各大maven公/私服倉庫中,這樣開發者就能引用你的依賴,不過官網已經寫了很多,我只是舉個例子,因爲發現很多羣里人都不清楚starter是做什麼的。

附全部代碼已上傳  https://github.com/dongguangming/springboot-custom-starter

 

參考:

0.    Creating Your Own Auto-configuration

https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-developing-auto-configuration

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章