J2EE interview -- Spring


J2EE interview -- Spring

Spring Overview Spring is a lightweight, complete solution that makes building enterprise Java applications much easier.

Modularity is its key feature, which allows enterprise application developers to use only the modules they require from the stack. IoC container

Inversion of Control is also known as dependency injection (DI). It is a process whereby objects define their dependencies via constructor, or setter method. The container then injects those dependencies when it creates the bean. AOP Second AOP, Aspect-Oriented Programming complements Object-Oriented Programming (OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Aspects enable the abstraction and modularization of crosscutting concern such as transaction management, logging, validation, authentication, authorization. Traditional object-oriented approach would cause two problems.

1. Code tangling: methods would include core logic and other crosscutting concerns; this will lead to poor code maintainability and reusability.

2. Code scattering: we have to repeat same statements and logics multiple times in multiple modules to fulfill a single requirement, if later we need to change the logic; we have to change all places.

AOP provides another way to implement crosscutting concerns. We implement the crosscutting concerns as aspects, and then through XML or Annotation, we weave aspects into various objects, in this way, we place the crosscutting concern in only one place. Spring has its own AOP framework, which successfully addresses the 80%s of AOP requirements. Also Spring provides integration with AspectJ, which is the richest and certainly most mature AOP implementation in the Java enterprise space. AOP is used in the Spring Framework to... • ... Provide declarative enterprise services. • ... Allow users to implement custom aspects. Data Access layer

Spring provides a comprehensive and consistent abstraction for transaction management. Spring provides consistent exception hierarchy, Spring wraps JDBC exceptions, Hibernate JDO and JPA specific exceptions, converts them from proprietary, checked exceptions to a set of runtime, unchecked exceptions, inherited from DataAccessException. This allows one to handle most persistence exceptions, which are non-recoverable, only in the appropriate layers, without having annoying boilerplate catch-and-throw blocks and exception declarations in one's DAOs. We can still trap and handle exceptions anywhere one needs to though. Spring JDBC abstraction simplifies the use of JDBC, by providing APIs that move tedious and error-prone exception handling out of application code into the framework. The framework takes care of all exception handling, Open the connection, Handle transactions, Close the connection, statement and resultset, application code can concentrate on issuing the appropriate SQL and extracting results. Spring supports integration with Hibernate, JPA, JDO and iBATIS. Spring also provides support for Object/XML Mapping, that is to convert an XML document to and from an object. Spring MVC Fourthly, In Web Layer, Spring provides Spring MVC, Spring's MVC model is most similar to that of Struts, But Spring MVC has some significant advantages over Struts.

Spring provides a very clean division between controllers, JavaBean models, and views.

Reusable business code, no need for duplication. we can use existing business objects as command or form objects instead of mirroring them and create a similar DTO object, such as formbean in struts1.

Spring, like WebWork, provides interceptors as well as controllers, making it easy to factor out behavior common to the handling of many requests.

Spring MVC is truly view-agnostic. You can choose to use nay view technologies like JSP, Velocity, XLST, iText, or POI, you can even write your custom view mechanism.

Spring Controllers are configured via IoC like any other objects. This makes them easy to test, and beautifully integrated with other objects managed by Spring.

Spring also provides a JSP form tag library, that makes writing forms in JSP pages much easier.

Spring MVC web tiers are typically easier to test than Struts web tiers, due to the avoidance of forced concrete inheritance and explicit dependence of controllers on the dispatcher servlet.

Also we can choose other web frameworks, and Srping provide integration with most of popular third party web frameworks such as struts1/2, JSF. Spring also makes it much easier to access, use EJBs and implement EJBs. Integration Spring provides a consistent integration and abstraction framework for many Java EE related technologies, such as JMS, JMX, Email, task scheduling, and integrate with other dynamic languages. Spring simplifies the use of these API remarkably. Spring also provides Security framework, Spring Security, previously call Acegi. The IoC container IoC(Inversion of Control ) is also known as dependency injection (DI). It is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor, or setter method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse of the bean itself controlling the instantiation or location of its dependencies by using direct construction of classes, or a mechanism such as the Service Locator pattern. Spring provides BeanFactory and ApplicationContext. The BeanFactory provides the configuration framework and basic functionality, and the ApplicationContext is a sub-interface of BeanFactory, and adds more enterprise-specific functionality. Spring supports XML-based and annotation-based configuration. Are annotations better then XML for configuring Spring? Due to the way they are defined, annotations provide a lot of context in their declaration leading to shorter, more concise configurations. However XML excels at wiring up components without touching their source code or recompile them. Some prefer having the wiring close to the source while others argue that annotated classes are no longer POJOs and further more that the configuration becomes decentralized and harder to control. Annotation-based container configuration Spring provides @Resource, @PostConstruct, and @PreDestroy, @Inject, @Required etc... Classpath scanning and managed components @Component and further stereotype annotations @Component is a generic stereotype for any Spring-managed component. @Repository, @Service, and @Controller are specializations of @Component for more specific use cases, for example, in the persistence, service, and presentation layers, respectively. Aspect-Oriented Programming (AOP) AOP concepts • Aspect: a modularization of a concern that cuts across multiple classes. Transaction management is a good example of a crosscutting concern in Java EE applications. • Join point: a point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution. • Advice: action taken by an aspect at a particular joins point. Different types of advice include "around," "before" and "after" advice. Many AOP frameworks, including Spring, model an advice as an interceptor, maintaining a chain of interceptors around the join point. • Pointcut: a predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name). The concept of join points as matched by pointcut expressions is central to AOP, and Spring uses the AspectJ pointcut expression language by default. Types of advice: Before advice, After returning advice, After throwing advice, After (finally) advice, Around advice. Data Access Layer In Data Access layer, Spring provides a comprehensive and consistent abstraction for transaction management, that delivers the following benefits: • Consistent programming model across different transaction APIs such as Java Transaction API (JTA), JDBC, Hibernate, Java Persistence API (JPA), and Java Data Objects (JDO). • Support for declarative transaction management and Programmatic transaction management. Also Spring's DAO support aims at making it easy to work with data access technologies like JDBC, Hibernate, JPA or JDO in a consistent way. This allows one to switch between these persistence technologies fairly easily. Consistent exception hierarchy Spring provides consistent exception hierarchy. Spring provides a convenient translation from technology-specific exceptions like SQLException to its own exception class hierarchy with the DataAccessException as the root exception.

Spring wraps JDBC exceptions, Hibernate JDO and JPA specific exceptions, converts them from proprietary, checked exceptions to a set of runtime, unchecked exceptions, inherited from DataAccessException. This allows one to handle most persistence exceptions, which are non-recoverable, only in the appropriate layers, without having annoying boilerplate catch-and-throw blocks and exception declarations in one's DAOs. We can still trap and handle exceptions anywhere one needs to though. Spring JDBC abstraction JDBC offers fairly good abstraction from the underlying database, but is a painful API to use. Some of the problems include: The need for verbose error handling to ensure that ResultSets, Statements and (most importantly) Connections are closed after use. This means that correct use of JDBC can quickly result in a lot of code. It's also a common source of errors. Connection leaks can quickly bring applications down under load. The relatively uninformative SQLException. JDBC does not offer an exception hierarchy, but throws SQLException in response to all errors. Finding out what actually went wrong involves examining the SQLState value and error code. The meaning of these values varies between databases. Spring addresses these problems in two ways: By providing APIs that move tedious and error-prone exception handling out of application code into the framework. The framework takes care of all exception handling, Open the connection, Handle transactions, Close the connection, statement and resultset, application code can concentrate on issuing the appropriate SQL and extracting results. By providing a meaningful exception hierarchy for your application code to work with in place of SQLException. Spring ORM Support Spring supports integration with Hibernate, Java Persistence API (JPA), Java Data Objects (JDO) and iBATIS for resource management, data access object (DAO) implementations, and transaction strategies. Benefits of Spring ORM Support include: Common data access exceptions. Spring can wrap exceptions from your ORM tool, converting them from proprietary (potentially checked) exceptions to a common runtime DataAccessException hierarchy. This feature allows you to handle most persistence exceptions, which are non-recoverable, only in the appropriate layers, without annoying boilerplate catches, throws, and exception declarations. You can still trap and handle exceptions as necessary. Spring's Object/XML Mapping support Spring also provides support for Object/XML Mapping, that is to convert an XML document to and from an object. The Web Layer Spring MVC is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale and theme resolution. A Controller implementation can even write directly to the response stream. Typically, a ModelAndView instance consists of a view name and a model Map, which contains bean names and corresponding objects such as a command or form, which contain reference data. Also we can choose other web frameworks, and Srping provide integration with most of popular third party web frameworks such as struts1/2, JSF.

This allows one to continue to leverage any and all of the skills one may have acquired in a particular web framework such as Struts, while at the same time being able to enjoy the benefits afforded by Spring in other areas such as data access, declarative transaction management, and flexible configuration and application assembly.

Other features

Spring Expression Language(SpEL)

Object/XML Mapping (OXM) module

Resources Introducing the Spring Framework spring-framework-reference-3.0.pdf

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)