Posts

How to take input from user in Java using Scanner

Image
One of the strengths of Java is the huge libraries of code available to you. This is code that has been written to do specific jobs. All you need to do is to reference which library you want to use, and then call a method into action. One really useful class that handles input from a user is called the  Scanner  class. The Scanner class can be found in the  java.util  library. To use the Scanner class, you need to reference it in your code. This is done with the keyword  import . import   java.util.Scanner; We first create an object of Scanner class and then we use the methods of Scanner class. Consider the statement Scanner a = new Scanner(System.in); Here Scanner is the class name, a is the name of object, new keyword is used to allocate the memory and System.in is the input stream. Following methods of Scanner class are used in the program below :- 1.       nextInt() to input an integer 2.   ...

Spring Bean Life Cycle

Image
The beans  life cycle  in spring is one of the most important features to understand. In many of the real time applications, it is necessary to perform some of the operations before  initializing  a bean and it is necessary to perform some cleanup operations before the bean is  destroyed  by the container. In Java, The life cycle of an object begins with  new  keyword. When we create an object using  new , that time it calls the series of hierarchical class  constructors (call goes from bottom to top and hence execution from top to bottom) and finally makes the object available. When this object will not have any reference, it will be  garbage collected . This is the simple life cycle of an object in Java. But in spring, Bean’s life cycle is having few more things to do. The spring bean’s life cycle is as shown below 1)     Spring container looks for the definition of the  bean  in the spri...

Spring bean tag properties (attributes)

Spring bean tag properties (attributes) In spring the beans are managed by Spring IoC container, these are backbone of the application. You can instantiate and manage them in your application using configurations. In xml based spring bean configurations, using <bean> tag, you can manage them. Here we have given complete list of bean tag properties: name / id: This attribute specifies the bean unique identifier. In XML based configuration metadata, you use the id and/or name attributes to specify the bean identifier. class: This attribute is mandatory and specify the bean class to be used to create the bean. You should specify fully qualified class name. Include package name. scope: This attribute specifies the scope of the objects created from a particular bean definition. The scope values can be prototype, singleton, request, session, and global session. constructor-arg: This is used to inject the dependencies through bean constructor. propertie...

Spring 3 Dependency Injection via Constructor and Setter

Spring Framework implementation of the Inversion of Control (IoC) principle, that is also known as Dependency Injection (DI). Dependency injection (DI) is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. DI exists in two major variants, Constructor-based dependency injection and Setter-based dependency injection. Constructor Based DI Example Employee.java package  smgc; public class Employee {                 private int  employeeId;                 private String employeeName;       ...