Skip to main content

Reading Property Files in a Spring Framework in Java


Reading Property Files in a Spring Framework in Java


Of the many ways possible to achieve this in  a Java based Project, they can be classified into 2 categories based on the nature of configuration, namely Annotation Based and Xml based.

Annotation Based:


Use @PropertySource("classpath:fileName.properties")


@Configuration
@ComponentScan(basePackages = { "com.myproject.*" })
@PropertySource("classpath:dbConfig.properties")
public class GetDBConnection {

...
}


Read Property File Data using either Environment or @Value
  • METHOD 1 - Using Environment:

@Autowired
private Environment env;


String dbUrl = env.getProperty("db.url");
  • Using @Value

@Value("${db.url}") // To be bound with an attribute
private String dbUrl;


//To resolve ${} in @Value, use PropertySourcesPlaceholderConfigurer.

@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
        return new PropertySourcesPlaceholderConfigurer();
}



 XML Based:


Using Spring Utils: (To Be Updated)








Comments

Popular posts from this blog

Fixing Keycloak Error : MediaType not set on path , with response status 200

Using a custom endpoint with Media Type set as 'Application/Json'. When this endpoint is accessed, below error is returned. ERROR [org.keycloak.headers.DefaultSecurityHeadersProvider] (default task-16) MediaType not set on path /auth/realms/my-realm/broker/keycloak-oidc/token, with response status 200 06:31:08,489 ERROR [org.keycloak.services.error.KeycloakErrorHandler] (default task-16) Uncaught server error: javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error at org.keycloak.keycloak-services@10.0.2//org.keycloak.headers.DefaultSecurityHeadersProvider.addHeaders(DefaultSecurityHeadersProvider.java:71) at org.keycloak.keycloak-services@10.0.2//org.keycloak.services.filters.KeycloakSecurityHeadersFilter.filter(KeycloakSecurityHeadersFilter.java:36) The fix for this is to bind the blank response to Application/Json as follows: return Response.ok(json, MediaType.APPLICATION_JSON).build(); Do try it out, and let me know if it works. Do drop some comments is an...

Extracting Data from Twitter using Twitter API (in Python/Pycharm)

Pre-Requisites for this task: User should have a Twitter Account. Phone number should be linked with Twitter Account. ( Ways to Add Phone number in twitter ) Pycharm should be installed. Add Plugin named Tweepy in Pycharm. Obtain Twitter API Key from Twitter. How To Obtain Twitter API key: Go to apps.twitter.com and log in with your Twitter account. Click 'Create a new app' and fill the details. Callback URL is not mandatory. The system will generate an API key and an API secret. Generate an access token on 'Keys and Access Tokens' tab. Four keys will be generated. What exactly to write in Pycharm: import tweepy consumer_key = 'CONSUMER-KEY-FROM-TWITTER' consumer_secret = 'CONSUMER-SECRET-FROM-TWITTER' access_token = 'ACCESS-TOKEN-FROM-TWITTER' access_secret = 'ACCESS-SECRET-FROM-TWITTER' authentication = tweepy.OAuthHandler(consumer_key,consumer_secret) authentication.set_access_token(access_token, access_sec...

Keycloak empty User-Attribute causes NPE

Fixing NPE in Keycloak due to non-existent user attribute In Keycloak, an error might be encountered as below: ERROR [org.keycloak.services.error.KeycloakErrorHandler] (default task-1) Uncaught server error: java.lang.NullPointerException at org.keycloak.models.utils.KeycloakModelUtils.resolveAttribute(KeycloakModelUtils.java:414) at org.keycloak.models.utils.KeycloakModelUtils.resolveAttribute(KeycloakModelUtils.java:415) at org.keycloak.protocol.oidc.mappers.UserAttributeMapper.setClaim(UserAttributeMapper.java:93) at org.keycloak.protocol.oidc.mappers.UserAttributeMapper.setClaim(UserAttributeMapper.java:101) at org.keycloak.protocol.oidc.mappers.AbstractOIDCProtocolMapper.setClaim(AbstractOIDCProtocolMapper.java:117) at org.keycloak.protocol.oidc.mappers.AbstractOIDCProtocolMapper.setClaim(AbstractOIDCProtocolMapper.java:119) at org.keycloak.protocol.oidc.mappers.AbstractOIDCProtocolMapper.transformAccessToken(AbstractOIDCProtocolMapper.java:81) at org.keycloak.pro...