Posts

Spring JdbcTemplate

  Spring JdbcTemplate Spring  JdbcTemplate  is a powerful mechanism to connect to the database and execute SQL queries. It internally uses JDBC api, but eliminates a lot of problems of JDBC API. Problems of JDBC API The problems of JDBC API are as follows: We need to write a lot of code before and after executing the query, such as creating connection, statement, closing resultset, connection etc. We need to perform exception handling code on the database logic. We need to handle transaction. Repetition of all these codes from one to another database logic is a time consuming task. Spring JdbcTemplate eliminates all the above mentioned problems of JDBC API. It provides you methods to write the queries directly, so it saves a lot of work and time. Spring Jdbc Approaches Spring framework provides following approaches for JDBC database access: JdbcTemplate NamedParameterJdbcTemplate SimpleJdbcTemplate SimpleJdbcInsert and SimpleJdbcCall JdbcTemplate class It is the central c...

Spring - Java Based Configuration

  Spring - Java Based Configuration Java-based configuration option enables you to write most of your Spring configuration without XML but with the help of few Java-based annotations explained. @Configuration & @Bean Annotations Annotating a class with the  @Configuration  indicates that the class can be used by the Spring IoC container as a source of bean definitions. The  @Bean  annotation tells Spring that a method annotated with @Bean will return an object that should be registered as a bean in the Spring application context. The simplest possible @Configuration class would be as follows − package JavaBasedConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class HelloworldConfig { @Bean public HelloWorld helloWorld() { return new HelloWorld(); } } The above code will be equivalent to the following XML configuration − <beans...

Spring Expression Language (SpEL) Example

  1.  The Spring Expression Language (SpEL) is a powerful expression language that supports querying and manipulating an object graph at runtime. 2.  We can use SpEL with annotation configuration, XML configuration and  SpelExpressionParser  class. 3.  In annotation-based and XML-based configuration, SpEL expression is used for defining  BeanDefinition  instances. In both cases, the syntax to define the expression is  of the form  #{ <expression string> }  . Using SpEL Spring Expression Language is passed in  #{ <expression string> }  format in both cases, annotation configuration as well as XML configuration. In annotation configuration, we pass expression in  @Value  annotation. 1.  The  T  operator is used to specify an instance of  java.lang.Class . @Value ( "#{ T(java.lang.Math).random() * 100}" ) private Integer randomNumber ; XML The  T  operator is used to spec...