Ravindra Ranwala
Ravindra Ranwala

Reputation: 21124

SpringBoot microservice How to set properties in application context using java configuration

I have a spring-boot microservice with Java based config. Now there is an auth token container, which I need to call to get the access token. That auth token library has a class like this. Notice that the class AuthServletContextListener comes from a third party jar file which I can not modify.

public class AuthServletContextListener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent arg0) {
        try {
            ServletContext e = arg0.getServletContext();
            Properties config = new Properties();
            this.addProp(config, e, "auth.token.url", "Token Service URL");
            this.addProp(config, e, "auth.system.username", "System Username");
            this.addProp(config, e, "cauth.system.password", "System Password");
            TokenContainer.init(config);
        } catch (IOException arg3) {
            arg3.printStackTrace();
        }

    }


    private void addProp(Properties config, ServletContext context, String propName, String descrip) {
        String propVal = (String) context.getAttribute(propName);
        if (StringUtils.isEmpty(propVal)) {
            propVal = context.getInitParameter(propName);
        }

        if (StringUtils.isNotEmpty(propVal)) {
            config.put(propName, propVal);
        } else {
            throw new RuntimeException("error: ");
        }
    }
}

The AuthContextListener has an annotation that automatically looks for an application startup event. This automatically starts the container correct settings if the above context params are included. I can then grab the token through that container like this:

TokenContainer.getSystemToken() 

This will initialize successfully if the above context params are included in the web.xml like so:

<context-param>
   <param-name>auth.system.username</param-name>
   <param-value>UserName</param-value>
</context-param> 

The same Application context configuration as above can be performed by creating a Spring bean with the following information:

<bean>
    <property name="attributes">
        <map>
            <entry key="auth.token.url" value="${auth.token.url}"/>
            <entry key="auth.system.username" value="${auth.system.username}"/>
            <entry key="auth.system.password" value="${auth.system.password}"/>
        </map>
    </property>
</bean>

My question is how can I achieve the same using Java based configuration in a latest spring boot application. All I have is application.yml file with the auth endpoint, username and password values. I tried using @Configuration bean but no luck. How can I set those three props in application context and make that listener start automatically for me.

Upvotes: 1

Views: 2486

Answers (2)

M. Deinum
M. Deinum

Reputation: 125212

Add the following to your application.properties

server.servlet.context-parameters.auth.token.url=<your-value>
server.servlet.context-parameters.auth.system.username=<your-value>
server.servlet.context-parameters.auth.system.password=<your-value>

This will expose the values as context parameters.

Upvotes: 1

e2rabi
e2rabi

Reputation: 4848

In your application.yml or application.properties add a set for your auth url :

#Example for application.properties
auth.token.url = http:\\...

Then in your configuration class configure your place holder so then you can read your properties values :

@Configuration
public class Config {
        @Bean
        public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
            PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
            propertySourcesPlaceholderConfigurer.setLocations(new ClassPathResource("application.properties"));//or application.yml
            return propertySourcesPlaceholderConfigurer;
        }

    }

Then in your AuthServletContextListener classes : add this

public class AuthServletContextListener implements ServletContextListener {

  @Value("${auth.token.url}")
  private String authUrl ;

    public void contextInitialized(ServletContextEvent arg0) {
        try {
            ServletContext e = arg0.getServletContext();
            Properties config = new Properties();
            this.addProp(config, e, "auth.token.url", authUrl);
            this.addProp(config, e, "auth.system.username", "System Username");
            this.addProp(config, e, "cauth.system.password", "System Password");
            TokenContainer.init(config);
        } catch (IOException arg3) {
            arg3.printStackTrace();
        }

    }


    private void addProp(Properties config, ServletContext context, String propName, String descrip) {
        String propVal = (String) context.getAttribute(propName);
        if (StringUtils.isEmpty(propVal)) {
            propVal = context.getInitParameter(propName);
        }

        if (StringUtils.isNotEmpty(propVal)) {
            config.put(propName, propVal);
        } else {
            throw new RuntimeException("error: ");
        }
    }
}

Upvotes: 1

Related Questions