0

My spring boot application can not get configuration parameters from application.yml file. My main class as following:

@SpringBootApplication(scanBasePackages={"com.test"})
public class Main {
    @Bean
    public Validator validator(){
        return new  org.springframework.validation.beanvalidation.CustomValidatorBean();
    }
    public static void main(String[] args) throws IOException {
        new SpringApplicationBuilder(Main.class)
        .properties("application.yml")
        .build()
        .run(args);
    }
}

My controller class as following:

@RestController
@RequestMapping("/test_traffic")
@Component
public class AnycastTrafficController {
    @Autowired
    TestService testService;

    @GetMapping("/test")
    public Object component() {
         return testService.getTraffic();
    }
}

My Service class as following:

@Service
public class TestService {
    @Autowired
    TestDao testDao;

    public Object getTraffic() {
        testDao.getTraff();
    }
}

My Dao class as following:

@Component
public class TestDao {

    @Autowired
    MyDBConfig mydbConfig;

    public DB getMyDBConfig () {
        DB db = new DB(mydbConfig.id, mydbConfig.key);
        return db;
    }
}

My Config class as following:

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "mydb")
public class MyDBConfig {

    public String id;
    public String key;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }
}

My application.yml (which located at /src/main/resources)as following:

server:
  port: 8003
  context-path: /

logging:
  level:
    ROOT: INFO
    org.springframework: INFO
    org.springframework.data: INFO
    com.alibaba: INFO
  file: log/

docserver:
  accessKeyId: 1111
  accessKeySecret: 2222

---

spring:
  profiles: dev
  application:
    name: test-application
mydb:
  id: 1111111
  key: 2222222

But when I started the Main class and request the url, it threw exception as following:

the id should not be empty.

that mean my Configuration class didn't get the configure data from yml file, so where I did wrong please. p.s(but the server port 8003 could be found by application). Thanks!

Jack
  • 5,540
  • 13
  • 65
  • 113
  • Where is the `application.yml` file? If it's in the resources directory (src/main/resources) spring boot should find it automatically without having to specify it. – Chris Thompson Dec 19 '18 at 00:03
  • yes, it is located at /src/main/resources, the server port 8003 could be found, but the mydb data could not be read. – Jack Dec 19 '18 at 00:18
  • when i comment all what you have in the application and i keep only the mydb and sub properties it works ! – Abder KRIMA Dec 19 '18 at 01:13
  • 1
    Are you setting the active profile? If not, Spring won't default to `dev` and will ignore that section https://stackoverflow.com/questions/37700352/setting-the-default-active-profile-in-spring-boot – Chris Thompson Dec 19 '18 at 05:35

1 Answers1

1

Your application.yml contains an invalid property option.

Instead of

spring:
  profiles: dev

you should use

spring:
  profiles:
    active: dev

After correcting this this, the configuration processor should work properly.

Szilárd Fodor
  • 354
  • 2
  • 9