如何在Spring中将端口和主机放在属性文件中?

时间:2018-12-17 14:03:19

标签: spring spring-boot

我有这个网址

private static final String PRODUCTS_URL = "http://localhost:3007/catalog/products/";

这种方法:

public JSONObject getProductByIdFromMicroservice(String id) throws IOException, JSONException {
        return getProductsFromProductMicroservice(PRODUCTS_URL + id);
    }

    public JSONObject getProductsFromProductMicroservice(String url) throws IOException, JSONException {
        CloseableHttpClient productClient = HttpClients.createDefault();
        HttpGet getProducts = new HttpGet(url);

        CloseableHttpResponse microserviceResponse = productClient.execute(getProducts);
        HttpEntity entity = microserviceResponse.getEntity();
        BufferedReader br = new BufferedReader(new InputStreamReader((entity.getContent())));
        StringBuilder sb = new StringBuilder();

        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

        br.close();

        System.out.println(sb.toString());
        JSONObject obj = new JSONObject(sb.toString());
        System.out.println(obj);

        return obj;
    }

我想将端口和主机放在单独的属性文件中。我已经看过使用属性和yml文件的示例。但是我不明白在创建类的实例时如何使用该端口使用我的方法,我将在属性文件中进行说明。你能告诉吗?

3 个答案:

答案 0 :(得分:1)

您可以将属性放在资源目录中的属性文件中,例如

if($('#section1').hasClass('active')){
    $('.sidePannel').css('display','none');
}

else{
    $('.sidePannel').css('display','block');
}

并将PRODUCTS_URL="http://localhost:3007/catalog/products/" 添加到您的主班(@PropertySource("YOUR_RESOURCE_FILE_HERE.properties")

Application.java

,然后使用@SpringBootApplication @PropertySource("products.properties") public class Application {...} 加载它:

@Value("${YOUR_PROPERTY_NAME}")

选中此tutorial

答案 1 :(得分:0)

这就是我的方法:

配置文件

#Database Server Properties
dbUrl=jdbc:sqlserver://localhost:1433;database=Something;
dbUser=sa
dbPassword=SomePassword

然后我用这个注释配置类:

@PropertySource("file:${ENV_VARIABLE_TO_PATH}/config.properties")

然后自动连接该字段:

@Autowired
private Environment environment;

然后创建数据源:

@Bean
public DataSource dataSource() 
{
    HikariDataSource dataSource = new HikariDataSource();

    try
    {
        dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        dataSource.setConnectionTestQuery("SELECT 1");
        dataSource.setMaximumPoolSize(100);

        String dbUrl = environment.getProperty("dbUrl");

        if (dbUrl != null)
        {
            dataSource.setJdbcUrl(dbUrl);
        }
        else
        {
            throw new PropertyNotFoundException("The dbUrl property is missing from the config file!");
        }

        String dbUser = environment.getProperty("dbUser");

        if (dbUser != null)
        {
            dataSource.setUsername(dbUser);
        }
        else
        {
            throw new PropertyNotFoundException("The dbUser property is missing from the config file!");
        }

        String dbPassword = environment.getProperty("dbPassword");

        if (dbPassword != null)
        {
            dataSource.setPassword(dbPassword);
        }
        else
        {
            throw new PropertyNotFoundException("The dbPassword property is missing from the config file!");
        }

        logger.debug("Successfully initialized datasource");
    }
    catch (PropertyNotFoundException ex) 
    {
        logger.fatal("Error initializing datasource : " + ex.getMessage());
    }

    return dataSource;
}

我知道这不完全是您的情况,但是也许您可以从此代码中找到适合您特定需求的灵感?

答案 2 :(得分:0)

这里提到的其他答案是使用@PropertySource批注指定配置文件的路径。同样,如果这是测试代码(单元/集成),则还可以使用另一个注释@TestPropertySource

这样,我们可以定义优先于项目中使用的其他任何源的配置源。

参见此处:https://www.baeldung.com/spring-test-property-source