设为首页 加入收藏

TOP

关于自定义程序打包成jar包,并读取配置(二)
2023-08-06 07:49:49 】 浏览:72
Tags:关于自 程序打 包成 jar
e
>provided</scope> </dependency> <!--mysql数据库驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> <scope>runtime</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build> </project>

 

  在config包里面,我们要建立几个配置文件:

UserConfiguration.java

@Slf4j
@Configuration
@ConditionalOnExpression("${enabled:true}")
@EnableConfigurationProperties(UserConfiguration .class)
public class UserConfiguration {

    @Bean
    @Primary
    public UserConfig getConfigValue(UserProperties properties){
        if (properties.getLog()){
            log.info("数据库中转服务API组件 ——> 开启组件");
        }
        return new UserConfig()
                .setOtherDataSourcesMap(properties.getOtherDataSourcesMap())
                .setLog(properties.getLog());
    }
}

UserConfig.java

@Data
@Accessors(chain = true)
public class UserConfig implements Serializable {
    private Map<String,Map<String,Map<String,Object>>> otherDataSourcesMap;

    /** 是否打印操作日志 */
    private Boolean log = true;
}
UserProperties.java
@Data
@ConfigurationProperties(prefix = "User")
public class UserProperties implements Serializable {

    private Map<String,Map<String,Map<String,Object>>> otherDataSourcesMap;

    /** 是否开启 */
    boolean enabled = true;

    /** 是否打印操作日志 */
    private Boolean log = false;
}
请注意:这里的@ConfigurationProperties 注解里面的 prefix 参数指的是在配置文件中你自己定义的标识符
UserBeanInject.java
public class UserBeanInject {

    /**
     * 注入配置Bean
     *
     * @param config 配置对象
     */
    @Autowired(required = false)
    public void setConfig(UserConfig config){
        UserSpi.setConfig(config);
    }
}

 当然,还有重要的一步,那就是在resources文件夹下创建一个Spring自动装配的约定文件,使得我们这几个java配置文件能够生效

 spring.factories

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  自己定义的包路径.configuration.UserConfiguration, \
  自己定义的包路径.configuration.UserBeanInject

 然后,我们要暴露这个接口

UserSpi.Java
@Slf4j
public class UserSpi{
    public volatile static UserConfig config;

    public static void setConfig(UserConfig config){
        UserSpi.config = config;
        if (config.getLog()){
            log.info("数据库中转服务API组件 ——> 打印配置信息\n", JSONUtil.toJsonStr(UserSpi.config));
        }
    }

    // =================== 获取Api 相关 ===================

    public static IUserService api = new UserServiceImpl();
}

这样基本上完成jar自动装配外部配置文件的配置,业务逻辑什么的看个人习惯自己去编写

 

感言

  • 从数据库里面读取配置信息方便时挺方便的,但是在高并发与高请求的项目中并不适用,
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Java中面向对象详解 下一篇RabbitMQ的安装、配置和实战

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目