Showing posts with the label autowiring

Advanced wiring in Spring Part-2

In the last article we have seen that how we can configure beans based on certain environments.This is quite useful features spring have provided.Sometimes we need more granular control over the bean autowiring in spring, such as we need certain beans that needs to be autowired or injected on some complex condition (which is something more than certain environment). for e.g. What if we need certain bean needs to be injected if some other bean have specific property value. or inject an bean only if some other class is found on class-path. Here Spring will help you to make such a configuration using @Conditional annotation. Suppose we want the bean OperationUser to be created if the property " department= Operations " is set in your properties file. @Bean @Conditional(OpsDepartmentCondition.class) public OperationsUser operationsUser(){ OpeartionsUser opsUser = new OperationsUser(); return opsUser; } We will take a look at how OpsDepartmentCondition will

Advanced wiring in Spring

Many times we face situation in which we want certain things configured as per the environment or on certain complex condition. Spring provides great features in easing the developer job for this use-case. We will look at such a scenario along with solution spring provides in this case. Environments and Profiles: One of the most challenging part of any application is to transitioning of the software into different environments (DEV, QA, STG, PROD). Certain choices that we make in DEV environments are not suitable when we run the same setup on PROD environment. Consider basic use of using different database as per the deployment environment. for DEV we might setup a datasource something like below: @Bean(destroyMethod = "shutdown") public DataSource dataSource() { return new EmbeddedDatabaseBuilder() .addScript("classpath:devCreate.sql") .addScript("classpath:devInsert.sql") .build(); } but when we will go on PROD environment, this c