Monday 16 April 2018

WebMvcConfigurerAdapter

package com.leejo.config;

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;

import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.PropertySource;

import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

import org.springframework.core.env.Environment;

import org.springframework.orm.hibernate4.HibernateTemplate;

import org.springframework.orm.hibernate4.HibernateTransactionManager;

import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;

import org.springframework.transaction.annotation.EnableTransactionManagement;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import org.springframework.web.servlet.view.InternalResourceViewResolver;

import com.leejo.bo.Interceptor;

import com.leejo.model.Student;

/**

* @author Leejo Jose

* @date 01-11-2017

*

*/

@Configuration

@ComponentScan("com.leejo")

@EnableWebMvc

@PropertySource("classpath:db.properties")

@EnableTransactionManagement

public class ApplicationContextConfig extends WebMvcConfigurerAdapter{

@Bean(name = "viewResolver")

public InternalResourceViewResolver getViewResolver() {

InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

viewResolver.setPrefix("/WEB-INF/views/");

viewResolver.setSuffix(".jsp");

return viewResolver;

}  

//leejo jose demo

@Autowired

private Environment env;

@Bean

public DataSource getDataSource() {

BasicDataSource dataSource = new BasicDataSource();

dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));

dataSource.setUrl(env.getProperty("jdbc.url"));

dataSource.setUsername(env.getProperty("jdbc.username"));

dataSource.setPassword(env.getProperty("jdbc.password"));  

return dataSource;

}

@Bean

@Autowired

public HibernateTransactionManager transactionManager(SessionFactory sessionFactory)

{

HibernateTransactionManager htm = new HibernateTransactionManager();

htm.setSessionFactory(sessionFactory);

return htm;

}

@Bean

@Autowired

public HibernateTemplate getHibernateTemplate(SessionFactory sessionFactory)

{

HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory);

return hibernateTemplate;

}

@Autowired

@Bean(name = "sessionFactory")

public SessionFactory getSessionFactory(DataSource dataSource) {

LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);  

//sessionBuilder.addAnnotatedClasses(User.class,UserProfile.class,Student.class);

sessionBuilder.scanPackages("com.leejo.model");

return sessionBuilder.buildSessionFactory();

}

@Bean

public Properties getHibernateProperties()

{

Properties properties = new Properties();

properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));

properties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));

properties.put("hibernate.current_session_context_class", env.getProperty("hibernate.current_session_context_class"));

return properties;

}

@Override

public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");

registry.addResourceHandler("/uploads/**").addResourceLocations("/uploads/");

registry.addResourceHandler("/albums/**").addResourceLocations("/albums/");

}

@Bean

public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {

return new PropertySourcesPlaceholderConfigurer();

}

@Autowired

@Bean(name = "studentService")

public Student getUserDetails(){

return new Student();

}

@Override

public void addInterceptors(InterceptorRegistry registry) {

// TODO Auto-generated method stub

registry.addInterceptor(new Interceptor());

}

}

Monday 2 April 2018

Java 8

forEach() method in Iterable interface

Whenever we need to traverse through a Collection, we need to create an Iterator whose whole purpose is to iterate over and then we have business logic in a loop for each of the elements in the Collection. We might get ConcurrentModificationException if iterator is not used properly.

Java 8 has introduced forEach method in java.lang.Iterable interface so that while writing code we focus on business logic only. forEach method takes java.util.function.Consumer object as argument, so it helps in having our business logic at a separate location that we can reuse. Let’s see forEach usage with simple example.

example

package com.leejo.forech;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import java.util.function.Consumer;

import java.lang.Integer;

public class Java8ForEachExample {

public static void main(String[] args) {

//creating sample Collection

List<Integer> myList = new ArrayList<Integer>();

for(int i=0; i<10; i++) myList.add(i);

//traversing using Iterator

Iterator<Integer> it = myList.iterator();

while(it.hasNext()){

Integer i = it.next();

System.out.println("Iterator Value::"+i);

}

//traversing through forEach method of Iterable with anonymous class

myList.forEach(new Consumer<Integer>() {

public void accept(Integer t) {

System.out.println("forEach anonymous class Value::"+t);

}

});

//traversing with Consumer interface implementation

MyConsumer action = new MyConsumer();

myList.forEach(action);

}

}

//Consumer implementation that can be reused

class MyConsumer implements Consumer<Integer>{

public void accept(Integer t) {

System.out.println("Consumer impl Value::"+t);

}

}

---The number of lines might increase but forEach method helps in having the logic for iteration and business logic at separate place resulting in higher separation of concern and cleaner code.