Recently asked Spring Interview Questions and Answers?

Explain Hibernate Architecture in Detail?
 Hibernate also provides built-in Transaction APIs which abstracts away the application from underlying JDBC or JTA transaction. Each transaction represents a single atomic unit of work. One Session can span through multiple transactions.

SessionFactory (org.hibernate.SessionFactory)

  • A thread-safe, immutable cache of compiled mappings for a single database. A factory for org.hibernate.Session instances. A client of org.hibernate.connection.ConnectionProvider. Optionally maintains a second level cache of data that is reusable between transactions at a process or cluster level.
  • Session (org.hibernate.Session)
  • A single-threaded, short-lived object representing a conversation between the application and the persistent store. Wraps a JDBC java.sql.Connection. Factory for org.hibernate.Transaction. Maintains a first level cache of persistent the application’s persistent objects and collections; this cache is used when navigating the object graph or looking up objects by identifier.
  • Persistent objects and collections
  • Short-lived, single threaded objects containing persistent state and business function. These can be ordinary JavaBeans/POJOs. They are associated with exactly one org.hibernate.Session. Once the org.hibernate.Session is closed, they will be detached and free to use in any application layer (for example, directly as data transfer objects to and from presentation).
  • Transient and detached objects and collections
  • Instances of persistent classes that are not currently associated with a org.hibernate.Session. They may have been instantiated by the application and not yet persisted, or they may have been instantiated by a closed org.hibernate.Session.
  • Transaction (org.hibernate.Transaction)
  • (Optional) A single-threaded, short-lived object used by the application to specify atomic units of work. It abstracts the application from the underlying JDBC, JTA or CORBA transaction. A org.hibernate.Session might span several org.hibernate.Transactions in some cases. However, transaction demarcation, either using the underlying API or org.hibernate.Transaction, is never optional.
  • ConnectionProvider (org.hibernate.connection.ConnectionProvider)
  • (Optional) A factory for, and pool of, JDBC connections. It abstracts the application from underlying javax.sql.DataSource or java.sql.DriverManager. It is not exposed to application, but it can be extended and/or implemented by the developer.
  • TransactionFactory (org.hibernate.TransactionFactory)
  • (Optional) A factory for org.hibernate.Transaction instances. It is not exposed to the application, but it can be extended and/or implemented by the developer.

 How are Spring beans injected ?
A dependency graph is constructed based on the various bean definitions
Beans are created using constructors (mostly no-arg) or factory methods
Dependencies that were not injected via constructor are then injected using setters
Any dependency that has not been created is created as needed

How are Spring beans created ?
How Spring will instantiate beans?
Beans are created in order based on the dependency graph
  • Often they are created when the factory loads the definitions
  • Can override this behavior in bean <bean class=“className” lazy-init=“true” />
  • You can also override this in the factory or context but this is not recommended
Spring will instantiate beans in the order required by their dependencies
  • app scope singleton - eagerly instantiated at container startup
  • lazy dependency - created when dependent bean created
  • VERY lazy dependency - created when accessed in code
Important Points to Remember while Coding for Java Developers?
Necessary Tabs and spaces in Blocks. (Theoretically you can write a long program in a single line) 

  • Use getter, setter, and is methods.
        int getCount() ;
        int setCount() ;
        isEmpty() ;

Self-checking classes 
Use main() almost in every class to check class functionality. 
You can make an expected output check, in order to verify changes in the class.

  • In highly coupled classes, consider to make inner classes.
  • Keep method definitions and scopes as short as possible. (For easy visualization and readability.)
  • Compiler-time errors are better then run time errors.
  • Prefer Interface definition to Abstract Class
       Not : abstract class X { abstract void f() ; }
       But : interface X { void f() ;
Spelling problems in overriding Classes
        class X { void encode() { … } }
        class Y extends X { void Encode() { … } }    // Does not overload the method
  • Use Java Container Library classes
List l = new ArrayList() ;
Map hm = new HashMap() ;
Set s = new HashSet() ;
List  myStack = new LinkedList() ;

Explain Aspect Oriented Programming (AOP)?

Aspect Oriented Programming (AOP) 
Enables encapsulation of functionality that affects multiple classes in separate units
• Complements object oriented programming
• Most popular implementation for Java is AspectJ
Aspect oriented extension for Java
Based on Eclipse, available as plugin and stand-alone