Senin, 30 Juni 2014

5 books to learn Spring framework and Spring MVC for Java Programmers




Spring and Spring MVC is one of the most popular Java framework and most
of new Java projects uses Spring these days. Java programmer often ask
questions like which books is good to learn Spring MVC or What is the best book
to learn Spring framework
etc. Actually, there are many books to learn Spring
and Spring MVC but only certain books can be considered good because of  there content, examples or the way they
explained concept involved in Spring framework. Similar to  Top
5 books on Java programming
we will some good books on Spring in this
article, which not only help beginners to start with Spring but also teaches
some best practices. In order to learn a new technology or a new framework,
probably best way is to start looking documentation provided and Spring
Framework is no short on this. Spring  provides great, detailed documentation to use
various features of Spring framework but despite of that nothing can replace a
good book. Luckily both Spring and Spring MVC got couple of good titles which
not only explains concepts like Dependency
Injection and Inversion of Control
which is core to spring framework but
also gives coverage to other important aspect of Spring. Following are some of
the good books available on Spring and Spring MVC which can help you to learn
Spring.









Top 5 Books on Spring
Framework and Spring MVC



Here is my list of top 5 books to learn Spring MVC and Spring framework.
Let me know if you come across any other great book on Spring, which is worth
adding into this list.






Expert
Spring MVC and Web Flow


Expert Spring MVC and Web Flow by Seth Ladd, Darren Davison, Steven
Devijver, Colin Yates is one of my favorite book on Spring MVC and arguably one
of the best book in Spring MVC. It covers both Spring MVC and web flow in depth
and explains each concept with simple explanation. I highly recommend this
book to any beginner which is learning Spring MVC framework. There chapter on Spring
fundamentals is also one of the best way to learn dependency
injection and inversion of control in Spring
and I myself learned DI and IOC
from that chapter. This is the Spring book I recommend to any Java web
developer who is familiar with Java web technology or any MVC framework like
Struts. Only missing point is that this book only covers Spring MVC and web
flow and does not cover whole Spring framework. Also, in my opinion there
chapter on Spring Fundamentals is one of the best way to start with Spring
framework. 



Good books to learn Spring Framework and Spring MVC














Spring
Recipes – A problem solution approach


This is another good book on Spring Framework which I like most. This
book is collection of Spring recipes or How to do in Spring Framework. In every
Spring recipes you learn some new concept and it also helps to learn Spring
fundamental e.g. there recipes help me to learn when
to use ApplicationContext and BeanFactory
and  Constructor
vs Setter Injection
. Key highlight of this book is, It’s problem
solution approach
. Since it’s teaching style is different than any
conventional book, it’s a good supplement along with Spring documentation. This
books also provide excellent coverage of many spring technologies e.g. Spring
Security
, Spring JDBC, Spring and EJB,
JMX, Email and have a chapter on scripting as well. If you like books on
problem solution approach than you will enjoy reading Spring Recipes, not the
best book on Spring but still a good one and will definitely made to any list
of top 10 books on Spring framework.



Best Spring books








Professional
Java Development with the Spring Framework


Main highlight of this book is that one of it’s author is Rod Johnson,
who is also created Spring framework. So you get his view on Spring and How
spring should be used used, what are best practices to follow on Spring e.g. When
to use Setter Injection and Constructor Injection
. This book provide good
coverage of Spring framework including Spring core, Spring MVC, Spring ORM support
etc. Also examples in this book is easy to understand and it also focus on Unit
tests
which is good practice. Though I don’t rate this book too high, like
if your focus is Spring MVC than Expert Spring MVC and Web flow is the best
Spring book to follow. If you are looking an overview on Spring features, than
Spring Documentation is best book to read. As I said positive point of this
book is knowing Spring from author Rod Johnson himself. Once you have basic
knowledge of Spring framework, you can read this book to get authors view.



Must read Spring Framework book for Java developers








Pro
Spring 3.0


Pro Spring is one of the best book to learn Spring Framework from start.
This book is massive and tries to cover most of the Spring concept e.g. Spring
fundamentals, JDBC
Support
, Transaction support, Spring AOP, Spring Web MVC, Spring Testing
etc. Good point about this book is that it’s conventional and easy to read, it
explains concept, followed with good example, which is good way to learn. What
is worrying is sheer size, I haven’t completed this book till date and only
refer with some topic. Good point is that this book covers Spring 3.1
which is the latest stable version. As I said this is one of the most comprehensive book on Spring framework
and any one who wants to learn Spring framework by following just one book, Pro
Spring 3.0 is a good choice.












Spring
Documentation


Spring framework documentation is located on Springsource website, here
is the link for Spring documentation for Spring framework 3.1 in HTML format http://static.springsource.org/spring/docs/3.1.0.M2/spring-framework-reference/html/. Though this is not a book, Spring tutorials and Spring documentation are
another two source of learning Spring framework, which I highly recommend. Main
reason for that is they are free and highly comprehensive and has lot of
examples to support various concept and feature. Also one of the best part
of reference documentation is that they are updated with the latest Spring
release available
. Updating books with every new version of Spring is
rather difficult than updating documentation. Spring documentation combine with
any Spring book is best way to learn Spring framework. For learning Spring MVC,
you can combine Spring documentation with earlier spring book, Expert Spring
MVC and Web Flow
.








Spring in Action


Lots of my readers suggested  Spring in Action from manning, as one of the best book to learn Spring. Seems like a worth reading book. I have seen it's content briefly and it does cover both Spring and Spring MVC. So if you are looking for common book for complete Spring framework, Spring in Action is another one.










These are some of the best books to learn Spring framework and Spring MVC.
Spring documentation is special  because
of update and new releases of Spring Framework. Given popularity of Spring
Framework for new Java development work, every Java developer should make
effort to learn Spring framework.

























Source:http://javarevisited.blogspot.com/2013/03/5-good-books-to-learn-spring-framework-mvc-java-programmer.html

White House Backs Right to Unlock Cell Phones

From the New York Times:
For a decade consumers have been able to keep their cellphone numbers even if they switched their wireless carriers. On Monday, the Obama administration and the Federal Communications Commission said consumers should also be able to switch carriers and keep their actual phones.

For consumers, being able to take their iPhone or any other type of handset with them when they switch carriers could make it easier to take advantage of lower rates once an initial contract is fulfilled. That might mean more price competition and more choices for cellphone customers. 

The administration and the F.C.C., under Julius Genachowski, announced that they will urge Congress to overturn a ruling last year by the Copyright Office of the Library of Congress that made it illegal for consumers to unlock their cellphones, opening the software that restricts most phones from working on another carrier’s network. . . .

Without a change, the potential consequences for unauthorized unlocking, under the Digital Millennium Copyright Act, are stiff: a $500,000 fine and five years in prison. 

Source:http://blog.agupieware.com/2013/03/white-house-backs-righ-to-unlock-cell.html

How to create Immutable Class and Object in Java - Tutorial Example




Writing or creating immutable classes in Java is becoming popular day by day, because
of concurrency and multithreading advantage provided by immutable objects. Immutable
objects offers several benefits over conventional mutable object, especially while
creating concurrent Java application. Immutable object not only guarantees safe
publication of object’s state, but also can be shared among other threads
without any external synchronization. In fact JDK
itself contains several immutable classes like String, Integer and other wrapper
classes. For those, who doesn’t know what is immutable class or object,
Immutable objects are those, whose state can not be changed once created e.g.
java.lang.String, once
created can not be modified e.g. trim, uppercase, lowercase. All modification in
String result in new object, see why String is immutable in Java
for mor
e details. In this Java programming tutorial, we will learn, how to
write immutable class in Java or how to make a class immutable. By the way making
a class immutable is not difficult on code level, but its the decision to make,
which class mutable or immutable which makes difference. I also suggest reading,
Java Concurrency in Practice
to learn
more about concurrency benefit offered by Immutable object.







What is immutable class in Java



What is Immutable class and object, how to create Immutable in Java with exampleAs said earlier, Immutable classes are those class, whose object can not be modified
once created, it means any modification on immutable object will result in
another immutable object. best example to understand immutable and mutable
objects are, String and StringBuffer. Since
String is immutable class, any change on existing string object will result in
another string e.g. replacing a character into String, creating substring from String,
a
ll result in a new objects. While in case of mutable object like
StringBuffer, any
modification is done on object itself and no new objects are created. Some times
this immutability of String can also cause security hole, and that the reason why password should be stored on char
array instead of String
.









How to write immutable class in Java



Despite of few disadvantages, Immutable object still offers several
benefits in multi-threaded programming and it’s a great choice to achieve thread safety in Java code.
here are few rules, which helps to make a class immutable in Java :





1. State of immutable object can not be modified after construction, any
modification should result in new immutable object.


2. All fields of Immutable class should be final.


3. Object must be properly constructed i.e. object reference must not
leak during construction process.


4. Object should be final in order to restrict sub-class for altering
immutability of parent class.





By the way, you can still create immutable object by violating few rules,
like String has its hashcode in non final field,
but its always guaranteed to be same. No matter how many times you calculate it,
because it’s calculated from final fields, which is guaranteed to be same. This
required a deep knowledge of Java memory model, and can create subtle race conditions if not
addressed properly. In next section we will see simple example of writing
immutable class in Java. By the way, if your Immutable class has lots of optional and mandatory fields, then you can also use Builder design pattern to make a class Immutable in Java.





Immutable Class Example in Java



Here is complete code example of writing immutable class in Java. We have
followed simplest approach and all rules for making a class immutable,
including it making class final to avoid
putting immutability at risk due to Inheritance and Polymorphism.






public final class Contacts {





    private final String name;


    private final String mobile;





    public Contacts(String name, String mobile) {


        this.name = name;


        this.mobile = mobile;


    }


  


    public String getName(){


        return name;


    }


  


    public String getMobile(){


        return mobile;


    }


}






This Java class is immutable, because its state can not be changed once
created. You can see that all of it’s fields are final. This is one of the most
simple way of creating immutable class in Java, where all fields of class also
remains immutable like String in above case. Some time you may need to write
immutable class which includes mutable classes like java.util.Date, despite
storing Date into final field it can be modified
internally, if
internal date is returned to the client. In order to preserve immutability in
such cases, its advised to return copy of original object, which is also
one of the Java best practice. here is
another example of making a class immutable in Java, which includes mutable member
variable.






public final class
ImmutableReminder{


    private final Date remindingDate;


  


    public ImmutableReminder (Date remindingDate)
{


        if(remindingDate.getTime() < System.currentTimeMillis()){


            throw new IllegalArgumentException("Can not
set reminder”
+


                        “ for past time: " + remindingDate);


        }


        this.remindingDate = new Date(remindingDate.getTime());


    }


  


    public Date getRemindingDate() {


        return (Date) remindingDate.clone();


    }


}






In above example of creating immutable class, Date is a mutable object. If getRemindingDate() returns
actual Date object than despite
remindingDate being final variable, internals of
Date can be modified by client code. By returning
clone() or copy of
remindingDate, we avoid that danger and preserves immutability of
class.





Benefits of Immutable Classes in Java



As I said earlier Immutable classes offers several benefits, here are few
to mention:





1) Immutable objects are by default thread safe, can be shared
without synchronization in concurrent environment.


2) Immutable object simplifies development, because its easier to share
between multiple threads without external synchronization.





3) Immutable object boost performance of Java application by reducing synchronization in code.



4) Another important benefit of Immutable objects is reusability, you can cache Immutable object and reuse them, much like String literals and Integers.  You can use static factory methods to provide methods like valueOf(), which can return an existing Immutable object from cache, instead of creating a new one.





Apart from above advantages, immutable object has disadvantage of
creating garbage as well. Since immutable object can not be reused and they are
just a use and throw. String being a prime example, which can create lot of
garbage and can potentially slow down application due to heavy garbage collection, but
again that's extreme case and if used properly Immutable object adds lot of
value.





That's all on how to write immutable class in Java. we have seen
rules of writing immutable classes, benefits offered by immutable objects and
how we can create immutable class in Java which involves mutable fields. Don’t
forget to read more about concurrency benefit offered by Immutable object in
one of the best Java book recommended to Java programmers, Concurrency Practice in Java.








Related Java programming concepts and tutorials





























Source:http://javarevisited.blogspot.com/2013/03/how-to-create-immutable-class-object-java-example-tutorial.html

Steps to Create JUnit Test in Eclipse and Netbeans IDE




Writing Junit tests for Java classes in Eclipse and Netbeans IDE are
super easy, and I will show you with that later in this JUnit tutorial. Before
that, let’s revise what is unit test and why should you write them. Unit test is
to test smaller unit of code, e.g. methods. 
Writing unit test to test individual unit of code is one of the best
development practice and helps to find bug earlier in development cycle. Though
there are other unit testing framework available in Java e.g.
TestNG, JUnit has it’s
own place among Java developers. IMHO code review and unit testing are
two most important practices for improving code quality and should always be
followed during software development. Sad thing is that not every developer
follows it; some programmer don’t write unit test due to ignorance and others
due to laziness. Any way, it’s just start which take time, once you start
writing unit tests, you will automatically start enjoying it. I have seen Java
developers testing there code with main() method, but now they prefer
to test them with
JUnit testcases. I agree few initial tests are difficult
because of knowledge and inertia and best way to approach is to start with
simplest of JUnit tests. In this JUnit tutorial, I will show you how to write and execute JUnit test from
Eclipse and Netbeans
, two popular Java IDE. By the way, if you are looking for any good book on JUnit and unit testing, you should look Pragmatic Unit Testing in Java with JUnit, it's an amazing book and teaches a lot about both JUnit and unit testing in Java.






JUnit 3 and JUnit 4 testing framework



JUnit frameworks is popular from quite a sometime now and there are two popular
versions available in form of
JUnit 3.8, known as JUnit
3
and  JUnit
4
. Working of both versions are same, as you create testcases, testsuite
and execute them. Main difference between JUnit 4 and JUnit 3 is that,
JUnit4 is based
on annotation feature of Java 1.5  and
easy to write, while
JUnit 3 uses “test” keyword,
to identify test methods. What is bonus in terms of writing
JUnit tests for
Java program is that, two of most popular Java IDE, Eclipse and Netbeans has inbuilt support and
provides easy interface and infrastructure to create and execute JUnit tests
for Java classes. In this JUnit 4 tutorial, we will see step by step guide for writing
JUnit test in Eclipse and Netbeans
with simple example of
Calculator. Our Calculator class has
methods
add() and multiply() for addition
and multiplication, I have used variable arguments of Java 1.5 to implement these
methods, so that they can accept any number of parameter.





How to write JUnit tests in Eclipse



1. Create a New Java Project called JUnitExample.


2. Create a Java class Calculator in project
which should have
add() and multiply() method.


3. Right click on Java class and click on create Junit
testcase





How to write JUnit test case in Eclipse JAvaHow to execute JUnit tests in Eclipse



How to run Unit test using Eclipse in JavaNow you have your Junit test created, you can execute it just like you
run any Java application:





Right Click --> Run As --> Junit Test





This will run all the JUnit tests
declared in this class and will pass if all the test run successfully and pass
the condition tested by various assert statement and fail if
any of JUnit tests failed. Eclipse will print stack trace and hyper link to the
failed test and you can go and fix the problem.











Why are my JUnit results not showing up?


Some time when your all JUnit tests passed you may not see any result, because of an option called "Show Failures Only". For more satisfying results, go to the downward-pointing triangle at the right end of the JUnit pane, and make sure Show Failures Only is not checked, as shown in following screenshot. Some time your JUnit test will not succeed i.e. it will fail or run into error. In JUnit, there is difference between a "failure" and an "error"? A failure is when one of your assertions fails--that is, your program does something wrong, and your JUnit test notices and reports the fact. An error is when some other Exception occurs due to programming mistakes, such as a NullPointerException or an ArrayIndexOutOfBoundsException. Both error and failure are not good for your code. A good Junit test case should be able to bring failure and error a like. You should also ensure that your JUnit testcase should pass always and doesn't throw any error or failures.




How to see result of JUnit test in Eclipse IDE












How to create JUnit Test suit in Eclipse


Like individual unit tests, you can also create a test suite for creating tests for more than one classes in Java. In order to create JUnit test suite in Eclipse  Go to File → New → Other... → Java → JUnit → TestSuite, and click Next>. Select all the classes, and click Finish. Once created, You can run this test suite the same way you run other JUnit tests. Result of JUnit test suite will also show in JUnit console like previous run.




How to create JUnit test suite in Eclipse IDE






How to write JUnit tests in Netbeans



Junit support in Netbeans is also great and seamless. Here is the steps
to create JUnit test in Netbeans





1. Create a New Java Project called JUnitExample.


2. Create a Java Class Calculator in project
which should have
add() and multiply() method.


3. Now Select a Java Class --> Right click --> Tools
--> Create Junit tests


this will create Junit test class for all the methods of selected Java class.





How to execute Junit tests in Netbeans



Executing JUnit tests in Netbeans is much simpler than it was in Eclipse.
Go to your Junit test class and right click and select
run
File
option. This will execute all the JUnit tests on File and show the
result in console. As earlier test will
be pass if all test method passes otherwise it will fail. Netbeans also shows complete stack trace and hyperlink of failed test cases.





Code


Here is complete code example of, How
to write unit test in Java using JUnit framework
. In this example, we have
a Java class called
Calculator, which has two methods add() and multiply() and takes
variable arguments. In this JUnit tutorial, we will write JUnit testcases, to
test these two methods.






/**


 * Simple Java Calculator with add and multiply
method


 */


public class Calculator {





        public int add(int... number)
{


                int total = 0;


                for (int i : number) {


                        total += i;


                }


                return total;


        }





        public int multiply(int... number)
{


                int product = 0;


                for (int i : number) {


                        product *= i;


                }


                return product;


        }





}









Following class CalculatorTest is our JUnit test class, it
contains two methods
testAdd() and testMultiply(). Since we
are using JUnit 4, we don’t need to use prefix
test, but I have
used that to make test methods explicit.
@Test annotation
is used by JUnit 4 framework to identify all test cases. If you are new to
JUnit 4, then see this post to learn more about JUnit 4 annotations. By the
way when we run this class as JUnit Test, it will show how many test cases pass
and how many failed. If all the test cases pass then it will show green bar,
otherwise red bar to indicate failure.






import static
org.junit.Assert.*;


import org.junit.Test;





/**


 * JUnit Test class for testing methods of
Calculator class.


 */


public class CalculatorTest {





        @Test


        public void testAdd() {


                Calculator calc = new Calculator();


                assertEquals(60, calc.add(10,20,30));


        }





        @Test


        public void testMultiply() {


                Calculator calc = new Calculator();


                assertEquals(6000, calc.multiply(10,20,30));


        }





}






That’s all on How to write unit test in Java using JUnit framework in
Eclipse and Netbeans
. JUnit testing framework is is very useful for writing
and executing unit test for Java classes and it can also be integrated into
build system by using ANT and Maven, which means you
can all your tests automatically at build time. By the way since many project
have there JUnit test running during build process, it’s good to keep  unit test short and quickly executable to
avoid lengthy build. See JUnit best practices guide
more unit testing practices. Also start using Junit 4 annotations, they have
made job of writing unit tests much easier. 




Recommended Book: Pragmatic Unit Testing in Java with JUnit

































Source:http://javarevisited.blogspot.com/2013/03/how-to-write-unit-test-in-java-eclipse-netbeans-example-run.html