Search This Blog

Wednesday 1 February 2017

BeanFactory and ApplicationContext

The org.springframework.beans and org.springframework.context packages are the two most important and fundamental packages in Spring. These packages provides the basis for Spring's Inversion of Control (Dependency Injection) features.
The BeanFactory provides configuration mechanism which is capable of managing beans (objects). The ApplicationContext is build on top of the BeanFactory which adds other functionality such as message resource handling (for use in internationalization), event propagation, easier integration with Springs AOP features, declarative mechanisms.
 The BeanFactory provides the configuration framework and basic functionality, while the ApplicationContext adds enhanced capabilities to it, some of them perhaps more J2EE and enterprise-centric. ApplicationContext is a superset of a BeanFactory, and BeanFactory capabilities and behavior apply to ApplicationContexts as well.
At times we often face the question as whether a BeanFactory or an ApplicationContext are best suited for use in a particular situation. Normally when building most applications in a J2EE-environment, the best option is to use the ApplicationContext, since it offers all the features of the BeanFactory and adds on to it in terms of features, while also allowing a more declarative approach to use of some functionality, which is desirable. In certain cases where memory usage is concerned one might prefer to use BeanFactory over Application Context.

The BeanFactory

The BeanFactory is the container which instantiates, configures, and manages a number of beans. These beans typically collaborate with one another.
A BeanFactory is represented by the  interface  org.springframework.beans.factory.BeanFactory, for which there are multiple implementations. The most commonly used simple BeanFactory implementation is org.springframework.beans.factory.xml.XmlBeanFactory.
The BeanFactory is instantiated as below for different implementation:
Resource res = new FileSystemResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
or
ClassPathResource res = new ClassPathResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
or
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
        new String[] {"applicationContext.xml", "applicationContext-part2.xml"});
BeanFactory factory = (BeanFactory) appContext;

The ApplicationContext

The  ApplicationContext, enhances BeanFactory functionality in a more framework-oriented style.
The ApplicationContext interface, is located in the  org.springframework.context package. Its is derived from the BeanFactory interface, and provides all the functionality of BeanFactory. To allow working in a more framework-oriented fashion, using layering and hierarchical contexts, the context package also provides the following:
  • MessageSource:  providing access to messages in, i18n-style
  • Access to resources: such as URLs and files
  • Event propagation: to beans implementing the ApplicationListener interface
  • Loading of multiple (hierarchical) contexts: allowing each to be focused on one particular layer.
As the ApplicationContext includes all functionality of the BeanFactory, it is generally recommended that it be used over the BeanFactory, except for a few limited situations where memory consumption might be critical, and a few extra kilobytes might make a difference. 


ApplicationContext
ApplicationContext

Monday 30 January 2017

Java Quiz 01 - Answers

Here are the Answers


Question Number
Correct Answer
1
A,B,C
2
A, D
3
D
4
D
5
A
6
A
7
A
8
A
9
A
10
C
11
A
12
B
13
A
14
D
15
A, B
16
C
17
B, D
18
A
19
A
20
C

Java Quiz 01


Take the Java Quiz


    1.     Which of the following is true Statement?
A.    A class that is abstract cannot not be instantiated
B.    final variable are constant variable
C.    A static variable indicates there is only one copy of that variable
D.    A method defined as private indicates that it is accessible to all other classes in the same package


    2.     Which is a valid keyword in java?
A.    int
B.    Boolean
C.    Integer
D.    Instanceof


    3.     Given :
public class A{
 class B{
  }
}
On compiling the code using
javac A.java
What all .class files will be created?"
A.      A.class
B.      B.class
C.      A.class AND B.class
D.      A.class AND A$B.class

     4.     Given:
public class TestString1 {
public static void main(String[] args) {
String str = "420";
str += 42;
System.out.print(str);
}
}
What is the output?
A.    42
B.    420
C.    462
D.    42042

    5.     Identify the correct syntax to create a user defined exception which handles exception at compile time
A.    class MyException extends Exception {}
B.    class MyException extends RuntimeException {}
C.    class MyException extends Error {}
D.    None from the option

    6.     Which of the following command will create a non-executable jar file
A.    java -cf jarname.jar classfiles
B.    java  jarname.jar classfiles
C.    java -cf classfiles jarname.jar
D.    None from the option


    7.     Exception given by  compiler  at compile time  are known as
A.    checked exception
B.    unchecked exception
C.    public exception
D.    None from the option

    8.     Which of the following is correct Statement for annotation?
A.    annotation is information for compiler
B.    none of the options
C.    annotation is use for compilation
D.    annotation is use to handle exception

    9.     Which class in java which can parse primitive types and strings using regular expressions
A.    Scanner
B.    Reader
C.    InputStream
D.    BufferedReader


   10.  Which of the following is part of Character stream?
A.    FileInputStream
B.    BufferedInputStream
C.    FileReader
D.    ObjectInputStream

   11.  Given:
import java.util.*;
public class Example {
public static void main(String[] args) {
// insert code here
set.add(new Integer(2));
set.add(new Integer(1));
System.out.println(set);
}
}
Which code, inserted at line // insert code here, guarantees that this program will
output [1, 2]?
A.    Set set = new TreeSet();
B.    Set set = new HashSet();
C.    Set set = new LinkedHashSet();
D.    None from the option

    12.  Given a class whose instances, when found in a collection of objects, are sorted by using the compare() method, which  statements are true?
A.    The class implements java.lang.Comparable.
B.    The class implements java.lang.Comparator
C.    The interface used to implement sorting allows this class to define only one sort
sequence
D.    The interface used to implement sorting allows this class to define many different sort
sequences.




    13.  Which interface provides the capability to store objects using a key-value pair?
A.    Java.util.Map
B.    Java.util.List
C.    Java.util.Set
D.    Java.util.Collection

    14.  Which type of driver converts JDBC calls into the network protocol used by the database management system directly?
A.    Type 1 driver
B.    Type 2 driver
C.    Type 3 driver
D.    Type 4 driver

    15.  Which of the following methods are needed for loading a database driver in JDBC?
A.    registerDriver() method
B.    Class.forName()
C.    getDriver()
D.    getConnection()

    16.  All the method should be preceded with the keyword _____________ , If the methods of an object should only be executed by one thread at a time
A.    Asynchronized
B.    Serialized
C.    Synchronized
D.    None from the option

    17.  Which of the following synchronized method or block will compile and run without exception?
A.    private synchronized Object o;
B.    public synchronized void go() { /* code here */ }
C.    private synchronized(this) void go() { /* code here */ }
D.    void go() {
synchronized(Object.class) { /* code here */ }

    18.  An object that can be used to get information about the types and properties of the columns in a ResultSet object is _____________________
A.    ResultSetMetaData
B.    DatabaseMetaData
C.    ColumnMetaDeta
D.    None from the option
   19.  Given:
public class MyThread implements Runnable{
//your code here
}

Which method definition in // your code here completes the class?
A.    void run()
B.    void start()
C.    void execute()
D.    void runThread()

   20.  A call to this methods invoked on a thread will wait and not return until either the thread has completed or it is timed out after the specified time.
A.    sleep()
B.    yeild()
C.    join()
D.    run()