Spring @ConditionalOnWebApplication and @ConditionalOnNotWebApplication Examples

Condition On Web And Not Web The Spring @ConditionalOnWebApplication and @ConditionalOnNotWebApplication annotations let configuration be included depending on whether the application is a web application. A web application is any application that uses a Spring WebApplicationContext, defines a session scope, or has a StandardServletEnvironment.

Spring @ConditionalOnExpression Example

Condition On Expression I will create examples how to work with Spring Conditional on Expression using @ConditionalOnExpression. The @ConditionalOnExpression annotation lets configuration be included based on the result of a SpEL (Spring Expression Language) expression. For this example the Module class is only loaded if a particular SpEL is enabled. This way, you might create similar modules that are only loaded if their…

Spring @ConditionalOnResource Example

Condition On Resource In this tutorial I will create examples on Spring @ConditionalOnResource. The @ConditionalOnResource annotation lets configuration be included only when a specific resource is present in the classpath. For example, the Log4j class is only loaded if the log4j configuration file (log4j.properties) was found on the class-path. This way, you might create similar modules that are only loaded if their respective…

Dependency injection in Spring

Introduction Here we will see different types of dependency injections in Spring framework. There are three types of dependency injections and they are constructor injection, setter injection and interface injection. Spring supports only constructor and setter injection. Interface injection is supported by other language like Avalon. Interface injection a different type of DI(dependency injection) that involves mapping items to inject…

Spring @ConditionalOnProperty Example

Condition On Property Here I will create examples on Spring conditional on property using the annotation @ConditionalOnProperty. The @ConditionalOnProperty annotation allows you to load beans conditionally depending on a certain environment property or configuration of a property. Use the prefix and name attributes to specify the property that should be checked. By default, any property that exists and is not equal…

Spring @ConditionalOnClass and @ConditionalOnMissingClass

Conditional Class Spring @ConditionalOnClass and @ConditionalOnMissingClass annotations let @Configuration classes be included based on the presence or absence of specific classes. So @ConditionalOnClass loads a bean only if a certain class is on the class path and @ConditionalOnMissingClass loads a bean only if a certain class is not on the class path. This mechanism does not apply the same way…

Spring @ConditionalOnMissingBean Example

Conditional On Missing Bean In this tutorial I will create examples on Spring @ConditionalOnMissingBean. Anywhere you define a Spring bean, you can optionally add a condition. Only if the specified condition is satisfied then only bean will be added to the application context. To declare a condition, you can use any of the @Conditional… annotations. The @ConditionalOnMissingBean annotation lets a…

Spring @ConditionalOnBean Example

Conditional On Bean I will create examples on Spring @ConditionalOnBean. Anywhere you define a Spring bean, you can optionally add a condition. Only if the specified condition is satisfied then only bean will be added to the application context. To declare a condition, you can use any of the @Conditional… annotations. The @ConditionalOnBean annotation let a bean be included based…

Spring Boot – QueryForObject, QueryForList, BeanPropertyRowMapper

QueryForObject, QueryForList, BeanPropertyRowMapper In this post you will see how to select records using queryForObject(), queryForList(), BeanPropertyRowMapper in Spring Boot JdbcTemplate. Spring’s queryForObject() is used to fetch single row from the database. Spring’s queryForList() and BeanPropertyRowMapper are used to fetch multiple rows from the database. So, I will fetch single and multiple rows from the database using Spring JdbcTemplate.

Spring Security – Authentication and Role Based Authorization using JWT

Spring Security JWT Auth In this post you will see an example on Spring Security authentication and role based authorization using JWT (JSON Web Token) on REST or RESTful services. I won’t explain here about JWT as there is already very good article on JWT. I will implement Spring Security’s UserDetailsService to load user from database. I will use Spring…