Google

Jun 3, 2014

Spring architecture and understanding its packages and dependencies

Spring Interview Questions and Answers Q1 - Q14 are FAQs

Q1 - Q4 Overview & DIP Q5 - Q8 DI & IoC Q9 - Q10 Bean Scopes Q11 Packages Q12 Principle OCP Q14 AOP and interceptors
Q15 - Q16 Hibernate & Transaction Manager Q17 - Q20 Hibernate & JNDI Q21 - Q22 read properties Q23 - Q24 JMS & JNDI Q25 JDBC Q26 Spring MVC Q27 - Spring MVC Resolvers

Q11. What is the minimal package requirements to get started with Spring framework in your Java project?
A11.

So, your maven pom.xml file should at least have

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>${spring.version}</version>
 </dependency>
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-expression</artifactId>
   <version>${spring.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>${spring.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>${spring.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${spring.version}</version>
 </dependency>



Q. When do you need the spring-context-support package?
A. This is required for integration of EhCache, JavaMail, Quartz, and Freemarker. So, if you are going to use Java Mail to send emails or Quartz to schedule a job, then you need spring-context-support.

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>${spring.version}</version>
</dependency>


Q. What package(s) will you include if you want transaction management support?
A. spring-tx, which depends on spring-core, spring-beans, spring-aop, and spring-context.

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>${spring.version}</version>
</dependency>


Q. What packages will be required for the database access?
A.  spring-jdbc, which depends on  spring-core, spring-beans, spring-context, and spring-tx. if using Object-to-Relation-Mapping (ORM) integration with Hibernate, JPA or iBatis then you need spring-orm, which depends on spring-core, spring-beans, spring-context, and spring-tx.

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${spring.version}</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>${spring.version}</version>
</dependency>


Q. What if you want to use JAXB, JiBX,XStream,  XMLBeans or any other Object-To-Xml(OXM) mapping?
A. You need spring-oxm, which depends on spring-core, spring-beans, and spring-context.

Similarly,
  • spring-test is required for junit testing.
  • spring-web is required if you want to use a web framework like Spring MVC, JSF, Struts, etc, and depends on depends on spring-core, spring-beans, and spring-context.
  • spring-webmvc is required to use Spring as the MVC framework for Web application or RESTFul web service. It depends on spring-core, spring-beans, spring-context, and spring-web.
  • spring-mock.jar containing mock classes to assist with the testing.
  • spring-jms for messaging and depends on spring-core and spring-oxm (i.e. for OXM).

Spring framework architecture is modular with layers like core, data access & integration, web/remoting, and other miscellaneous support.

Q. What is a JAR hell issue, and how will you go about resolving it?
A. The following maven command helps you to find duplicated or overlapping JAR versions.

$ mvn dependency:tree

and then you can exclude them as shown in the example below with "Exclusion"

<dependency>
    <groupId>com.myapp</groupId>
 <artifactId>some-app</artifactId>
 <version>${myapp.version}</version>
 <exclusions>   
  <exclusion>
   <groupId>org.springframework</groupId>
   <artifactId>spring-aop</artifactId>
  </exclusion>
  <exclusion>
   <groupId>org.springframework</groupId>
   <artifactId>spring-beans</artifactId>
  </exclusion>
  <exclusion>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
  </exclusion>
  <exclusion>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context-support</artifactId>
  </exclusion>
  <exclusion>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
  </exclusion>
  <exclusion>
   <groupId>org.springframework</groupId>
      <artifactId>spring-jms</artifactId>
  </exclusion>
  <exclusion>
   <groupId>org.springframework</groupId>
   <artifactId>spring-tx</artifactId>
  </exclusion>
 </exclusions>
</dependency>



Q. Why would you use Spring framework in your next project?
A.
  • A key design principle in Spring in general is the “Open for extension, closed for modification” principle.So, some of the methods in the core classes are marked "final".
  • Spring is an IoC container that supports both constructor injection (supplying arguments to constructors) and setter-based injection (calling setters on a call) to get the benefits of Dependency Injection (DI) like loose coupling, easier to test, etc.
  • It is a light-weight framework.
  • It is modular.
  • It is very matured and has been used in many large Java/JEE applications.
  • It supports AOP to implement cross cutting concerns.
  • It supports transaction management.
  • It promotes uniformity to handle exceptions (e.g. checked vs unchecked) and integration with JMS, JDBC, JNDI, remoting, etc.

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home