智慧交通 App 开发如何提升城市交通管理与用户出行体验
642
2023-07-06
springboot自定义stater启动流程
springboot启动时自动加载application.properties或者application.yml,如何定义自己的配置让springboot自动识别:
首先我们新建一个maven工程打包方式选择jar,然后引入所需的包
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
然后是配置读取
@ConfigurationProperties(prefix = "xxl.job.executor")
@Data
public class XxlProperties {
private String adminAddresses;
private String appName;
private String ip;
private int port;
private String accessToken;
private String logPath;
private int logRetentionDays;
private String basePackages;
}
将读取到的配置注入到bean中,然后加载到spring bean容器中
@Configuration
@EnableConfigurationProperties(XxlProperties.class)
public class XxlAutoConfiguration {
private Logger logger = LoggerFactory.getLogger(XxlAutoConfiguration.class);
@Resource
private XxlProperties xxlProperties;
@Bean(initMethod = "start", destroyMethod = "destroy")
public XxlJobSpringExecutor xxlJobExecutor() {
logger.info(">>>>>>>>>>> xxl-job config init.");
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
xxlJobSpringExecutor.setAdminAddresses(xxlProperties.getAdminAddresses());
xxlJobSpringExecutor.setAppName(xxlProperties.getAppName());
xxlJobSpringExecutor.setIp(xxlProperties.getIp());
xxlJobSpringExecutor.setPort(xxlProperties.getPort());
xxlJobSpringExecutor.setAccessToken(xxlProperties.getAccessToken());
xxlJobSpringExecutor.setLogPath(xxlProperties.getLogPath());
xxlJobSpringExecutor.setLogRetentionDays(xxlProperties.getLogRetentionDays());
return xxlJobSpringExecutor;
}
}
最后在resources目录下添加META-INF文件夹添加spring.factories文件,文件内容为指定要初始化springbean的类全路径
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.ruobbo.xxl.XxlAutoConfiguration
使用过程中引入包
然后添加配置到application.properties或者application.yml
总结
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~