Spring NamedParameterJdbcTemplate example
Spring NamedParameterJdbcTemplate is template like JdbcTemplate with set of methods supporting JDBC operations. NamedParameterJdbcTemplate supports named parameters. A positional parameter (?) is set by its index in the clause. A named parameter (:paramName) is set by its name.
NamedParameterJdbcTemplate allows us to represent and pass the sql parameters with parameter names rather than the positional parameter (‘?’).
In the below example, EMPLOYEE_ID parameter is set by name ‘ :empId’(colon followed by a parameter name). Continue reading
Spring JdbcTemplate stored procedure example
Spring JdbcTemplate provides support for calling stored procedures. The method ‘call’ is added in JdbcTemplate to execute the stored procedures.
public Map call(CallableStatementCreator csc,List declaredParameters) throws DataAccessException
The method takes two parameters
- CallableStatementCreator csc
First parameter of the method is CallableStatementCreator interface which is used by JdbcTemplate to get CallableStatement. Implementation class of the interface has to implement the ‘createCallableStatement’ method. Continue reading
Spring RowMapper Example
The RowMapper is interface used by JdbcTemplate for mapping each row of data in the ResultSet.
public interface RowMapper<T>
Implementation class of the interface has to implement the ‘mapRow’ method and map each row of ResultSet to required Object. The ‘mapRow’ method returns an Object for the current row. SQLExceptions will be caught and handled by the calling JdbcTemplate.
Object mapRow(ResultSet rs,int rowNum) throws SQLException
In the below example, mapRow() method is implemented to map each row of ResultSet to Employee Object.
Example Continue reading
Spring JdbcTemplate batchupdate example
Batch update option allows us to submit multiple queries to database at once. Spring JdbcTemplate supports batch updates through JDBC Statement and PreparedStatement. The Spring JdbcTemplate provides following overloaded methods for batch updates
int batchUpdate(String sql) throws DataAccessException
Executes multiple SQL queries on a single JDBC Statement
int batchUpdate(String sql, BatchPreparedStatementSetter pss) throws DataAccessException
Executes multiple SQL queries on a single PreparedStatement
The batch update support is introduced in JDBC 2.0.If your driver does not support batch updates then the above methods executes the queries as separate updates on single JDBC Statement and PreparedStatement.
Example Continue reading
Spring AOP AspectJ annotation example
Spring AspectJ provides annotation support for Spring AOP. AspectJ automatically generates proxy for the bean to intercept method invocations as configured.
How to enable @AspectJ support?
If you are using xml configuration file include <aop:aspectj-autoproxy/>
If you are using configuration class add @EnableAspectJAutoProxy annotation Continue reading
Spring MVC Dispatcher Servlet
Spring Web MVC framework is designed around the Front controller which is DispatcherServlet. DispatcherServlet is a servlet implemented as a subtype of HttpServlet. It is responsible for initializing the Spring Web MVC framework.
DispatcherServlet listens to the incoming requests and dispatches the request to handler,configurable handler mapping, view resolvers, local and theme resolvers. It has to be configured in web.xml Continue reading
Spring factory bean attribute
A spring bean can be instantiated using non static factory method. A method that returns instance of a class is called factory method. The spring container uses the non-static factory method to instantiate the bean.
The bean of the factory method’s class has to be specified using ‘factory-bean’ attribute and the factory method name has to be specified using ‘factory-method’ attribute of the bean element.
<bean id=”employeeFactory” class=”com.answersz.factorymethods.EmployeeFactory” />
<bean id=”employeeService” factory-bean=”employeeFactory” factory-method=”getEmployeeService” />
Example Continue reading
Spring map injection
The spring core container supports injecting the primitives, array, collections and spring bean through constructor or setter dependency injection.
A map can be injected using <map> element. A <map> element represents java.util.Map. A map can contain zero or more <entry> elements. Each <entry> represents one map entry. The <entry> element takes <key> and <value> elements.
In the below example, countryMap is injected using setter dependency injection by placing the <map> element inside the <property>. The map can be injected using dependency injection by placing the <map> element inside <constructor-arg>. Continue reading
Spring factory method attribute
A spring bean can be instantiated using static factory method. A method that returns instance of a class is called factory method. The spring container uses the static factory method to instantiate the bean. The class name of the factory method has to be specified using ‘class’ attribute and the factory method name has to be specified using ‘factory-method’ attribute of the bean element.
<bean id=”employeeService” class=”com.answersz.factorymethods.EmployeeFactory” factory-method=”getEmployeeService” />
In the below example, getEmployeeService is the static factory method in EmployeeFactory class which returns the instance of EmployeeService. The EmployeeService bean can instantiated by configuring class as ‘EmployeeFactory’ and factory-method as ‘getEmployeeService ‘.
EmployeeService.java Continue reading
Spring IoC Container and Beans
Spring IoC Container
Spring IoC Container is the basis for spring framework. The primary job of spring IoC container is to instantiate the Java object, injecting its dependencies and managing them.
The object created and managed by spring core container is called a Spring Bean. The bean objects has to be added in the configuration metadata which is taken by the Spring IoC Controller.
The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework IoC container.
BeanFactory Continue reading