1

I have the below configuration class definition in java springboot. However, it fails for reference to property values.

@org.springframework.context.annotation.Configuration
public class HbaseConfig {

@Value("${keytab.user.name}")
private String username;

@Value("${keytab.path}")
private String keytabpath;

@Bean
public Connection getHbaseConnect() throws IOException {
    Configuration conf = HBaseConfiguration.create();
    Connection connection = ConnectionFactory.createConnection(conf); 
    UserGroupInformation.setConfiguration(conf);

    System.out.println("hbase connect..is connection closed..." + connection.isClosed());
    UserGroupInformation.loginUserFromKeytabAndReturnUGI(username, keytabpath);
    return connection;
}

@Bean
public Admin getHbaseAdmin(Connection connection) throws IOException{
    Admin admin = connection.getAdmin();
    return admin;
}

}

application.properties

 keytab.user.name="username"
 keytab.path="pathtokeytab"

To put it simple, I need the above keytab username and path read from a property file in my HbaseConfig class.

Siva
  • 25
  • 9

3 Answers3

1

Can you please try out the below method,

  @ConfigurationProperties(prefix = "keytab")
  public class KeyTabConfig {

        private String username;

        private String path;

        public String getUsername(){ return this.username; }
        public String getPath(){ return this.path;}
        }

Habseconfig class as follows

@Configuration
@EnableConfigurationProperties({ KeyTabConfig.class})
public class HbaseConfig {

              @Bean
              public Connection getHbaseConnect(KeyTabConfig keyTabConfig) throws IOException {
                   Configuration conf = HBaseConfiguration.create();
                   Connection connection = 
                   ConnectionFactory.createConnection(conf); 
                   UserGroupInformation.setConfiguration(conf);

                   System.out.println("hbase connect..is connection closed..." + 
                   connection.isClosed());
                   UserGroupInformation.loginUserFromKeytabAndReturnUGI(keyTabConfig.getUsername(), keyTabConfig.getPath());
                   return connection;
                 }

             @Bean
             public Admin getHbaseAdmin(Connection connection) throws IOException{
               Admin admin = connection.getAdmin();
               return admin;
              }

          }

application.properties file as

    keytab.username=uname
    keytab.path=path
Midhun Mohan
  • 649
  • 1
  • 5
  • 13
  • I tried this but still getting the values as null. I assumed the below code refers to the object of the keytabconfig class: `UserGroupInformation.loginUserFromKeytabAndReturnUGI(keyTabConfig.getUsername(), keyTabConfig.getPath());` hence i created an autowired object of KeyTabConfig class in HbaseConfig class. – Siva Sep 17 '19 at 00:36
  • @Siva even if you didn't autowired the DI will work in this case. KeyTabConfig will be injected as desired – Midhun Mohan Sep 18 '19 at 05:06
  • Thanks @MidhunMohan. I tried it though, it was coming as null. – Siva Sep 18 '19 at 06:34
  • https://gitlab.com/midhunmohan/environment-read-demo A sample of the above you can find it here @Siva – Midhun Mohan Sep 18 '19 at 09:07
0
  1. Make sure your property file name should application.properties and location should in

src/main/resources/application.properties

  1. used only @Configuration at the class level and also use @PropertySource to define the location of our properties file.@PropertySource("classpath:configprops.properties") if your property file name is different.

Otherwise, Spring uses the default location (classpath:application.properties).

MangduYogii
  • 935
  • 10
  • 24
  • @MangduYogil yes the property filename is as application.properties and in the directory as you mentioned. – Siva Sep 16 '19 at 06:30
  • then try with @PropertySource("classpath:application.properties") – MangduYogii Sep 16 '19 at 06:35
  • I am reading other property values form the same application.properties but it is read in my Repository class and that works fine. – Siva Sep 16 '19 at 06:39
-1

The actual processing of @Value annotation is performed by BeanPostProcessor and so we cannot use @Value within BeanPostProcessor types.

Examples: https://www.concretepage.com/spring-5/spring-value#placeholder

Link: How exactly does the Spring BeanPostProcessor work?

Join Free
  • 89
  • 3
  • 1
    I downvoted this answer because you are confusing the main question, let he adds more details so that you can answer properly. – Jonathan JOhx Sep 16 '19 at 05:35