getProperty dbType return enabledDBType null enabledDBType equalsIgnoreCase

Getproperty dbtype return enableddbtype null

This preview shows page 43 - 47 out of 65 pages.

getProperty("dbType");return(enabledDBType!=null&&enabledDBType.equalsIgnoreCase("MONGODB"));}}Now we can configure bothJdbcUserDAOandMongoUserDAObeans conditionally using@Conditionalas follows:@Configurationpublic classAppConfig{@Bean@Conditional(MySQLDatabaseTypeCondition.class)publicUserDAOjdbcUserDAO(){return newJdbcUserDAO();}@Bean@Conditional(MongoDBDatabaseTypeCondition.class)publicUserDAOmongoUserDAO(){
Background image
Chapter 3: SpringBoot Behind the scenes37return newMongoUserDAO();}}Now if we run the application likejava -jar myapp.jar -DdbType=MYSQLthen onlyJdbcUser-DAObean will be registered.But if you set the System property like-DdbType=MONGODBthen onlyMongoUserDAObeanwill be registered.Now that we have seen how to conditionally register a bean based on System Property.Using @Conditional based on Presence/Absence of a Java ClassSuppose we want to registerMongoUserDAObean only when MongoDB java driver class“com.mongodb.Server”is available on classpath, if not we want to registerJdbcUserDAObean.To accomplish that we can create Conditions to check the presence or absence of MongoDB driverclass“com.mongodb.Server”as follows:public classMongoDriverPresentsConditionimplementsCondition{@Overridepublicbooleanmatches(ConditionContext conditionContext,AnnotatedTypeMetadata metadata){try{Class.forName("com.mongodb.Server");return true;}catch(ClassNotFoundException e) {return false;}}}public classMongoDriverNotPresentsConditionimplementsCondition{@Overridepublicbooleanmatches(ConditionContext conditionContext,AnnotatedTypeMetadata metadata){try{Class.forName("com.mongodb.Server");return false;
Background image
Chapter 3: SpringBoot Behind the scenes38}catch(ClassNotFoundException e) {return true;}}}We just have seen how to register beans conditionally based on presence/absence of a class inclasspath.Using @Conditional based on Configured Spring BeansWhat if we want to registerMongoUserDAObean only if no other Spring bean of typeUserDAOis already registered.We can create a Condition to check if there is any existing bean of a certain type as follows:public classUserDAOBeanNotPresentsConditionimplementsCondition{@Overridepublicbooleanmatches(ConditionContext conditionContext,AnnotatedTypeMetadata metadata){UserDAO userDAO=conditionContext.getBeanFactory().getBean(UserDAO.clas\s);return(userDAO==null);}}Using @Conditional based on Properties ConfigurationWhat if we want to registerMongoUserDAObean only if propertyapp.dbType=MONGOis set inproperties placeholder configuration file?We can implement that Condition as follows:
Background image
Chapter 3: SpringBoot Behind the scenes39public classMongoDbTypePropertyConditionimplementsCondition{@Overridepublicbooleanmatches(ConditionContext conditionContext,AnnotatedTypeMetadata metadata){String dbType=conditionContext.getEnvironment().getProperty("app.dbType");return"MONGO".equalsIgnoreCase(dbType);}}We have just seen how to implement various types of Conditions.
Background image
Image of page 47

You've reached the end of your free preview.

Want to read all 65 pages?

  • Spring '14
  • ThomasC.Hartrum
  • Spring Framework, SpringBoot

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture