-1

Here is my final class "Constants"

@Component
public final class Constants {

    @Value("${db2.schema}")
    private static String Schema;

    public static final String STUDENT_TABLE = Schema + ".Student";
}
 

I have db2.schema in my properties file :

db2.schema = ${DB2_SCHEMA}
DB2_SCHEMA = D5677ESB
Jakub Biały
  • 391
  • 2
  • 16
GKr297
  • 185
  • 2
  • 16
  • 1
    You can create a method, and use it in a static context. What have you tried, what worked and what did not, what was the error? – Bonatti Aug 05 '20 at 13:50
  • Thanks! I tried using @Value as shown above to get the value, but I am getting null as Schema.. What would be the best way.. I am new to springboot.. – GKr297 Aug 05 '20 at 13:54
  • [link](https://stackoverflow.com/questions/30528255/how-to-access-a-value-defined-in-the-application-properties-file-in-spring-boot) this may help – bhuwan Aug 05 '20 at 14:23

2 Answers2

-1

@Value can not be used with static fields

Anup
  • 23
  • 8
-1

You can use like: ... public class Constants {

public static final String NewOrder = "neworder";
public static final String POST = "POST";
public static final String CONTENT_TYPE = "Content-Type";
public static final String APPLICATION_TYPE = "application/json";
public static final String ACCEPT = "Accept";
public static final String CART_URL = PropsUtil.get("order.inquiry.search.insertCartDataURL");

} ... ** or check this code:** ... public String getPropValues() throws IOException {

    try {
        Properties prop = new Properties();
        String propFileName = "config.properties";

        inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);

        if (inputStream != null) {
            prop.load(inputStream);
        } else {
            throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
        }

        Date time = new Date(System.currentTimeMillis());

        // get the property value and print it out
        String user = prop.getProperty("user");
        String company1 = prop.getProperty("company1");
        String company2 = prop.getProperty("company2");
        String company3 = prop.getProperty("company3");

        result = "Company List = " + company1 + ", " + company2 + ", " + company3;
        System.out.println(result + "\nProgram Ran on " + time + " by user=" + user);
    } catch (Exception e) {
        System.out.println("Exception: " + e);
    } finally {
        inputStream.close();
    }
    return result;
} ...