Cuba获取属性文件中的配置

时间:2023-03-09 03:50:53
Cuba获取属性文件中的配置

  最直接的办法是,使用AppContext.getProperty("cuba.trustedClientPassword");

  可以获取到系统中的web模块下的wep-app.properties和 core模块下的app.properties文件中的配置信息(core是业务,web是UI),但是不推荐这么使用。

  推荐使用Config代替AppContext

  在web模块中使用WebAuthConfig
  @Inject
  private WebAuthConfig webAuthConfig;

  获取:

  webAuthConfig.getTrustedClientPassword();

  在core模块中使用ServerConfig
  @Inject
  private ServerConfig serverConfig;

  获取:

  serverConfig.getTrustedClientPassword();

  这两个类有同样的这个方法

@Source(type = SourceType.APP)
public interface WebAuthConfig extends Config {
@Property("cuba.trustedClientPassword")
@DefaultString("")
String getTrustedClientPassword();
} @Source(type = SourceType.APP)
public interface ServerConfig extends Config {
@Property("cuba.trustedClientPassword")
@DefaultString("")
String getTrustedClientPassword();
}

  而cuba.trustedClientPassword = **********,这个配置,是配置在web-app.properties里的。ServerConfig也是可以获取到的。

  注意:这种使用方法只能用在Bean中,而不能在普通的jaca类里,因为要使用到注解,而只有Bean才能被容器管理,才可以使用注解。

新建属性

  上面的两个属性,都是cuba系统自带的属性,那两个类也是写在jar文件中的。如果是自己新增的需要,需要自定义一个Config类,继承Config类就可以。

  如下:

//定义属性类类
@Source(type= SourceType.DATABASE)
public interface CAConfig extends Config {
@Property("ucenter.CA.pdf.sign.enable")
@DefaultBoolean(false)
Boolean getCAPDFSignEnable();
} //调用时
@Inject
private CAConfig caConfig; //获取
caConfig.getCAPDFSignEnable()

在运行时修改属性

  刚开始把属性获取到以后,直接放到了全局静态变量里了
    public static final String CUBA_TRUSTED_CLIENT_PASSWORD = AppContext.getProperty("cuba.trustedClientPassword");

  但是这样的坏处是:如果在项目运行时修改了属性,就必须重启系统才能生效,这明显是不好的。所以不应该使用上面的方法,而之前的办法在项目运行是进行修改,也会实时得获取到。

  那么怎么修改呢?

  使用【管理员】登陆,在【管理】-【应用程序属性】下可以搜索到:

Cuba获取属性文件中的配置

  编辑:

Cuba获取属性文件中的配置