2 분 소요


SpringBoot Configuration

SpringBoot stores various configurations in application.properties.

While you can use application.properties, SpringBoot also supports application.yml, which allows you to express hierarchy for better readability.

Additionally, I want to briefly cover the @Value annotation, which is used to retrieve values from the configuration file application.yml.

Setting up application.yml

I have previously written about using application.yml, and I will write this in a similar manner.

However, aside from typical database settings or server port settings, I want to demonstrate how to retrieve specific values.

# application.yml
myspring:
  test:
  name: wool
  age: 20

myspringListTest: banana,orange,apple

Besides the basic settings, I have created test.name, test.age, etc. under the name myspring.

When accessing these values, simply append . to navigate to the child elements.

Writing Test Code

Let’s create code that can retrieve these values and test it.

For testing, I will create a ConfigurationAnnotationTests class and write a simple test.

Prepare the ConfigurationAnnotationTests class where we will write the test code.

ConfigurationAnnotationTest.java

package com.wool.springconf;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

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

    // Code will be written here

}

Let’s write the code step by step. Let’s retrieve the configuration values written in the application.yml file above.

// Above content omitted
@Value("${myspring.test.name}")
private String mySpringTestName; // a

@Value("${myspring.test.age}")
private int mySpringTestAge; // b

@Value("${myspringListTest}")
private String[] mySpringArray; // c

@Value("$#{'${myspringListTest}'.split(',')}")
private List<String> mySpringList;

The @Value annotation allows access to configuration values.

  • a) "${myspring.test.name}" means accessing test under myspring, then accessing name under it. This value is placed inside the @Value annotation, and the corresponding mySpringTestName stores wool as written in application.yml.
  • b) "${myspring.test.age}" similarly indicates accessing the age value in test under myspring, and stores that value in mySpringTestAge.
  • c,d) The myspringListTest value is slightly different from other values in application.yml - it’s bundled as myspringListTest: banana,orange,apple. It can be received in two ways:
    • c) Like String[] mySpringArray, you can assign it to a list and store values inside the list to check them one by one.
    • d) List<String> mySpringList can also receive it the same way.

Complete Test Code

    package com.wool.springconf;

    import org.junit.Assert;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;

    import java.util.List;

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

    	@Value("${myspring.test.name}")
    	private String mySpringTestName; // a

    	@Value("${myspring.test.age}")
    	private int mySpringTestAge; // b

    	@Value("${myspringListTest}")
    	private String[] mySpringArray; // c

    	@Value("$#{'${myspringListTest}'.split(',')}")
    	private List<String> mySpringList;

    	@Test
    	public void valueAnnotationTest(){

    		Assert.assertEquals(mySpringTestName, "wool");
    		Assert.assertEquals(mySpringTestAge, 20);

    		Assert.assertEquals(mySpringArray[0], "banana");
    		Assert.assertEquals(mySpringArray[1], "orange");
    		Assert.assertEquals(mySpringArray[2], "apple");

    		Assert.assertEquals(mySpringList.get(0), "banana");
    		Assert.assertEquals(mySpringList.get(1), "orange");
    		Assert.assertEquals(mySpringList.get(2), "apple");
    	}

    }

When you run the test code above, it retrieves the values from our application.yml and compares them with the expected values to verify they match.

If the values are retrieved correctly, you will see the test succeed.

댓글남기기