0

I'm new to spring and spring-roo. I try to build an application and read some key value pairs from a properties file.

I created a myconfig.properties file and saved it to src/main/resources/META-INF/spring/. The content of the file is:

## My Configuration settings
myconfig.url=https://1.2.3.4/api.php
myconfig.username=user1
myconfig.password=password1

Now I added a bean configuration into appilcationContext.xml in the same directory

  <bean id="MyConfig" class="com.test.client.MyClient">
    <property name="url" value="${myconfig.url}" />
    <property name="username" value="${myconfig.username}" />
    <property name="password" value="${myconfig.password}" />
</bean>

In my class file I tried to access the values, but I get an File not found error

     package com.test.client;

     import org.springframework.context.support.AbstractApplicationContext;
     import org.springframework.context.support.ClassPathXmlApplicationContext;
     import org.springframework.roo.addon.javabean.RooJavaBean;
     import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
     import org.springframework.roo.addon.tostring.RooToString;
     import org.springframework.util.LinkedMultiValueMap;
     import org.springframework.util.MultiValueMap;
     import org.springframework.web.client.RestTemplate;

     @RooJavaBean
     @RooToString
     @RooJpaActiveRecord
     public class MyClient {

         private String url;

         private String username;

         private String password;


         public static String login()
 {
    // Construct the spring application context
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

    MyClient config = (MyClient) context.getBean("MyConfig");

    // Register hook to shutdown Spring gracefully
    // See http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-factory-shutdown
    context.registerShutdownHook();

    String token = null;

    final String url = config.getUrl();
    final String username = config.getUsername();
    final String password = config.getPassword();

  ....

Thanks for any help!

1 Answer 1

1

Try

AbstractApplicationContext context = new ClassPathXmlApplicationContext("classpath*:META-INF/spring/applicationContext.xml");

However the best practice is to implement interface ApplicationContextAware.

Stefano

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.