Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Getting Started

Is there any possibility to override the base url on SB service, but without using application.properties, from code? TY

I

1 Answer

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

Could you please explain exactly why do you need it, i.e. provide specific goal that you are trying to achieve?

The point of properties is to make initial setup that is stable during the lifecycle of application.

From the way I see that how application work. We always have some properties/configuration that we set up in the beginning and live with them in application lifecycle.

Because your question is so generic ... all I can adivise is to try to read this exhaustive documentation about properties

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

I honestly don't know what else to tell you.

I change the properties only in Unit Testing, that is for example can be found here:

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html

There you have to change a lot of stuff, for example database locations, configuration classes and etc.

Again, if you tell what exactly you are trying to achieve, I can try to help you, but other than that, I think short answer to your question:

"No we can't change properties, except in properties file, in @Configuration files and when testing using @SpringBootTest. We can't change properties during life-cycle of App. If one wants to change something, he has to re-run the application".

Changing properties in @Component classes, like Repositories or @Controller-s, I thinks is impossible.

Hi Alexander thanks for replaying me! Let me explain the scenario, I have got an application.yml file where I have the following property for some service for example:

os: url: service://order-service/1/${cf.environment:development}

Here, I want to get rid of this link, for example use http://order-service.development instead of that 1, the url will be gotten from code!

I need to do this without using application.yml file, from code must be

Regards!

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

One thing you have to tell me:

How does your service works, I mean could you provide actual Java code @Controller, @Service, or other class where you actually want to change property,

Where and how do you want to override service ?

My question would be : if you want to re-write the property anyway, why do you put it in properties file?

Is that because you use environmental variable ${cf.environment:development} ?

All I could find on the web was that link

http://stackoverflow.com/questions/35341684/changing-spring-boot-properties-programmatically

So basically person there is Autowiring Environment and refreshing that, although it looks like he does it in Testing, and I don't know how dangerous it would be to do it not in testing.

That is why I need your actual Java code, I cannot say without it what are you trying to achieve and in which class, on which layer of App?

How about changing the property directly in class, possible example would be:

@Controller
public class SomeController{

    @Autowired
    private Environment env;

    @RequestMapping("/") 
    public home() {
       String somePropertyThatIsChanging;
       if (someConditionIsMet) {
             somePropertyThatIsChanging = env.getProperty("someProperty") + oneString;
       } else {
            somePropertyThatIsChanging = env.getProperty("someProperty") + anotherString;
       }
    }
}

In Spring Boot 1.4 one can do the following (example from my integration REST tests)

@RunWith(SpringRunner.class)
@SpringBootTest
public class RecipeRestIntegrationTest {

    // will be something like "/api/v1":
    // is set from properties file
    @Value("${spring.data.rest.base-path}")
    private String BASE_URL;

    @Test
    public void test() throws Exception {

       String somePropertyThatIsChanging;
       if (someConditionIsMet) {
             somePropertyThatIsChanging =  BASE_URL + oneString;
       } else {
            somePropertyThatIsChanging = BASE_URL + anotherString;
       }
    }

}

But again, without knowing your actual end goal, I cannot help.

If you cannot share project information, I can understand, but then at least try to provide more details, or better create example project that look like yours, so that I can help.