项目需求需要写一个starter,记录一下
start基本要用到的类
AutoConfiguration 将需要自动注入的类写在这里面
service 业务实现类
properties 配置类
AutoConfiguration 类
@Configuration
@ConditionalOnClass(value = {FtpService.class, ErpFtpClientFactory.class})
@EnableConfigurationProperties(FtpProperties.class)
public class FtpAutoConfiguration {
/**
* 配置类
*/
@Autowired
FtpProperties ftpProperties;
/**
* FtpService 主要功能类(上传/下载)
* @return
*/
@Bean
@ConditionalOnMissingBean(FtpService.class)
public FtpService ftpService() {
FtpService ftpService = new FtpService();
return ftpService;
}
/**
* 文件操作工具类
* @return
*/
@Bean
@ConditionalOnMissingBean(ErpFtpClientFactory.class)
public ErpFtpClientFactory erpFtpClientFactory() {
ErpFtpClientFactory erpFtpClientFactory = new ErpFtpClientFactory();
ErpFtpClientFactory.properties = ftpProperties;
return erpFtpClientFactory;
}
}
配置类
@Getter
@Setter
@ConfigurationProperties(prefix = "ftp.user")
//ftp.user 在业务端yml文件配置的前缀
public class FtpProperties {
private static final String DEFAULT_USER_NAME = "";
private static final String DEFAULT_PASSWORD = "";
private static final String DEFAULT_ADDRESS = "";
private static final String DEFAULT_POOT ="" ;
private static final String DEFAULT_ROOT = "";
private String userName = DEFAULT_USER_NAME;
private String password =DEFAULT_PASSWORD;
private String loginAddress=DEFAULT_ADDRESS;
private String port=DEFAULT_POOT;
private String root=DEFAULT_ROOT;
}
在service层写所需业务。
最后在resource文件夹下面建立META-INF 文件,添加名为spring.factories 文件填写
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.gw.ftp.config.AutoConfiguration