我們用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
1. How to custom Spring Boot Starter https://medium.com/@alexeynovikov_89393/how-to-write-your-own-spring-boot-starters-566ce5992954
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/181216.html
標籤:Java
上一篇:JAVA自學筆記(3)
