application.properties file in springboot giving null value while accessing from another file

1.1k Views Asked by At

I am trying to access values from application.properties file as below code in rest controller i am trying to create instance of testCon which will return con.

@RestController
public class TestCtrl {
     private Logger logger = LogManager.getLogger(getClass().getName());
@GetMapping("/test")
    public String test() {
    
        logger.error("in test ctrl");
        ResultSet rs = null;
        String finalJson = null;
        Connection con = null;
        DSConnection dsobj;
        try {
            testCon = new testCon("dbid");
            con = (java.sql.Connection) testCon.getConnection();
            logger.error("in test got con");
        } catch (Exception e1) {
            logger.error("error while getting con in welcome2 ctrl");
            e1.printStackTrace();
        }

In testCon class i have below code which reads value from applicatio.properties and creates connection with properties. Bu when i call above URL it shows null in logger like not able to read from prop file.

    
     @Value("${spring.datasource.jndi-name}")
     private String dsName;
     private static final Logger logger = LogManager.getLogger(testCon.class);

     public testCon(String dbinstance) throws Exception{
         String username = "";
         String password = "";
        ** logger.debug("title"+dsName);** > here it gives null

and application.properties file is

server.port=8090
spring.datasource.jndi-name=java:flo15d_any
0

There are 0 best solutions below