Question: I have a Spring boot application, I have multiple beans configured in main class and I also have different profiles set for different environments. Please see the code below:

```
private static String propertiesFilePath;

    public static void main(String[] args) {
        LOGGER.info("Starting app..!!");
        SpringApplication.run(Application.class, args);
    }

    @Bean
    @Profile("local")
    public  String localBean() {
        propertiesFilePath = "application-local.properties";
        return propertiesFilePath;
    }

    @Bean
    public static CommandLineRunner init() {
        return (args) -> {
            try {
                LOGGER.info("Starting ..");
                CommonUtil.initializeCacheManager(propertiesFilePath);
                } catch (Exception e) {
                LOGGER.info("CommonUtil.initializeCacheManager :" + e.getMessage());
            }
        };

    }

    @Bean
    public BroadcastHandler createBroadcastHandler() {
        return new BroadcastHandler();
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

```

In this code, other beans are getting called first then the bean annotated with @Profile, I can see in logs below line:

> 
> The following profiles are `active: local`
> 
> 
> 

Since Spring container is trying to initialize other beans so without property getting initialized first, the application is failing to start.

Is there any reason why bean with @Profile is not getting called and how can I fix it?

Answer: 
Spring boot doesn't work with profiles like you've presented in the code snippet in the question.

It resolves all the profiles resolution automatically and loads corresponding beans (annotated with `@Profle` annotation):

When you run the application, add `--spring.profiles.active=local` to the command that executes the java application.

This alone will instruct spring boot to look for `application-local.properties` (or yaml) and will resolve all the properties from there just like it works with usual `application.properties` processed by default.

The properties files by default will be resolved if you place it into `src/main/resources` or `src/main/resources/config` folders.

There is also a way to enable profiles automatically from java code (with Environment Post Processors) but it's beyond the scope of the question.
Try adding 

```
spring.profiles.active=local

```

in your application.properties file.