NutzCN Logo
问答 nutzboot 如何获取 properties 配置信息
发布于 1817天前 作者 Hamming 1467 次浏览 复制 上一个帖子 下一个帖子
标签:

nutzboot 如何获取 properties 配置信息

10 回复
@Inject
PropertiesProxy conf;

demo里面有

springboot 配置文件 可以酱紫加载

package com.ruoyi.framework.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 读取代码生成相关配置
 * 
 * @author ruoyi
 */
@Component
@ConfigurationProperties(prefix = "gen")
public class GenConfig
{
    /** 作者 */
    public static String author;
    /** 生成包路径 */
    public static String packageName;
    /** 自动去除表前缀,默认是true */
    public static String autoRemovePre;
    /** 表前缀(类名不会包含表前缀) */
    public static String tablePrefix;

    public static String getAuthor()
    {
        return author;
    }

    public void setAuthor(String author)
    {
        GenConfig.author = author;
    }

    public static String getPackageName()
    {
        return packageName;
    }

    public void setPackageName(String packageName)
    {
        GenConfig.packageName = packageName;
    }

    public static String getAutoRemovePre()
    {
        return autoRemovePre;
    }

    public void setAutoRemovePre(String autoRemovePre)
    {
        GenConfig.autoRemovePre = autoRemovePre;
    }

    public static String getTablePrefix()
    {
        return tablePrefix;
    }

    public void setTablePrefix(String tablePrefix)
    {
        GenConfig.tablePrefix = tablePrefix;
    }

    @Override
    public String toString()
    {
        return "GenConfig [getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString()
                + "]";
    }
}

# 代码生成
gen: 
  # 作者
  author: ruoyi
  # 默认生成包路径 system 需改成自己的模块名称 如 system monitor tool
  packageName: com.ruoyi.project.system
  # 自动去除表前缀,默认是true
  autoRemovePre: true
  # 表前缀(类名不会包含表前缀)
  tablePrefix: sys_

那么 nutz 怎么 配置 简单呢

package io.nutz.nutzsite.common.config;

import org.nutz.ioc.loader.annotation.Inject;

/**
 * 读取代码生成相关配置
 */
public class GenConfig {
    /**
     * application
     * 作者
     */
    @Inject("java:application.get('author')")
    public static String author;
    /**
     * 生成包路径
     */
    @Inject("java:application.get('packageName')")
    public static String packageName;
    /**
     * 自动去除表前缀,默认是true
     */
    @Inject("java:application.get('autoRemovePre')")
    public static String autoRemovePre;
    /**
     * 表前缀(类名不会包含表前缀)
     */
    @Inject("java:application.get('tablePrefix')")
    public static String tablePrefix;

    public static String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        GenConfig.author = author;
    }

    public static String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        GenConfig.packageName = packageName;
    }

    public static String getAutoRemovePre() {
        return autoRemovePre;
    }

    public void setAutoRemovePre(String autoRemovePre) {
        GenConfig.autoRemovePre = autoRemovePre;
    }

    public static String getTablePrefix() {
        return tablePrefix;
    }

    public void setTablePrefix(String tablePrefix) {
        GenConfig.tablePrefix = tablePrefix;
    }

    @Override
    public String toString() {
        return "GenConfig [getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString()
                + "]";
    }
}

酱紫 应该可以了吧

conf.make(GenConfig.class.getName(),"abc.")

nutz ioc不支持对static属性的注入

或者用注解@IocBean(factory="$conf#make", args={"xxx.zzz.yyy.GenConfig","abc."}),不过基本上没人这样写

还是 这样 加载 配置文件简单

public class Main {
 
    private static String driver;
    private static String url;
    private static String username;
    private static String password;
    static
    {
        try {
            //获取当前类加载器
            ClassLoader classLoader=Main.class.getClassLoader();
            //通过当前累加载器方法获得 文件db.properties的一个输入流
            InputStream is=classLoader.getResourceAsStream("db.properties");
            //创建一个Properties 对象
            Properties properties=new Properties();
            //加载输入流
            properties.load(is);
 
            driver=properties.getProperty("driver");
            url=properties.getProperty("url");
            username=properties.getProperty("username");
            password=properties.getProperty("password");
 
        }
        catch (IOException e) {
            e.printStackTrace();
        }
 
 
    }
    public static void main(String[] args){
        System.out.println(driver);
        System.out.println(url);
        System.out.println(username);
        System.out.println(password);
    }
}

喜欢用静态属性???

添加回复
请先登陆
回到顶部