3

i would like to load @Configuration classes in an order. i have two configuration classes. i am having a requirement of loading my SampleProperties class before sampleconfiguration class.

I have tried the following annotations but it is not working as expected.

@AutoConfigureAfter(SampleProperties.class )
@AutoConfigureBefore(SampleConfiguration.class)

I have put my congiurations class in diff package in order to read configurations classes in an order.using @Import function, i am including my configuration classes into my application

My Main Class:

@Import({SampleProperties.class,SampleConfiguration.class,}) 
public class SampleApplication{  

    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }
}

My SampleProperties Class

@Configuration
@AutoConfigureBefore(SampleConfiguration.class) 
@ConfigurationProperties("demo")
@Data
public class SampleProperties  {

    private String team;
    private int teamSize;
    private String teamLeader;

}

My sampleconfiguration Class:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef="sampleEntityManager", 
                       transactionManagerRef="sampleTransactionManager",
                       basePackages= {"com.example.demo.repo"})
@AutoConfigureAfter(SampleProperties.class)                    
public class SampleConfiguration {


    @Autowired
    Environment env;



    @Bean(name="sampleDataSource")
    @Primary
    public DataSource dmsDataSource() { 

         // functions
        return null;
    }


    @Primary
    @Bean(name = "sampleEntityManager")
    public LocalContainerEntityManagerFactoryBean dmsEntityManagerFactory(EntityManagerFactoryBuilder builder) {
      // functions
        return null;
    }

    @Primary
    @Bean(name = "sampleTransactionManager")
    public PlatformTransactionManager dmsTransactionManager(@Qualifier("sampleEntityManager") EntityManagerFactory entityManagerFactory) {
      // functions
        return null;
    }
}

can anyone tell me what missing and where am making mistakes?

karthick S
  • 564
  • 1
  • 9
  • 18
  • i think there is an answer here: https://stackoverflow.com/questions/44314418/order-of-configuration-in-springboot – Nishant Lakhara Oct 24 '19 at 09:34
  • i tried that too. but not working – karthick S Oct 24 '19 at 09:35
  • 1
    Your `SampleProperties` isn't (or should) be an `@Configuration` object. It should be an `@Component`. Also the `@AutoConfigure` won't help as it isn't an auto configuration. Why is the ordering so important? – M. Deinum Oct 24 '19 at 09:53
  • Maybe you can try to use `@Order`. I think it can also apply to `@Configuration`, not just `@Component`. – LHCHIN Oct 24 '19 at 09:53

1 Answers1

1

I think you have to use @Order annotation.

@Component
@Order(1)
public class SampleProperties {
   // code
}

@Component
@Order(2)
public class SampleConfiguration {
    // code
}
Anish B.
  • 9,111
  • 3
  • 21
  • 41