简体   繁体   中英

Spring Boot is not loading the properties from a YML file

My test file is :

@ActiveProfiles("test")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class MyApplicationTest {
    @InjectMocks
    @Autowired
    protected MyController myController;

} 

My Controller file is:

@RestController
@RequestMapping(value="${app.base-path}")  // Unable to load this property
public class MyController {

}

I have tried @TestPropertySource but did not work. The SpringBootApplication works fine. This is not the case with @Value annotation. Fields decorated with @Value annotation works just fine.

Did you name your .yml file as application.yml and put it on the src/main/resources or src/test/resources ?

Placing the application.yml file on those folders will automatically add it to the classpath so you can access this file properties at runtime.

By default, Spring Boot will try to load application.yml and application-{profilename}.yml available on classpath, so you could try with application-test.yml (it will eliminate the problem with defining profiles in one file - maybe it causes your problem).

Also, you could try with bootstrap.yml , which is loaded before application.yml .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM