Tuesday, April 8, 2014

Spring Core : Explanation of few very important meta data and their properties

Spring is a container-based framework. But if you don’t configure Spring, then it’s an empty container and doesn’t serve much purpose. Programming by exception - the common jingle for Java EE developer right?

So we need to configure Spring to tell what beans it should contain and how to wire those beans so that they can work together. As of Spring 3.0, there are two ways to configure beans in the Spring container. Traditionally, Spring configuration is defined in one or more XML files. But Spring 3.0 also offers a Java-based configuration option. but here i will focus on the traditional XML option.

Spring comes with several XML name-spaces through which you can configure the spring container. Among those beans, aop, context, mvc, oxm, jee and tx are the most frequent used name-spaces in an application. Let see those name-spaces and their properties :


1. beans : The core primitive spring name-space, enabling declaration of beans and how they should be wired.  
         Attributes of a bean:
          
         a. id: Represents how a bean should be referred by the client application.
         b. class: Tells which class a bean points to.
         c. constructor-arg: The element is used to give Spring additional information to use when constructing a bean. If no arguments are given, the default constructor is used.
         d. factory-method: Lets you specify a static method to be invoked instead of the constructor to create an instance of a class. This attribute will allow you to achieve exactly the same thing.
         e. scope: By default, all Spring beans are singletons. This attribute lets you declare the scope of a bean without hard coding the scoping rules in the bean class itself. A scope of a bean might be:
                e.1. singleton(Spring’s singleton beans only guarantee a single instance of the bean definition per the application context)
                e.2. prototype
                e.3. request
                e.4. session
                e.5. global-session
         
         f. init-method: The init-method attribute specifies a method that is to be called on the bean immediately upon instantiation.
         g. destroy-method: destroy-method specifies a method that is called just before a bean is removed from the container.
         h. property: Bean properties can be configured in Spring using this element. Instead of injecting values through a constructor argument, injects by calling a property’s setter method.
                

   
   
      
      
      
   

Note that the value attribute is used exactly the same when setting a numeric value as it is when setting a String value. Spring will determine the correct type for the value based on the property’s type.
      
            
                  
                        
                        
                        
                        
                  
            
      
Just because a property is a java.util.Set, that doesn’t mean that you must use to do the wiring. Even though it may seem odd to configure a java.util.List property using , it’s certainly possible. In doing so, you’ll be guaranteed that all members of the List will be unique. In case of Map, the declaration would be:
  
      
            
                  
                        
                        
                        
                  
              
       

      i. primary & auto-wire-candidate: The primary attribute is only useful for identifying a preferred autowire candidate. If you’d rather eliminate some beans from consideration when auto-wiring, then you can set their auto-wire-candidate attribute to false.

2. context:  The context schema deals with ApplicationContext configuration with the help of its other elements. The elements that context does have are annotation-config, property-placeholder, component-scan, load-time-weaver, spring-configured, mbean-export.


      a. context:annotation-config: tells Spring that you intend to use annotation-based wiring in Spring. Once it’s in place you can start annotating your code to indicate that spring should automatically wire values into properties, methods, and constructors.
       
      b. property-placeholer: This element activates the replacement of ${...} placeholders, resolved against the specified properties file (as a Spring resource location). This element is a convenience mechanism that sets up a PropertyPlaceholderConfigurer for you; if you need more control over the PropertyPlaceholderConfigurer, just define one yourself explicitly. For example follow the configuration below:


      


      
            
            
            
             
      

The actual values come from another file in the standard Java Properties format:


jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root

      c. context:component-scan: The element does everything that does, plus it configures Spring to automatically discover beans and declare them for you. What this means is that most (or all) of the beans in your Spring application can be declared and wired without using in your spring configuration file. By default, looks for classes that are annotated with one of a handful of special stereotype annotations: 

            1.@Component—A general-purpose stereotype annotation indicating that the class is a Spring component. 
            2.@Controller—Indicates that the class defines a Spring MVC controller.
            3.@Repository—Indicates that the class defines a data repository. 
            4.@Service—Indicates that the class defines a service. 
            5.Any custom annotation that is itself annotated with @Component. 
  
      d. load-time-wevaer: Registers an AspectJ load-time weaver.
      e. mbean-export: Exports beans as JMX MBeans.  
      f. spring-configured: Enables injection into objects that are not instantiated by Spring. 

3. jee: The JEE namespace provides configuration elements for looking up objects from JNDI as well as wiring references to EJBs into a Spring context.    
      a. jndi-environment: Defines environment settings for JNDI lookups.        
      b. jndi-lookup: Declares a reference to an object to be retrieved from JNDI.
      c. local-slsb: Declares a reference to a local stateless session EJB.
      d. remote-slsb: Declares a reference to a remote stateless session EJB.

 4. tx: The "tx" namespace provides support for declarative transactions across beans declared in Spring.
       
       a. advice: Declares transactional advice.
       b. annotation-driven: Tells Spring to use the @Transactional annotation for transactional rules.
       c.  attributes: Declares transactional rules for one or more methods.
       d. jta-transaction-manager: Configures a JTA transaction manager, automatically detecting WebLogic, WebSphere, or OC4J.
       e. method: Describes transactional rules for a given method signature.

5. mvc:

          a. annotation-driven: The tag packs a punch. It registers several features, including JSR-303 validation support, message conversion and support for field formatting.
          b. resources:  Configures a handler for serving static resources such as images, js, and, css files with cache headers optimized for efficient loading in a web browser. Allows resources to be served out of any path that is reachable via Spring's Resource handling.
          c. default-servlet-handler:  Configures a handler for serving static resources by forwarding to the Servlet container's default Servlet. Use of this handler allows using a "/" mapping with the Dispatcher Servlet while still utilizing the Servlet container to serve static resources.
          d. interceptors:  The ordered set of interceptors that intercept HTTP Servlet Requests handled by Controllers. Interceptors allow requests to be pre/post processed before/after handling. Each inteceptor must implement the org.springframework.web.servlet.HandlerInterceptor or org.springframework.web.context.request.WebRequestInterceptor interface. The interceptors in this set are automatically configured on each registered HandlerMapping. The URI paths each interceptor applies to are configurable.
          e. view-controller: The name of the view to render. If not specified, the view name will be determined from the current HttpServletRequest by the DispatcherServlet's  RequestToViewNameTranslator.

6. oxm:

          a. jaxb2-marshaller:
          b. class-to-be-bound

Boring right! But the guys who have experience in Spring or the guys who like to start working in Spring will soon realize that spending time to learn Spring configuration is a good investment. Enjoy!!
Reference: manning-spring-in-action( 3rd edition )

No comments:

Post a Comment

SyntaxHighlighter