Friday, 4 March 2016
Wednesday, 2 March 2016
What is the difference between dependency injection and dependency look up?
- Dependency lookup:
- If resource of the application spends time to search and get dependent values from other resources of application, then it is called dependency lookup
- Example :
- If the student get his dependent value material from instruction by asking for it then is called dependency lookup.
- The way servlet / ejb component gets jdbc data source object form registry through jndi lookup operation is called dependency lookup.
- In dependency lookup, resource perform “pull” operation on other resource or on underlying s/w to get the dependent values.
- <jee:jndi-lookup id="sdatasource" jndi-name="jdbc/synergy-emp-ds"/>
//@Repository ,@Component,@Controller
@Repository("LoginDaoImpl")
@Transactional
public class LoginDaoImpl implements LoginDao {
@Autowired
@Qualifier("sdatasource")
private DataSource dataSource;
}
- Dependency injection:
When we create any application there are many classes in it which depend on each other to execute some business logic. In that case any object which needs other objects to do its work, is responsible for getting those dependencies itself (by using new keyword) by that way those classes are tightly coupled.
In case of dependency injection objects won't be creating their dependencies themselves but the dependencies will be injected into the objects. How Spring helps with dependency injection is you can define your beans in a XML file (there are other ways too) and spring will initialize the classes and create the associations.
In case of dependency injection objects won't be creating their dependencies themselves but the dependencies will be injected into the objects. How Spring helps with dependency injection is you can define your beans in a XML file (there are other ways too) and spring will initialize the classes and create the associations.
Aliasing a bean outside the bean definition
#####################################################################
In a bean definition itself, you can supply more than one name for the bean, by using a combination of up to one name specified by the
idattribute, and any number of other names in the name attribute. These names can be equivalent aliases to the same bean, and are useful for some situations, such as allowing each component in an application to refer to a common dependency by using a bean name that is specific to that component itself.
Specifying all aliases where the bean is actually defined is not always adequate, however. It is sometimes desirable to introduce an alias for a bean that is defined elsewhere. This is commonly the case in large systems where configuration is split amongst each subsystem, each subsystem having its own set of object definitions. In XML-based configuration metadata, you can use the
<alias/> element to accomplish this.<alias name="fromName" alias="toName"/>
Friday, 15 January 2016
Using HSQL database with Hibernate
Step-1
Add dependency inside the pom.xml
<!-- Dependency for hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.4.Final</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.2.9</version>
</dependency>
</dependencies>
Step-2
<!-- Hibernate 4 with spring 3.x -->
<bean id="librarySessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" scope="singleton">
<property name="dataSource" ref="libraryDataSource" />
<property name="packagesToScan">
<list>
<value>com.usc.dao.entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- below key is important use this key = hibernate.hbm2ddl.auto not
hbm2ddl.auto -->
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
Step-3
Define the datasource
<context:property-placeholder location="/WEB-INF/context/persistence-mysql.properties" />
<bean id="libraryDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource" scope="singleton">
<property name="driverClassName" value="${db.driverclassname}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
Step-4
"/WEB-INF/context/persistence-mysql.properties
################Comment###############
#db.driverclassname=com.mysql.jdbc.Driver
#db.url=jdbc:mysql://localhost:3306/library_db?createDatabaseIfNotExist=true
#db.username=root
#db.password=root
########Setting for HSQL##################
db.driverclassname=org.hsqldb.jdbc.JDBCDriver
db.url=jdbc:hsqldb:mem:library_db?createDatabaseIfNotExist=true
db.username=sa
db.password=
Step-5
Define Spring Transaction Manager
<!--
@Transactional
-->
<tx:annotation-driven proxy-target-class="true"
transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="librarySessionFactory"></property>
</bean>
Step-6
Applying transaction manager in dao layer
/**
*
* @author usc
*
*/
@Repository("IUserDao")
@Transactional(propagation=Propagation.REQUIRED)
public class IUserDao extends AbstractDaoImpl<UserEntity,Integer> implements UserDao {
}
AbstractDaoImpl.java
/**
* @author usc
* @param <E>
* @param <I>
*/
public abstract class AbstractDaoImpl<E, I extends Serializable> implements AbstractDao<E, I> {
private Class<E> entityClass;
protected AbstractDaoImpl(Class<E> entityClass) {
this.entityClass = entityClass;
}
@Autowired
@Qualifier("librarySessionFactory")
private SessionFactory psessionFactory;
public Session getCurrentSession() {
return psessionFactory.getCurrentSession();
}
}
Monday, 4 January 2016
Implementing Pagination with Spring JDBC
JSP Code :
<span style="float: right;margin-right: 150px;">
${(fruitPaginationForm.currentPage-1)*fruitPaginationForm.recordsPerPage+1}
-
<c:if test="${(fruitPaginationForm.currentPage-1)*fruitPaginationForm.recordsPerPage+fruitPaginationForm.recordsPerPage gt fruitPaginationForm.noOfRecords}">
${fruitPaginationForm.noOfRecords}
</c:if>
<c:if test="${(fruitPaginationForm.currentPage-1)*fruitPaginationForm.recordsPerPage+fruitPaginationForm.recordsPerPage lt fruitPaginationForm.noOfRecords}">
${(fruitPaginationForm.currentPage-1)*fruitPaginationForm.recordsPerPage+fruitPaginationForm.recordsPerPage}
</c:if>
of ${fruitPaginationForm.noOfRecords}
</span>
Code for Next and Previous Button on JSP
<c:if test="${fruitPaginationForm.currentPage != 1}">
<td><a href="${pageContext.request.contextPath}/fruitWithPagination.do?page=${fruitPaginationForm.currentPage - 1}">Previous</a></td>
</c:if>
|
<c:if test="${fruitPaginationForm.currentPage lt fruitPaginationForm.noOfPages}">
<td><a href="${pageContext.request.contextPath}/fruitWithPagination.do?page=${fruitPaginationForm.currentPage + 1}">Next</a></td>
</c:if>
Controller Code
@RequestMapping(value="fruitWithPagination.do",method=RequestMethod.GET)
public String fruitWithPagination(@RequestParam(value="page",required=false)String page,Model model) {
int recordsPerPage=3;
int currentPage=0;
if(page==null) {
currentPage=1;
}else{
currentPage=Integer.parseInt(page);
}
FruitPaginationForm fruitPaginationForm = fruitService.findFruitsWithPagination( (currentPage-1)*recordsPerPage, recordsPerPage);
fruitPaginationForm.setCurrentPage(currentPage);
fruitPaginationForm.initPagination(); //initializing number of total pages......
model.addAttribute("fruitPaginationForm", fruitPaginationForm);
return "tfruits"; //give view name with out extension........../fruitSearch.jsp
}
public class FruitPaginationForm {
private int currentPage;
private int noOfRecords;
private List<FruitForm> fruitFormList;
private int recordsPerPage=3;
private int noOfPages;
public void initPagination(){
noOfPages=(int) Math.ceil(noOfRecords * 1.0 / recordsPerPage);
}
}
Service Layer Code
@Override
public FruitPaginationForm findFruitsWithPagination(int start,
int noOfRecords) {
FruitPaginationEntity fruitPaginationEntity=fruitDao.findFruitsWithPagination(start, noOfRecords);
FruitPaginationForm fruitPaginationForm=new FruitPaginationForm();
BeanUtils.copyProperties(fruitPaginationEntity, fruitPaginationForm);
List<FruitForm> fruitFormList=new ArrayList<FruitForm>();
List<FruitEntity> fruitEntities=fruitPaginationEntity.getFruitEntityList();
for(FruitEntity fe:fruitEntities){
FruitForm form=new FruitForm();
BeanUtils.copyProperties(fe, form);
fruitFormList.add(form);
}
fruitPaginationForm.setFruitFormList(fruitFormList);
return fruitPaginationForm;
}
Dao Layer Code with MySQL Database
@Override
public FruitPaginationEntity findFruitsWithPagination(int start, int noOfRecords) {
String query = "select * from fruit_tbl limit " + start + ", " + noOfRecords;
List<FruitEntity> fruitEntityList = (List<FruitEntity>) jdbcTemplate .query(query,
new BeanPropertyRowMapper(FruitEntity.class));
// To obtain this row count, include a SQL_CALC_FOUND_ROWS option in the
// SELECT statement, and then invoke FOUND_ROWS() afterward
FruitPaginationEntity fruitPaginationEntity = new FruitPaginationEntity();
int tnoOfRecords = jdbcTemplate.queryForInt("select count(*) from fruit_tbl");
fruitPaginationEntity.setNoOfRecords(tnoOfRecords);
fruitPaginationEntity.setFruitEntityList(fruitEntityList);
/* query a total number of rows from database. */
return fruitPaginationEntity;
}
*/
public class FruitPaginationEntity {
private int noOfRecords;
private List<FruitEntity> fruitEntityList;
public int getNoOfRecords() {
return noOfRecords;
}
}
Subscribe to:
Posts (Atom)



