Java

Java is a set of computer software and specifications developed by Sun Microsystems, which was later acquired by the Oracle Corporation, that provides a system for developing application software and deploying it in a cross-platform computing environment. Java is used in a wide variety of computing platforms from embedded devices and mobile phones to enterprise servers and supercomputers.

Spring Logo

Spring Framework

The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key element of Spring is infrastructural support at the application level: Spring focuses on the "plumbing" of enterprise applications so that teams can focus on application-level business logic, without unnecessary ties to specific deployment environments.

Hibernate Logo

Hibernate Framework

Hibernate ORM is an object-relational mapping framework for the Java language. It provides a framework for mapping an object-oriented domain model to a relational database.

Thursday, February 20, 2014

Case Insensitive Sorting Example in Oracle11g


Step1: Create Employee table 
create table employee(id int, name varchar(20));
 
Step2: Insert values to Employee table
insert into employee values(1,'Ranga');
insert into employee values(2,'rAnga Reddy');
insert into employee values(3,'Raja');
insert into employee values(4,'raJa Reddy');
insert into employee values(5,'raja');
insert into employee values(6,'Reddy');
insert into employee values(7,'ranga');
 
Step3: Select the Employee values with out sorting
select * from employee;
------------------------------------------- 
ID NAME
1 Ranga
2 rAnga Reddy
3 Raja
4 raJa Reddy
5 raja
6 Reddy
7 ranga
 
Step4: Select the Employee values with sorting 
select * from employee order by name;
------------------------------------------- 
ID NAME
3 Raja
1 Ranga
6 Reddy
2 rAnga Reddy
4 raJa Reddy
7 ranga
5 raja

 
Step5: Select the Employee values with case insensitive sorting 
select * from employee order by upper(name);
------------------------------------------- 
ID NAME
5 raja
3 Raja
4 raJa Reddy
7 ranga
1 Ranga
2 rAnga Reddy
6 Reddy
 
Click here to Run and Test the above example in online.


Friday, February 14, 2014

Formatting source code on Blogger


The following are the few sites to format a source code on blogger.
  1. http://hilite.me/
  2. http://formatmysourcecode.blogspot.in/
  3. http://markup.su/highlighter/

Java program to check if number is Armstrong or not.


/**
* @file : ArmstrongExample.java
* @description :
*/

/**
A number is called as ARMSTRONG number if,sum of cube of every digit present in the number is equal
to the number itself then that number is called as armstrong number.For example the 153 is a armstrong
number because 1^3 + 5^3 + 3^3 =153. 
 
Some of the Armstrong number are 0, 1, 153, 370, 371, 407
 
*/

package com.ranga.collections;

import java.util.Scanner;

/**
* Java program to check if number is Armstrong or not.
* @author: ranga
* @date: Feb 13, 2014
*/
public class ArmstrongExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Number: ");
int num = scanner.nextInt();
boolean isArmstrong = isArmstrong(num);
if(isArmstrong) {
System.out.println(num +" is Armstrong");
} else {
System.out.println(num +" not Armstrong");
}
}
private static boolean isArmstrong(int num) {
int number = num;
int sum = 0;
do {
int den = num % 10;
sum = den * den * den + sum;
num = num / 10;
} while(num != 0);

if(number == sum) {
return true;
}
return false;
}
} 
 
Output: 
Enter Number: 
141
141 not Armstrong

Enter Number: 
153
153 is Armstrong


Explanation:
Assume Number is 153
 
Step1: 
 
Initially
number = 153
sum = 0

after entering while loop

den = 153 % 10 = 3
sum = 3 * 3 * 3 + 0(sum) = 27
num = 153 / 10 = 15
 
next do while check the do while condition i.e num != 0 = 15 != 0 = true

Step2:

Now sum = 27
    num = 15
 
den = 15 % 10 = 5
sum = 5 * 5 * 5 + 27(sum) = 125 + 27 = 152
num = 15 / 10 = 1
 
next it will check condition i.e num != 0 = 1 != 0 = true
 
Step3:
 
Now sum = 152
    num = 1
 den = 1 % 10 = 1
 sum = 1 * 1 * 1 + 152(sum) = 1 + 152 = 153
 num = 1 / 10 = 0
next it will check do while condition i.e num != 0 = 0 != 0 = false
 
Now program exits the while loop...
 
then we are comparing the number == sum i.e 153 == 153
 
 
 

Sunday, February 2, 2014

New Features in Spring Framework 4.0:

New Features in Spring Framework 4.0:

The Spring 4.0 is the latest major release of Spring framework which the support for Java 8, JEE 7, REST WebServices and HTML 5 supports.
  • Removed Deprecated Packages and Methods - In Spring Framework 4.0 all deprecated packages, classes and methods have been removed.
  • Java 8 support - Spring Framework 4.0 provides support for several Java 8 features such as expression of callbacks using lambdas, JSR 310 Date and Time API, and parameter name discovery.
    • Auto-wiring based on generic types.
    • @Description annotation has been added.
    • @Conditional annotation is introduced that can conditionally filter the beans.
    • @Ordered annotation and Ordered interface are supported for ordering the Beans.
    • Now programmers can write custom annotations that expose specific attributes from the source annotation.
    • The @Lazy annotation can now be used on injection points, as well as on @Bean definitions.
    • CGLIB-based proxy classes no longer require a default constructor. Support is provided via the objenesis library which is repackaged inline and distributed as part of the Spring Framework. With this strategy, no constructor at all is being invoked for proxy instances anymore.
  • General Web Improvements - The following general improvements have been made to Spring’s Web modules:
  • WebSocket, SockJS, and STOMP Messaging
    • A new spring-websocket module provides comprehensive support for WebSocket-based, two-way communication between client and server in web applications. It is compatible with JSR-356, the Java WebSocket API, and in addition provides SockJS-based fallback options (i.e. WebSocket emulation).
    • A new spring-messaging module adds support for STOMP as the WebSocket subprotocol to use in applications along with an annotation programming model for routing and processing STOMP messages from WebSocket clients. As a result an @Controller can now contain both @RequestMapping and @MessageMapping methods for handling HTTP requests and messages from WebSocket-connected clients.
  • Testing Improvements -
    • Almost all annotations in the spring-test module (e.g., @ContextConfiguration, @WebAppConfiguration, @ContextHierarchy, @ActiveProfiles, etc.) can now be used as meta-annotations to create custom composed annotations and reduce configuration duplication across a test suite.
    • Active bean definition profiles can now be resolved programmatically, simply by implementing a custom ActiveProfilesResolver and registering it via the resolver attribute of @ActiveProfiles.

New Features in Spring Framework 3.x

New Features in Spring Framework 3.x:

  • Java 5 Support - The core API of Spring 3.0 framework is using Java 5, so Java5 or above is required to run Spring 3.0 based applications. Java 5 features such as generics and annotations and varargs can be used in Spring 3.0 Framework based applications. The Spring 3.0 is fully compatible with the JEE1.4 and JEE5.
  • Spring Expression Language (SpEL) - Spring 3.0 introduces Spring Expression Language (SpEL), a powerful expression language that supports querying and manipulating an object graph at runtime. With SpEL, Spring applications can overcome the limitation of using only fixed values in configuration files. It can be used for bean definitions in both XML- and annotation-based configurations.
  • IoC enhancements/Java based bean metadata are introduced - Some of the Spring JavaConfig capabilities have been migrated into the core framework, such as @Configuration, @Bean, @Primary, @DependsOn, @Lazy, @Import and @Value.
  • Declarative model validation using constraint annotations - JSR 303 (Bean Validation) annotations support has been added and validation rules can now be added to beans using annotations such as @NotNull and @Max(23).
  • Comprehensive REST support in Spring MVC - Support for building RESTful servers and clients has been added.
  • Java EE 6 support - Many of the Java EE 6 features such as JPA 2.0 and JSF 2.0 are supported and they work on non-EE 6 containers like Tomcat and J2EE 1.4 app servers.
  • JSR 330 support - The javax.inject annotations introduced by JSR 330 are now supported.
  • Annotation-based formatting - Bean fields can be automatically formatted and converted using annotations such as @DateFimeFormat(iso=ISO.DATE) and @NumberFormat(style=Style.CURRENCY).
  • Support for embedded databases -Support for embedded Java database engines provides by the org.springframework.jdbc.datasource.embedded package (HSQL, H2, and Derby).
  • Object to XML Mapping(OXM)- The Object-to-XML (OXM) mapping functionality from the Spring Web Services project has been moved into the Core Spring Framework (org.springframework.oxm).
  • Support for Hibernate 4.x (Spring 3.1):  org.springframework.orm.hibernate4 package.
  • Support for Servlet 3 based asynchronous request processing (Spring3.1):  The Spring MVC programming model now provides explicit Servlet 3 async support. @RequestMapping methods can return one of:
    • java.util.concurrent.Callable to complete processing in a separate thread managed by a task executor within Spring MVC.
    • org.springframework.web.context.request.async.DeferredResult to complete processing at a later time from a thread not known to Spring MVC — for example, in response to some external event (JMS, AMQP, etc.)
    • org.springframework.web.context.request.async.AsyncTask to wrap a Callable and customize the timeout value or the task executor to use.
  • Spring MVC Test framework (Spring 3.1): First-class support for testing Spring MVC applications with a fluent API and without a Servlet container. Server-side tests involve use of the DispatcherServlet while client-side REST tests rely on the RestTemplate.
  • Refined Java SE 7 / OpenJDK 7 support (Spring 3.2): Spring Framework 3.2 comes with refined Java 7 support within the framework as well as through upgraded third-party dependencies: specifically, CGLIB 3.0, ASM 4.0

New Features in Spring Framework 2.5:

New Features in Spring Framework 2.5:
  • Annotation-driven configuration:  Annotation-driven dependency injection through @Autowired annotation and fine-grained auto wiring control with@Qualifier.
Support for JSR-250 annotations including @Resource for dependency injection of a named resource, as well as @PostConstruct and @PreDestroy for life-cycle methods.
  • Autodetecting components in the classpath: Spring 2.5 introduces support component scanning: autodetecting annotated components in the classpath. Typically, such component classes will be annotated with stereotypes such as @Component, @Repository, @Service, @Controller.
  • Portlet framework: Spring 2.0 ships with a Portlet framework that is conceptually similar to the Spring MVC framework.
  • Enhanced testing support: Spring 2.5 introduces the Spring TestContext Framework which provides annotation-driven unit and integration testing. A new integration test framework that is based on JUnit 4 and Annotations.
  • Java 5 (Tiger) support: Java 5 support in Spring 2.0 and 2.5.
  • Dynamic language support: Spring 2.5 refines the dynamic languages support with autowiring and support for the recently released JRuby 1.0.
  • Java EE 5 support: including JTA 1.1, JavaMail 1.4 and JAX-WS2.0 etc.
  • Support for AspectJ load-time weaving: Spring 2.5 introduces explicit support AspectJ load-time weaving, as alternative to the proxy-based AOP framework. The new context:load-time-weaver configuration element automatically activates AspectJ aspects as defined in AspectJ's META-INF/aop.xml descriptor, applying them to the current application context through registering a transformer with the underlying ClassLoader.
  • Easier XML configuration: New XML configuration namespaces, including context namespace for configuring application context details and a jms namespace for configuring message-driven beans.

Step by Step to develop Spring4.0 Application

Step1:  Create Java Project

The first step is to create a simple Java Project using Eclipse IDE. Follow the option File -> New -> Project and finally select Java Project wizard from the wizard list. Now name your project as HelloWorld.

Step 2 - Add Required Libraries:

Add Spring Framework and common logging API libraries in HelloWorld project. To do this, right click on your project name HelloWorld and then follow the following option available in context menu: Build Path -> Configure Build Path to display the Java Build Path.

Now use Add External JARs button available under Libraries tab to add the following core JARs from Spring Framework and Common Logging installation directories:
Windows:  
SPRING4_HOME=E:\ranga\MyFiles\Spring\Software\spring-framework-4.0.0.RELEASE
Linux: 
SPRING4_HOME=/home/ranga/MyFiles/Spring/Software/spring-framework-4.0.0.RELEASE
%SPRING4_HOME%/libs/spring-core-4.0.0.RELEASE.jar
%SPRING4_HOME%/libs/spring-beans-4.0.0.RELEASE.jar

commons-logging-1.1.3.jar

Step 3 - Create HelloMessage Interface (HelloMessage.java) :
package com.ranga;
 
public interface HelloMessage {
public void sayHello();
}

Step 4 - Create HelloMessageImpl Class (HelloMessageImpl.java) :

package com.ranga;

public class HelloMessageImpl implements HelloMessage{
@Override
public void sayHello() {
System.out.println("Hello Ranga!");
}
}

Step 5 - Create Bean Configuration file (beans.xml):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<bean id="helloMessage" class="com.ranga.HelloMessageImpl"></bean>

</beans>

Step 6 - Create Client Application (Application.java):

package com.ranga;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Application {
public static void main(String[] args) {
Resource resource = new ClassPathResource("beans.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
HelloMessage helloMessage = (HelloMessage) beanFactory.getBean("helloMessage");
helloMessage.sayHello();
}
}

Step 7 - Run the Application (Application.java):
Feb 2, 2014 11:31:26 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Hello Ranga!

Next Program We can see by using Annotations and Java Configuration
 .............
Coming soon......