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

Post a Comment

Labels

Java (159) Lucene-Solr (112) Interview (61) All (58) J2SE (53) Algorithm (45) Soft Skills (38) Eclipse (33) Code Example (31) Linux (25) JavaScript (23) Spring (22) Windows (22) Web Development (20) Tools (19) Nutch2 (18) Bugs (17) Debug (16) Defects (14) Text Mining (14) J2EE (13) Network (13) Troubleshooting (13) PowerShell (11) Chrome (9) Design (9) How to (9) Learning code (9) Performance (9) Problem Solving (9) UIMA (9) html (9) Http Client (8) Maven (8) Security (8) bat (8) blogger (8) Big Data (7) Continuous Integration (7) Google (7) Guava (7) JSON (7) Shell (7) ANT (6) Coding Skills (6) Database (6) Lesson Learned (6) Programmer Skills (6) Scala (6) Tips (6) css (6) Algorithm Series (5) Cache (5) Dynamic Languages (5) IDE (5) System Design (5) adsense (5) xml (5) AIX (4) Code Quality (4) GAE (4) Git (4) Good Programming Practices (4) Jackson (4) Memory Usage (4) Miscs (4) OpenNLP (4) Project Managment (4) Spark (4) Testing (4) ads (4) regular-expression (4) Android (3) Apache Spark (3) Become a Better You (3) Concurrency (3) Eclipse RCP (3) English (3) Happy Hacking (3) IBM (3) J2SE Knowledge Series (3) JAX-RS (3) Jetty (3) Restful Web Service (3) Script (3) regex (3) seo (3) .Net (2) Android Studio (2) Apache (2) Apache Procrun (2) Architecture (2) Batch (2) Bit Operation (2) Build (2) Building Scalable Web Sites (2) C# (2) C/C++ (2) CSV (2) Career (2) Cassandra (2) Distributed (2) Fiddler (2) Firefox (2) Google Drive (2) Gson (2) How to Interview (2) Html Parser (2) Http (2) Image Tools (2) JQuery (2) Jersey (2) LDAP (2) Life (2) Logging (2) Python (2) Software Issues (2) Storage (2) Text Search (2) xml parser (2) AOP (1) Application Design (1) AspectJ (1) Chrome DevTools (1) Cloud (1) Codility (1) Data Mining (1) Data Structure (1) ExceptionUtils (1) Exif (1) Feature Request (1) FindBugs (1) Greasemonkey (1) HTML5 (1) Httpd (1) I18N (1) IBM Java Thread Dump Analyzer (1) JDK Source Code (1) JDK8 (1) JMX (1) Lazy Developer (1) Mac (1) Machine Learning (1) Mobile (1) My Plan for 2010 (1) Netbeans (1) Notes (1) Operating System (1) Perl (1) Problems (1) Product Architecture (1) Programming Life (1) Quality (1) Redhat (1) Redis (1) Review (1) RxJava (1) Solutions logs (1) Team Management (1) Thread Dump Analyzer (1) Visualization (1) boilerpipe (1) htm (1) ongoing (1) procrun (1) rss (1)

Popular Posts